-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Screen shake example #15567
Screen shake example #15567
Conversation
Closes #15564 |
@m-edlund could I get your review here? |
A ci test fails probably because of the way I use rng, how can I fix this? |
Swap to a seeded RNG, picking an arbitrary number for the seed rather than |
Co-authored-by: Alice Cecile <[email protected]>
Co-authored-by: Alice Cecile <[email protected]>
The generated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading the issue #15564, it says we should be using Camera::sub_camera_view
to do this rather than transform directly. I suggested some changes that switches it to that. Feel free to disregard this if you think we should be using Transform
instead.
examples/camera/screen_shake.rs
Outdated
fn screen_shake( | ||
time: Res<Time>, | ||
mut screen_shake: ResMut<ScreenShake>, | ||
mut query: Query<&mut Transform, With<Camera>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mut query: Query<&mut Transform, With<Camera>>, | |
mut query: Query<&mut Camera>, |
examples/camera/screen_shake.rs
Outdated
for mut transform in query.iter_mut() { | ||
transform.translation.x = rng.gen_range(-shake_amount..shake_amount); | ||
transform.translation.y = rng.gen_range(-shake_amount..shake_amount); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for mut transform in query.iter_mut() { | |
transform.translation.x = rng.gen_range(-shake_amount..shake_amount); | |
transform.translation.y = rng.gen_range(-shake_amount..shake_amount); | |
} | |
for mut camera in query.iter_mut() { | |
let sub_view = camera.sub_camera_view.as_mut().unwrap(); | |
sub_view.offset = Vec2::new( | |
rng.gen_range(-shake_amount..shake_amount), | |
rng.gen_range(-shake_amount..shake_amount), | |
); | |
} |
examples/camera/screen_shake.rs
Outdated
fn new(intensity: f32, duration: f32) -> Self { | ||
ScreenShake { | ||
intensity, | ||
duration, | ||
timer: Timer::from_seconds(0.0, TimerMode::Once), // Start timer at 0 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If intensity and duration is set on every call of start_shake
, setting the values in new serves no purpose and can be replaced by deriving Default for ScreenShake. As a bonus the Resource can then also be created with init_resource
instead of insert_resource
.
examples/camera/screen_shake.rs
Outdated
#[derive(Resource)] | ||
struct ScreenShake { | ||
intensity: f32, | ||
duration: f32, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duration is already encoded in the timer, and can thus be omitted.
The generated |
examples/camera/screen_shake.rs
Outdated
if screen_shake.timer.finished() { | ||
return; // No shake if the timer has finished | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be something to reset the transformation / the offset back to the default, once the screen shake timer has finished. Else the screen might end up stuck slightly off-center when the timer ends, depending on how intense the screen was shook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the contribution! I think having a screen shake example would be a great addition :)
This particular solution falls into a few traps discussed in this talk: Math for Game Programmers: Juicing Your Cameras With Math.
I would suggest you give it a watch and then overhaul the implementation. Feel free to ping me afterwards for a review.
Sorry I'm not enumerating my particular issues, but I don't think there is much value in doing that when the talk I linked does a way better job of explaining these things than I could hope to do :)
@janhohenheim thank you I'll watch it |
9a35c08
to
6e630dd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me
He approved the current version |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be good if the user could increase or decrease those variables with key bindings, but not a blocker for this PR, LGTM
@moOsama76 you'll need to adjust this example to match the latest changes to how cameras work :) Let us know if you need a hand. |
Sure |
@alice-i-cecile I need help |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You changed the size/full_size
of SubCameraView
(it was defualt previously, witch is 1.0:1.0
I think) and since offset
is relative to those values, you also have to change MAX_OFFSET
.
examples/camera/2d_screen_shake.rs
Outdated
|
||
// screen_shake parameters, maximum addition by frame not actual maximum overall values | ||
const MAX_ANGLE: f32 = 0.5; | ||
const MAX_OFFSET: f32 = 0.5; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const MAX_OFFSET: f32 = 0.5; | |
const MAX_OFFSET: f32 = 500.0; |
@alice-i-cecile Is there an issue? It works for me at the latest commit, assuming this is what it's supposed to look like: 2024-10-08.00-06-33.mp4 |
Looks like it's been resolved :) |
Objective
Closes #15564
Solution
Adds a screen shake example.