we have drawing physics

rapier
Zynh0722 2024-02-16 23:12:30 -08:00
parent c8ae692c33
commit a663efd150
2 changed files with 28 additions and 18 deletions

View File

@ -1,4 +1,5 @@
use nannou::prelude::*; use nannou::prelude::*;
use rapier2d::geometry::{Collider, ColliderSet, TypedShape};
pub(crate) trait Drawable { pub(crate) trait Drawable {
fn draw(&self, draw: &Draw); fn draw(&self, draw: &Draw);
@ -13,6 +14,31 @@ where
} }
} }
impl Drawable for Collider {
fn draw(&self, draw: &Draw) {
match self.shape().as_typed_shape() {
TypedShape::Ball(ball) => {
draw.ellipse()
.radius(ball.radius)
.xy((*self.translation()).into());
}
TypedShape::Cuboid(cube) => {
draw.rect()
.w(cube.half_extents.x * 2.0)
.h(cube.half_extents.y * 2.0)
.xy((*self.translation()).into());
}
_ => eprintln!("Attempted to draw a shape with no draw impl"),
}
}
}
impl Drawable for ColliderSet {
fn draw(&self, draw: &Draw) {
self.iter().for_each(|(_, collider)| draw.draw(collider))
}
}
pub(crate) trait DrawShape<T> pub(crate) trait DrawShape<T>
where where
T: Drawable, T: Drawable,

View File

@ -21,7 +21,6 @@ struct Model {
_window: WindowId, _window: WindowId,
particles: Vec<Particle>, particles: Vec<Particle>,
engine: PhysicsEngine, engine: PhysicsEngine,
handles: Vec<RigidBodyHandle>,
} }
fn fill_particles(app: &App, particles: &mut Vec<Particle>) { fn fill_particles(app: &App, particles: &mut Vec<Particle>) {
@ -60,10 +59,8 @@ fn model(app: &App) -> Model {
..Default::default() ..Default::default()
}; };
let mut handles = Vec::new();
/* Create the ground. */ /* Create the ground. */
let collider = ColliderBuilder::cuboid(100.0, 0.1) let collider = ColliderBuilder::cuboid(100.0, 10.0)
.translation(vector![0.0, -200.0]) .translation(vector![0.0, -200.0])
.build(); .build();
engine.state.colliders.insert(collider); engine.state.colliders.insert(collider);
@ -79,8 +76,6 @@ fn model(app: &App) -> Model {
.colliders .colliders
.insert_with_parent(collider, ball_body_handle, &mut engine.state.bodies); .insert_with_parent(collider, ball_body_handle, &mut engine.state.bodies);
handles.push(ball_body_handle);
let mut particles = Vec::new(); let mut particles = Vec::new();
fill_particles(app, &mut particles); fill_particles(app, &mut particles);
@ -89,7 +84,6 @@ fn model(app: &App) -> Model {
_window, _window,
particles, particles,
engine, engine,
handles,
} }
} }
@ -123,17 +117,7 @@ fn view(app: &App, model: &Model, frame: Frame) {
let canvas = app.draw(); let canvas = app.draw();
canvas.background().color(CORNFLOWERBLUE); canvas.background().color(CORNFLOWERBLUE);
// canvas.draw(&model.particles); canvas.draw(&model.engine.state.colliders);
model
.handles
.iter()
.map(|handle| model.engine.state.bodies.get(*handle).unwrap())
.for_each(|body| {
canvas
.ellipse()
.radius(10.0)
.xy(std::convert::Into::<Vec2>::into(*body.translation()));
});
// I don't think there is even a fail condition in this function, but it returns a result? // I don't think there is even a fail condition in this function, but it returns a result?
canvas.to_frame(app, &frame).unwrap(); canvas.to_frame(app, &frame).unwrap();