-
-
Notifications
You must be signed in to change notification settings - Fork 338
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
New Example - Simple Physics Simulation (Confetti) #874
Conversation
A super simple 2D simulation where balls fall and bounce off each other. Click to spawn new balls.
Please run |
f472630
to
183d5c7
Compare
Apologies for the delay, Thanks! |
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.
Left some suggestions to remove unsafe
and a few other things.
@@ -0,0 +1,221 @@ | |||
use macroquad::prelude::*; | |||
extern crate micromath; |
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.
This crate is not part of the macroquad dependency tree. I tried running the program without this line and the line below and it worked fine though, so this should be removable.
const BALL_RADIUS: f32 = 5.0; | ||
|
||
// Please, don't change | ||
static mut BALL_ID: usize = 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.
You can use an AtomicUsize
here to avoid using the unsafe
static mut
:
static BALL_ID: AtomicUsize = AtomicUsize::new(0);
unsafe { | ||
id = BALL_ID; | ||
BALL_ID += 1; | ||
} |
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.
With an atomic as above, this is just let id = BALL_ID.fetch_add(1, Ordering::SeqCst);
.
|
||
fn collide(&mut self, balls: &[Ball]) { | ||
unsafe { | ||
let skip = (BALL_ID / 1000).max(8); |
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.
BALL_ID.load(Ordering::SeqCst)
instead of unsafe
.
unsafe { | ||
BALL_ID = 0; | ||
balls.clear(); | ||
} |
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.
ditto
} | ||
|
||
unsafe { | ||
if BALL_ID == 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.
ditto
x_vel, | ||
y_vel: 0.0, | ||
color: Color::from_rgba( | ||
(random as usize * 47 % 255) as u8, |
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.
random
is already an usize
though?
Thanks. I updated my code on my main and i pulled it over after fmt. |
PR to add an interactive 2D physics example to the example games.
Confetti is a super simple simulation where balls fall and collide with each other. Players Click to spawn new balls.
You can find my ReadMe with more instructions and description. Only uses std:u8 (Color) and Macroquad.
Confetti Repo (Written by me 31/12/24 and MIT)
A fun example I made to compare rust to a friend's python code. You can rename it as you see fit; I thought the balls looked like confetti (Runs smoothly with 5k balls).