use computed center of mass
This commit is contained in:
parent
15b98b6995
commit
81bec0b2b4
2 changed files with 28 additions and 13 deletions
|
@ -18,7 +18,7 @@ use bevy::{
|
||||||
|
|
||||||
use crate::physics::CatControllerState;
|
use crate::physics::CatControllerState;
|
||||||
|
|
||||||
pub const SPRING_CONSTANT: Scalar = 50.0;
|
pub const SPRING_CONSTANT: Scalar = 40.0;
|
||||||
pub const DAMPING_CONSTANT: Scalar = 10.0;
|
pub const DAMPING_CONSTANT: Scalar = 10.0;
|
||||||
pub const WHEEL_RADIUS: Scalar = 0.4;
|
pub const WHEEL_RADIUS: Scalar = 0.4;
|
||||||
pub const REST_DISTANCE: Scalar = 1.5 + WHEEL_RADIUS;
|
pub const REST_DISTANCE: Scalar = 1.5 + WHEEL_RADIUS;
|
||||||
|
|
|
@ -22,7 +22,7 @@ impl Default for CatControllerSettings {
|
||||||
Self {
|
Self {
|
||||||
kp: 45.0,
|
kp: 45.0,
|
||||||
kd: 15.0,
|
kd: 15.0,
|
||||||
ki: 1.0,
|
ki: 2.5,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,10 @@ impl CatControllerState {
|
||||||
mod systems {
|
mod systems {
|
||||||
use std::f32::consts::{FRAC_PI_3, FRAC_PI_4};
|
use std::f32::consts::{FRAC_PI_3, FRAC_PI_4};
|
||||||
|
|
||||||
use avian3d::prelude::*;
|
use avian3d::prelude::{
|
||||||
|
ComputedCenterOfMass, ExternalForce, Gravity, LinearVelocity, RayCaster, RayHits,
|
||||||
|
RigidBodyQueryReadOnly,
|
||||||
|
};
|
||||||
use bevy::{
|
use bevy::{
|
||||||
ecs::error::BevyError,
|
ecs::error::BevyError,
|
||||||
prelude::{
|
prelude::{
|
||||||
|
@ -183,7 +186,10 @@ mod systems {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn suspension(
|
pub(super) fn suspension(
|
||||||
mut bike_body_query: Query<(&Transform, &mut ExternalForce), With<CyberBikeBody>>,
|
mut bike_body_query: Query<
|
||||||
|
(&Transform, &ComputedCenterOfMass, &mut ExternalForce),
|
||||||
|
With<CyberBikeBody>,
|
||||||
|
>,
|
||||||
mut wheel_mesh_query: Query<
|
mut wheel_mesh_query: Query<
|
||||||
(&mut Transform, &mut WheelState, &WheelConfig, &CyberWheel),
|
(&mut Transform, &mut WheelState, &WheelConfig, &CyberWheel),
|
||||||
Without<CyberBikeBody>,
|
Without<CyberBikeBody>,
|
||||||
|
@ -213,7 +219,7 @@ mod systems {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let (bike_xform, mut bike_forces) = bike_body_query.single_mut()?;
|
let (bike_xform, com, mut bike_forces) = bike_body_query.single_mut()?;
|
||||||
|
|
||||||
for (mut xform, mut state, config, wheel) in wheel_mesh_query.iter_mut() {
|
for (mut xform, mut state, config, wheel) in wheel_mesh_query.iter_mut() {
|
||||||
let (caster, hits) = match wheel {
|
let (caster, hits) = match wheel {
|
||||||
|
@ -248,7 +254,7 @@ mod systems {
|
||||||
bike_forces.apply_force_at_point(
|
bike_forces.apply_force_at_point(
|
||||||
force,
|
force,
|
||||||
caster.global_origin(),
|
caster.global_origin(),
|
||||||
bike_xform.translation,
|
bike_xform.translation + com.0,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
xform.translation = config.attach + (caster.direction.as_vec3() * config.rest_dist);
|
xform.translation = config.attach + (caster.direction.as_vec3() * config.rest_dist);
|
||||||
|
@ -277,7 +283,7 @@ mod systems {
|
||||||
let bike_vel = bike_vel.0;
|
let bike_vel = bike_vel.0;
|
||||||
|
|
||||||
let dt = time.delta().as_secs_f32();
|
let dt = time.delta().as_secs_f32();
|
||||||
let max_thrust = 5000.0;
|
let max_thrust = 1000.0;
|
||||||
let yaw_angle = -yaw_to_angle(input.yaw);
|
let yaw_angle = -yaw_to_angle(input.yaw);
|
||||||
|
|
||||||
for (mut state, config, wheel) in wheels.iter_mut() {
|
for (mut state, config, wheel) in wheels.iter_mut() {
|
||||||
|
@ -290,7 +296,7 @@ mod systems {
|
||||||
let forward = normal.cross(*bike_xform.right());
|
let forward = normal.cross(*bike_xform.right());
|
||||||
let thrust_mag = input.throttle * max_thrust * dt;
|
let thrust_mag = input.throttle * max_thrust * dt;
|
||||||
let (thrust_dir, thrust_force) = match wheel {
|
let (thrust_dir, thrust_force) = match wheel {
|
||||||
CyberWheel::Rear => (forward, thrust_mag * 0.1),
|
CyberWheel::Rear => (forward, thrust_mag * 0.5),
|
||||||
CyberWheel::Front => (rot * forward, thrust_mag),
|
CyberWheel::Front => (rot * forward, thrust_mag),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -318,7 +324,11 @@ mod systems {
|
||||||
state.sliding = false;
|
state.sliding = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bike_force.apply_force_at_point(force, contact_point, bike_xform.translation);
|
bike_force.apply_force_at_point(
|
||||||
|
force,
|
||||||
|
contact_point,
|
||||||
|
bike_xform.translation + bike_body.center_of_mass.0,
|
||||||
|
);
|
||||||
gizmos.arrow(
|
gizmos.arrow(
|
||||||
contact_point,
|
contact_point,
|
||||||
contact_point + friction,
|
contact_point + friction,
|
||||||
|
@ -337,13 +347,18 @@ mod systems {
|
||||||
|
|
||||||
pub(super) fn drag(
|
pub(super) fn drag(
|
||||||
mut bike_query: Query<
|
mut bike_query: Query<
|
||||||
(&Transform, &LinearVelocity, &mut ExternalForce),
|
(
|
||||||
|
&Transform,
|
||||||
|
&LinearVelocity,
|
||||||
|
&ComputedCenterOfMass,
|
||||||
|
&mut ExternalForce,
|
||||||
|
),
|
||||||
With<CyberBikeBody>,
|
With<CyberBikeBody>,
|
||||||
>,
|
>,
|
||||||
mut gizmos: Gizmos,
|
mut gizmos: Gizmos,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
) -> Result {
|
) -> Result {
|
||||||
let (xform, vel, mut force) = bike_query.single_mut()?;
|
let (xform, vel, com, mut force) = bike_query.single_mut()?;
|
||||||
|
|
||||||
let dt = time.delta_secs();
|
let dt = time.delta_secs();
|
||||||
|
|
||||||
|
@ -354,8 +369,8 @@ mod systems {
|
||||||
if speed > 1.0 {
|
if speed > 1.0 {
|
||||||
force.apply_force_at_point(
|
force.apply_force_at_point(
|
||||||
drag,
|
drag,
|
||||||
xform.translation - (0.1 * *xform.down()),
|
xform.translation - (0.2 * *xform.down()),
|
||||||
xform.translation,
|
xform.translation + com.0,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue