Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

Andrew-McCall
Copy link

@Andrew-McCall Andrew-McCall commented Dec 31, 2024

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).

A super simple 2D simulation where balls fall and bounce off each other.
Click to spawn new balls.
@cyrgani
Copy link
Contributor

cyrgani commented Jan 3, 2025

Please run cargo fmt on your code for consistent style.

@Andrew-McCall
Copy link
Author

Apologies for the delay,
Ran cargo fmt

Thanks!

Copy link
Contributor

@cyrgani cyrgani left a 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;
Copy link
Contributor

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;
Copy link
Contributor

@cyrgani cyrgani Jan 8, 2025

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;
}
Copy link
Contributor

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);
Copy link
Contributor

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();
}
Copy link
Contributor

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 {
Copy link
Contributor

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,
Copy link
Contributor

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?

@Andrew-McCall
Copy link
Author

Thanks. I updated my code on my main and i pulled it over after fmt.
but left my testing and other software rot in it. My bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants