Created
November 20, 2022 02:40
-
-
Save f0ursqu4r3/db0049d4e4bd781eede3284594560312 to your computer and use it in GitHub Desktop.
Stupid Physics with rust and Rapier2D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use rapier2d::prelude::*; | |
use raylib::prelude::*; | |
pub struct Entity { | |
radius: f32, | |
physics_handle: RigidBodyHandle, | |
} | |
fn main() { | |
let (mut rl, thread) = raylib::init().size(640, 480).title("Hello, World").build(); | |
let mut entities: Vec<Entity> = vec![]; | |
let mut rigid_body_set = RigidBodySet::new(); | |
let mut collider_set = ColliderSet::new(); | |
let gravity = vector![0.0, 0.0]; | |
let integration_parameters = IntegrationParameters::default(); | |
let mut physics_pipeline = PhysicsPipeline::new(); | |
let mut island_manager = IslandManager::new(); | |
let mut broad_phase = BroadPhase::new(); | |
let mut narrow_phase = NarrowPhase::new(); | |
let mut impulse_joint_set = ImpulseJointSet::new(); | |
let mut multibody_joint_set = MultibodyJointSet::new(); | |
let mut ccd_solver = CCDSolver::new(); | |
let physics_hooks = (); | |
let event_handler = (); | |
// build the floor, ceiling, and walls | |
collider_set.insert( | |
ColliderBuilder::cuboid(320.0, 2.0) | |
.translation(vector![320.0, 476.0]) | |
.build(), | |
); | |
collider_set.insert( | |
ColliderBuilder::cuboid(320.0, 2.0) | |
.translation(vector![320.0, 2.0]) | |
.build(), | |
); | |
collider_set.insert( | |
ColliderBuilder::cuboid(2.0, 240.0) | |
.translation(vector![636.0, 240.0]) | |
.build(), | |
); | |
collider_set.insert( | |
ColliderBuilder::cuboid(2.0, 240.0) | |
.translation(vector![2.0, 240.0]) | |
.build(), | |
); | |
while !rl.window_should_close() { | |
if rl.is_mouse_button_down(MouseButton::MOUSE_LEFT_BUTTON) { | |
let mut pos = rl.get_mouse_position(); | |
pos.x += rand::random::<f32>() * 2.0 - 1.0; | |
pos.y += rand::random::<f32>() * 2.0 - 1.0; | |
let radius = 4.0; | |
let rb = RigidBodyBuilder::dynamic() | |
.translation(vector![pos.x, pos.y]) | |
.build(); | |
let c = ColliderBuilder::ball(radius).restitution(0.1).build(); | |
let bbh = rigid_body_set.insert(rb); | |
collider_set.insert_with_parent(c, bbh, &mut rigid_body_set); | |
let ent = Entity { | |
radius, | |
physics_handle: bbh, | |
}; | |
entities.push(ent); | |
} | |
let _dt = rl.get_frame_time(); | |
physics_pipeline.step( | |
&gravity, | |
&integration_parameters, | |
&mut island_manager, | |
&mut broad_phase, | |
&mut narrow_phase, | |
&mut rigid_body_set, | |
&mut collider_set, | |
&mut impulse_joint_set, | |
&mut multibody_joint_set, | |
&mut ccd_solver, | |
&physics_hooks, | |
&event_handler, | |
); | |
let mut d = rl.begin_drawing(&thread); | |
d.clear_background(Color::new(30, 20, 30, 255)); | |
// draw the entities | |
for e in &entities { | |
let bb = &rigid_body_set[e.physics_handle]; | |
d.draw_circle_v( | |
Vector2::new(bb.translation().x as f32, bb.translation().y as f32), | |
e.radius, | |
Color::GREEN, | |
); | |
} | |
// draw the number of entities | |
d.draw_text( | |
&format!("Entities: {}", entities.len()), | |
13, | |
13, | |
20, | |
Color::BLACK, | |
); | |
d.draw_text( | |
&format!("Entities: {}", entities.len()), | |
12, | |
12, | |
20, | |
Color::RAYWHITE, | |
); | |
// draw the framerate | |
d.draw_text(&format!("FPS: {}", d.get_fps()), 13, 33, 20, Color::BLACK); | |
d.draw_text( | |
&format!("FPS: {}", d.get_fps()), | |
12, | |
32, | |
20, | |
Color::RAYWHITE, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment