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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 221 additions & 0 deletions examples/confetti.rs
Original file line number Diff line number Diff line change
@@ -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.

use micromath::F32Ext;

fn clip(pos: f32, bound: f32, velocity: f32) -> f32 {
if pos > bound {
let delta = (pos - bound).abs();
let mut new_velocity = velocity;
if velocity > 0.0 {
new_velocity = -velocity
}
if (new_velocity.abs() + BALL_RADIUS) < delta {
return -delta / get_fps() as f32 * 60.0;
}
return new_velocity * FLOOR_LOSS;
}
velocity
}

fn clip_minus(pos: f32, bound: f32, velocity: f32) -> f32 {
return -clip(bound, pos, -velocity);
}

#[derive(Debug, Clone, Copy)]
pub struct Ball {
id: usize,
x: f32,
y: f32,
x_vel: f32,
y_vel: f32,
color: Color,
}

// Customizable
const FLOOR_LOSS: f32 = 0.9;
const DRAG: f32 = 0.998;
const GRAVITY: f32 = 1.0;
const MAX_VEL: f32 = 40.0;

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

const BALL_RADIUS_SQR: f32 = BALL_RADIUS * BALL_RADIUS;

impl Ball {
fn new(x: f32, y: f32, seed: usize) -> Ball {
let id;
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);.


let seed = seed % (usize::MAX / 64);
let random = (32_1239 * seed * seed * seed * 17 + id) % 1000;
let x_vel = random % 9;
let x_vel = (x_vel as f32 - 4.0) / 1.5;

return Ball {
id,
x,
y,
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?

(random as usize * 29 % 255) as u8,
(random as usize * 101 % 255) as u8,
255,
),
};
}

fn tick(&mut self) {
self.x_vel *= DRAG;
self.y_vel *= DRAG;

self.x += self.x_vel;
self.y += self.y_vel;

self.x = self.x.clamp(-3000.0, 3000.0);
self.x = if self.x.is_normal() { self.x } else { 0.0 };
self.y = self.y.clamp(-3000.0, 3000.0);
self.y = if self.y.is_normal() { self.y } else { 0.0 };
self.y_vel = self.y_vel.clamp(-MAX_VEL, MAX_VEL);
self.y_vel = if self.y_vel.is_normal() {
self.y_vel
} else {
0.0
};
self.x_vel = self.x_vel.clamp(-MAX_VEL, MAX_VEL);
self.x_vel = if self.x_vel.is_normal() {
self.x_vel
} else {
0.0
};

self.y_vel = clip(self.y, screen_height(), self.y_vel + GRAVITY);
self.x_vel = clip(self.x, screen_width(), self.x_vel);
self.x_vel = clip_minus(self.x, 0.0, self.x_vel);
self.y_vel = clip_minus(self.y, -screen_height(), self.y_vel);
}

fn draw(&self) {
draw_circle(self.x, self.y - BALL_RADIUS, BALL_RADIUS, self.color);
}

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.


for other in balls {
if other.id == self.id || other.id % 10 < skip {
continue;
}

let dx = self.x - other.x;
let dy = self.y - other.y;
let distance_sqr = dx * dx + dy * dy;

if distance_sqr > BALL_RADIUS_SQR {
continue;
}

let distance = distance_sqr.sqrt();
if distance == 0.0 {
continue;
}
let nx = dx / distance;
let ny = dy / distance;

let relative_velocity_x = self.x_vel - other.x_vel;
let relative_velocity_y = self.y_vel - other.y_vel;
let relative_velocity_normal = relative_velocity_x * nx + relative_velocity_y * ny;

if relative_velocity_normal > 0.0 {
continue;
}

let impulse = (2.0 * relative_velocity_normal) / 2.0 * FLOOR_LOSS;

self.x_vel -= (impulse * nx).clamp(-MAX_VEL, MAX_VEL);
self.y_vel -= (impulse * ny).clamp(-MAX_VEL, MAX_VEL);
}
}
}
}

#[macroquad::main("AM - Confetti")]
async fn main() {
let mut balls: Vec<Ball> = Vec::new();
let mut frame_count: usize = 0;
loop {
// Loop start
frame_count += 1;
clear_background(Color {
r: 0.95,
g: 0.9,
b: 0.9,
a: 1.0,
});

// Handle Inputs
if is_mouse_button_down(MouseButton::Left) {
let (mouse_x, mouse_y) = mouse_position();
balls.push(Ball::new(mouse_x, mouse_y, frame_count));
balls.push(Ball::new(mouse_x, mouse_y, frame_count));
}
if is_mouse_button_pressed(MouseButton::Right) {
let (mouse_x, mouse_y) = mouse_position();
balls.push(Ball::new(mouse_x, mouse_y, frame_count))
}
if is_key_pressed(KeyCode::Space) {
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

}

// Handle Tick
let balls_prev = balls.to_vec();
for ball in &mut balls {
ball.collide(&balls_prev);
ball.tick();
ball.draw();
}

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

draw_text(
"Click anywhere to begin!",
screen_width() / 2.0 - 240.0,
screen_height() / 2.0,
48.0,
RED,
);
} else {
draw_text(
&format!(
"Balls: {}\nFPS: {} {}",
BALL_ID,
get_fps(),
if BALL_ID > 1000 {
"(Press Space to Reset)"
} else {
""
}
),
15.0,
25.0,
32.0,
BLACK,
);
}
}

next_frame().await
}
}
// Andrew McCall <3
// https://github.com/Andrew-McCall/MacroquadConfetti