we have drawing physics

This commit is contained in:
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 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<T>
where
T: Drawable,

View file

@ -21,7 +21,6 @@ struct Model {
_window: WindowId,
particles: Vec<Particle>,
engine: PhysicsEngine,
handles: Vec<RigidBodyHandle>,
}
fn fill_particles(app: &App, particles: &mut Vec<Particle>) {
@ -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::<Vec2>::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();