Replicates old feel with Rapier-native code.

This commit is contained in:
Joe Ardent 2022-02-08 22:00:05 -08:00
parent 5e4ef7d5de
commit 3cbdb522f2
3 changed files with 29 additions and 23 deletions

View file

@ -10,8 +10,6 @@ use crate::{
pub struct MovementSettings { pub struct MovementSettings {
pub sensitivity: f32, pub sensitivity: f32,
pub accel: f32, pub accel: f32,
pub drag: f32,
pub gravity: f32,
} }
impl Default for MovementSettings { impl Default for MovementSettings {
@ -19,8 +17,6 @@ impl Default for MovementSettings {
Self { Self {
sensitivity: 1.0, sensitivity: 1.0,
accel: 40., accel: 40.,
drag: 0.0005,
gravity: 10.0,
} }
} }
} }
@ -37,6 +33,12 @@ fn setup_colliders(
}; };
let pcollide = ColliderBundle { let pcollide = ColliderBundle {
shape: ColliderShape::ball(PLANET_RADIUS).into(), shape: ColliderShape::ball(PLANET_RADIUS).into(),
material: ColliderMaterial {
friction: 0.0,
restitution: 0.3,
..Default::default()
}
.into(),
..Default::default() ..Default::default()
}; };
commands commands
@ -47,12 +49,24 @@ fn setup_colliders(
let (bike, xform) = bike_query.single(); let (bike, xform) = bike_query.single();
let mut bbody = RigidBodyBundle::default(); let mut bbody = RigidBodyBundle::default();
bbody.damping.angular_damping = 0.8; bbody.damping.angular_damping = 0.8;
bbody.damping.linear_damping = 0.4; bbody.damping.linear_damping = 0.5;
let isometry = Isometry::from_parts(xform.translation.into(), xform.rotation.into()); let isometry = Isometry::from_parts(xform.translation.into(), xform.rotation.into());
bbody.position = isometry.into(); bbody.position = isometry.into();
// collider // collider
let shape = ColliderShape::capsule(
Vec3::new(0.0, 0.0, -1.25).into(),
Vec3::new(0.0, 0.0, 1.2).into(),
0.4,
);
let bcollide = ColliderBundle { let bcollide = ColliderBundle {
shape: ColliderShape::ball(1.1).into(), shape: shape.into(),
mass_properties: ColliderMassProps::Density(0.3).into(),
material: ColliderMaterial {
friction: 0.0,
restitution: 0.3,
..Default::default()
}
.into(),
..Default::default() ..Default::default()
}; };
commands commands
@ -63,30 +77,26 @@ fn setup_colliders(
} }
fn gravity(xform: Query<&Transform, With<CyberBike>>, mut config: ResMut<RapierConfiguration>) { fn gravity(xform: Query<&Transform, With<CyberBike>>, mut config: ResMut<RapierConfiguration>) {
let gravity = xform.single().translation.normalize() * -10.0; let gravity = xform.single().translation.normalize() * -8.0;
config.gravity = gravity.into(); config.gravity = gravity.into();
} }
fn falling_cat( fn falling_cat(
time: Res<Time>,
mut bike_query: Query<(&Transform, &mut RigidBodyForcesComponent), With<CyberBike>>, mut bike_query: Query<(&Transform, &mut RigidBodyForcesComponent), With<CyberBike>>,
) { ) {
let dt = time.delta_seconds();
let (bike_xform, mut forces) = bike_query.single_mut(); let (bike_xform, mut forces) = bike_query.single_mut();
let up = bike_xform.translation.normalize(); let up = bike_xform.translation.normalize();
let cam_up = bike_xform.up(); let cam_up = bike_xform.up();
let cos = up.dot(cam_up); let cos = up.dot(cam_up);
let theta = cos.acos(); let theta = cos.acos();
let force = if !theta.is_normal() { 0.0 } else { theta * dt }; let force_mag = if !theta.is_normal() { 0.0 } else { theta * 5.0 };
let torque = cam_up.cross(up).normalize() * force * 50.0; let torque = cam_up.cross(up).normalize() * force_mag;
forces.torque = torque.into(); forces.torque = torque.into();
} }
fn update_forces( fn update_forces(
time: Res<Time>,
settings: Res<MovementSettings>, settings: Res<MovementSettings>,
input: Res<InputState>, input: Res<InputState>,
mut query: Query< mut query: Query<
@ -98,19 +108,17 @@ fn update_forces(
With<CyberBike>, With<CyberBike>,
>, >,
) { ) {
let dt = time.delta_seconds();
let (xform, mut forces, mut cmat) = query.single_mut(); let (xform, mut forces, mut cmat) = query.single_mut();
// thrust // thrust
let thrust: Vector3<f32> = let thrust: Vector3<f32> = (xform.forward() * input.throttle * settings.accel).into();
(xform.forward() * input.throttle * dt * settings.accel * 500.0).into();
forces.force += thrust; forces.force += thrust;
// brake // brake
cmat.friction = if input.brake { 1.0 } else { 0.0 }; cmat.friction = if input.brake { 2.0 } else { 0.0 };
// steering // steering
let torque: Vector3<f32> = (xform.up() * input.yaw * dt * settings.sensitivity * 500.0).into(); let torque: Vector3<f32> = (xform.up() * input.yaw * settings.sensitivity).into();
forces.torque += torque; forces.torque += torque;
} }

View file

@ -2,7 +2,7 @@ use bevy::prelude::*;
use crate::Label; use crate::Label;
pub const PLANET_RADIUS: f32 = 360.0; pub const PLANET_RADIUS: f32 = 860.0;
pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS + 100.0; pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS + 100.0;
#[derive(Component, Debug)] #[derive(Component, Debug)]

View file

@ -11,10 +11,8 @@ use cyber_rider::{
}; };
const MOVEMENT_SETTINGS: MovementSettings = MovementSettings { const MOVEMENT_SETTINGS: MovementSettings = MovementSettings {
sensitivity: 0.3, // default: 1.0 sensitivity: 4.0, // default: 1.0
accel: 20.0, // default: 40.0 accel: 15.0, // default: 40.0
drag: 0.0001, // default: 0.0005
gravity: 10.0, // default: 10.0
}; };
fn main() { fn main() {