From a663efd1508a673d31e81381ecbfaca7d5766394 Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Fri, 16 Feb 2024 23:12:30 -0800 Subject: [PATCH] we have drawing physics --- src/drawable.rs | 26 ++++++++++++++++++++++++++ src/main.rs | 20 ++------------------ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/drawable.rs b/src/drawable.rs index b919a54..bc6aa26 100644 --- a/src/drawable.rs +++ b/src/drawable.rs @@ -1,4 +1,5 @@ use nannou::prelude::*; +use rapier2d::geometry::{Collider, ColliderSet, TypedShape}; pub(crate) trait Drawable { 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 where T: Drawable, diff --git a/src/main.rs b/src/main.rs index e74987c..59366b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,6 @@ struct Model { _window: WindowId, particles: Vec, engine: PhysicsEngine, - handles: Vec, } fn fill_particles(app: &App, particles: &mut Vec) { @@ -60,10 +59,8 @@ fn model(app: &App) -> Model { ..Default::default() }; - let mut handles = Vec::new(); - /* 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]) .build(); engine.state.colliders.insert(collider); @@ -79,8 +76,6 @@ fn model(app: &App) -> Model { .colliders .insert_with_parent(collider, ball_body_handle, &mut engine.state.bodies); - handles.push(ball_body_handle); - let mut particles = Vec::new(); fill_particles(app, &mut particles); @@ -89,7 +84,6 @@ fn model(app: &App) -> Model { _window, particles, engine, - handles, } } @@ -123,17 +117,7 @@ fn view(app: &App, model: &Model, frame: Frame) { let canvas = app.draw(); canvas.background().color(CORNFLOWERBLUE); - // canvas.draw(&model.particles); - model - .handles - .iter() - .map(|handle| model.engine.state.bodies.get(*handle).unwrap()) - .for_each(|body| { - canvas - .ellipse() - .radius(10.0) - .xy(std::convert::Into::::into(*body.translation())); - }); + canvas.draw(&model.engine.state.colliders); // I don't think there is even a fail condition in this function, but it returns a result? canvas.to_frame(app, &frame).unwrap();