Skip to content

Commit 3fdf40d

Browse files
committed
Make the contributor birbs bounce to the window height! (#5274)
Birbs no longer bounce too low, not coming close to their true bouncy potential. Birbs also no longer bonk head when window is smaller. (Will still bonk head when window is made smaller too fast! pls no) *cough cough* Make the height of the birb-bounces dependent on the window size so they always bounce elegantly towards the top of the window. Also no longer panics when closing the window q: ~~Might put a video here if I figure out how to.~~ <sup> rendering video is hard. birbrate go brr Co-authored-by: devil-ira <justthecooldude@gmail.com>
1 parent d4f8f88 commit 3fdf40d

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

examples/games/contributors.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct Velocity {
5858
rotation: f32,
5959
}
6060

61-
const GRAVITY: f32 = -9.821 * 100.0;
61+
const GRAVITY: f32 = 9.821 * 100.0;
6262
const SPRITE_SIZE: f32 = 75.0;
6363

6464
const SATURATION_DESELECTED: f32 = 0.3;
@@ -243,7 +243,7 @@ fn velocity_system(time: Res<Time>, mut velocity_query: Query<&mut Velocity>) {
243243
let delta = time.delta_seconds();
244244

245245
for mut velocity in &mut velocity_query {
246-
velocity.translation += Vec3::new(0.0, GRAVITY * delta, 0.0);
246+
velocity.translation.y -= GRAVITY * delta;
247247
}
248248
}
249249

@@ -256,16 +256,23 @@ fn collision_system(
256256
windows: Res<Windows>,
257257
mut query: Query<(&mut Velocity, &mut Transform), With<Contributor>>,
258258
) {
259-
let mut rng = rand::thread_rng();
260-
261-
let window = windows.primary();
259+
let window = if let Some(window) = windows.get_primary() {
260+
window
261+
} else {
262+
return;
263+
};
262264

263265
let ceiling = window.height() / 2.;
264266
let ground = -(window.height() / 2.);
265267

266268
let wall_left = -(window.width() / 2.);
267269
let wall_right = window.width() / 2.;
268270

271+
// The maximum height the birbs should try to reach is one birb below the top of the window.
272+
let max_bounce_height = window.height() - SPRITE_SIZE * 2.0;
273+
274+
let mut rng = rand::thread_rng();
275+
269276
for (mut velocity, mut transform) in &mut query {
270277
let left = transform.translation.x - SPRITE_SIZE / 2.0;
271278
let right = transform.translation.x + SPRITE_SIZE / 2.0;
@@ -275,11 +282,16 @@ fn collision_system(
275282
// clamp the translation to not go out of the bounds
276283
if bottom < ground {
277284
transform.translation.y = ground + SPRITE_SIZE / 2.0;
278-
// apply an impulse upwards
279-
velocity.translation.y = rng.gen_range(700.0..1000.0);
285+
286+
// How high this birb will bounce.
287+
let bounce_height = rng.gen_range((max_bounce_height * 0.4)..max_bounce_height);
288+
289+
// Apply the velocity that would bounce the birb up to bounce_height.
290+
velocity.translation.y = (bounce_height * GRAVITY * 2.).sqrt();
280291
}
281292
if top > ceiling {
282293
transform.translation.y = ceiling - SPRITE_SIZE / 2.0;
294+
velocity.translation.y *= -1.0;
283295
}
284296
// on side walls flip the horizontal velocity
285297
if left < wall_left {

0 commit comments

Comments
 (0)