From 4dd8e0a127e64b832a4f7593cf16437d83c2450d Mon Sep 17 00:00:00 2001 From: shnewto Date: Tue, 13 Aug 2024 20:55:53 -0400 Subject: [PATCH] use velocity to move the car instead of just changing the transform --- examples/avian2d_colliders.rs | 21 ++++++++++++--------- src/collider/avian2d.rs | 11 +++++------ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/examples/avian2d_colliders.rs b/examples/avian2d_colliders.rs index 5020686..c129bed 100644 --- a/examples/avian2d_colliders.rs +++ b/examples/avian2d_colliders.rs @@ -6,11 +6,9 @@ use bevy::pbr::wireframe::WireframePlugin; use bevy::prelude::*; use bevy::render::settings::{RenderCreation, WgpuFeatures, WgpuSettings}; use bevy::render::RenderPlugin; +use bevy_collider_gen::avian2d::single_convex_polyline_collider_translated; use bevy_collider_gen::{ - avian2d::{ - multi_convex_polyline_collider_translated, single_convex_polyline_collider_translated, - single_heightfield_collider_translated, - }, + avian2d::{multi_convex_polyline_collider_translated, single_heightfield_collider_translated}, Edges, }; use bevy_prototype_lyon::prelude::{Fill, GeometryBuilder, ShapePlugin}; @@ -68,7 +66,7 @@ pub fn car_spawn( game_assets: Res, image_assets: Res>, ) { - let initial_xyz = Vec3::new(-200.0, -4.0, 0.0); + let initial_xyz = Vec3::new(-200.0, -5.0, 0.0); let sprite_handle = game_assets.image_handles.get("car_handle"); if sprite_handle.is_none() { return; @@ -356,17 +354,22 @@ pub fn controls_text_spawn(mut commands: Commands, game_assets: Res) }); } -pub fn car_movement(mut query: Query<(&Car, &mut Transform)>, keys: Res>) { - for (car, mut transform) in query.iter_mut() { +pub fn car_movement( + mut query: Query<(&Car, &mut LinearVelocity, &mut Transform)>, + keys: Res>, +) { + for (car, mut linear_velocity, mut transform) in query.iter_mut() { if keys.pressed(KeyCode::KeyD) { - transform.translation.x += 5.0; + linear_velocity.x += 30.0; } if keys.pressed(KeyCode::KeyA) { - transform.translation.x -= 5.0; + linear_velocity.x -= 30.0; } if keys.pressed(KeyCode::Digit1) { + linear_velocity.x = 0.0; + linear_velocity.y = 0.0; *transform = Transform::from_xyz(car.initial_xyz.x, car.initial_xyz.y, car.initial_xyz.z); } diff --git a/src/collider/avian2d.rs b/src/collider/avian2d.rs index 566f153..ea46bfe 100644 --- a/src/collider/avian2d.rs +++ b/src/collider/avian2d.rs @@ -1,8 +1,7 @@ use avian2d::{ - math::Vector2, + math::{Scalar, Vector, Vector2}, parry::{ - math::{Point, Real, Vector}, - na::DVector, + math::{Point, Real}, shape::SharedShape, }, prelude::Collider, @@ -167,10 +166,10 @@ pub fn multi_convex_hull_collider_raw(image: &Image) -> Vec> { /// parses x,y points into y values at the top of the image (smallest y) and creates a /// heightfield collider fn heightfield_collider_from_points(v: &[Vec2]) -> Collider { - let hf: DVector = heights_from_points(v).into(); + let hf: Vec = heights_from_points(v); let x_scale: Real = hf.len() as f32 - 1.0; - let scale: Vector = Vector2::new(x_scale, 1.0).into(); - Collider::from(SharedShape::heightfield(hf, scale)) + let scale: Vector = Vector2::new(x_scale, 1.0); + Collider::heightfield(hf, scale) } /// takes x,y points collects the y values at the top of the image (smallest y)