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 <alice.i.cecile@gmail.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
|
The generated |
There was a problem hiding this comment.
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.
| 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.
| mut query: Query<&mut Transform, With<Camera>>, | |
| mut query: Query<&mut Camera>, |
| 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.
| 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), | |
| ); | |
| } |
| 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.
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.
| #[derive(Resource)] | ||
| struct ScreenShake { | ||
| intensity: f32, | ||
| duration: f32, |
There was a problem hiding this comment.
Duration is already encoded in the timer, and can thus be omitted.
|
The generated |
| if screen_shake.timer.finished() { | ||
| return; // No shake if the timer has finished | ||
| } |
There was a problem hiding this comment.
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.
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
He approved the current version |
pablo-lua
left a comment
There was a problem hiding this comment.
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 |
|
|
||
| // 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.
| 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.