Skip to content

Commit

Permalink
Merge pull request #17 from shnewto/update-avian-example
Browse files Browse the repository at this point in the history
use velocity to move the car instead of just changing the transform
  • Loading branch information
shnewto authored Aug 14, 2024
2 parents 3f0c0e8 + 4dd8e0a commit 150ff6c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
21 changes: 12 additions & 9 deletions examples/avian2d_colliders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -68,7 +66,7 @@ pub fn car_spawn(
game_assets: Res<GameAsset>,
image_assets: Res<Assets<Image>>,
) {
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;
Expand Down Expand Up @@ -356,17 +354,22 @@ pub fn controls_text_spawn(mut commands: Commands, game_assets: Res<GameAsset>)
});
}

pub fn car_movement(mut query: Query<(&Car, &mut Transform)>, keys: Res<ButtonInput<KeyCode>>) {
for (car, mut transform) in query.iter_mut() {
pub fn car_movement(
mut query: Query<(&Car, &mut LinearVelocity, &mut Transform)>,
keys: Res<ButtonInput<KeyCode>>,
) {
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);
}
Expand Down
11 changes: 5 additions & 6 deletions src/collider/avian2d.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -167,10 +166,10 @@ pub fn multi_convex_hull_collider_raw(image: &Image) -> Vec<Option<Collider>> {
/// 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<Real> = heights_from_points(v).into();
let hf: Vec<Scalar> = heights_from_points(v);
let x_scale: Real = hf.len() as f32 - 1.0;
let scale: Vector<Real> = 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)
Expand Down

0 comments on commit 150ff6c

Please sign in to comment.