Compare commits

..

No commits in common. "52cac4a3eba5b948d8358adab0304a234c902ac9" and "a65e60a3162853cb8430119f36aecf98bcb1ee03" have entirely different histories.

2 changed files with 38 additions and 33 deletions

View file

@ -92,6 +92,22 @@ fn spawn_bike(
let body_collider =
Collider::capsule_endpoints(0.5, Vec3::new(0.0, 0.0, -0.65), Vec3::new(0.0, 0.0, 0.8));
let mut body = commands.spawn((xform, Visibility::default()));
body.insert((
Name::new("body"),
RigidBody::Dynamic,
body_collider,
CollisionLayers::from_bits(1, 1),
SleepingDisabled,
CyberBikeBody,
CatControllerState::default(),
ColliderDensity(1.4),
AngularDamping(0.2),
LinearDamping(0.1),
ExternalForce::ZERO.with_persistence(false),
ExternalTorque::ZERO.with_persistence(false),
));
let pbr_bundle = {
let color = Color::srgb(0.7, 0.05, 0.7);
let mut xform = Transform::default();
@ -104,29 +120,13 @@ fn spawn_bike(
TransformInterpolation,
)
};
let mut body = commands.spawn((xform, Visibility::default()));
body.insert((
Name::new("body"),
RigidBody::Dynamic,
body_collider,
CollisionLayers::from_bits(1, 1),
SleepingDisabled,
CyberBikeBody,
CatControllerState::default(),
ColliderDensity(1.6),
AngularDamping(0.2),
LinearDamping(0.1),
ExternalForce::ZERO.with_persistence(false),
ExternalTorque::ZERO.with_persistence(false),
));
body.insert(spawn_children(body.id(), meshes, materials))
.with_child(pbr_bundle);
let parent = body.id();
body.insert(spawn_children(parent, pbr_bundle, meshes, materials));
}
fn spawn_children(
parent: Entity,
body: impl Bundle,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) -> impl Bundle {
@ -147,6 +147,7 @@ fn spawn_children(
let rear_wheel_pos = REAR_ATTACH + (rear_rake * REST_DISTANCE);
children![
body,
(
RayCaster::new(FRONT_ATTACH, Dir3::new_unchecked(front_rake))
.with_max_hits(1)

View file

@ -20,9 +20,9 @@ pub struct CatControllerSettings {
impl Default for CatControllerSettings {
fn default() -> Self {
Self {
kp: 60.0,
kd: 30.0,
ki: 0.0,
kp: 55.0,
kd: 20.0,
ki: 12.5,
}
}
}
@ -260,7 +260,11 @@ mod systems {
hit_point + force,
Color::linear_rgb(1., 0.5, 0.2),
);
bike_forces.apply_force_at_point(force, hit_point, bike_xform.translation + com.0);
bike_forces.apply_force_at_point(
force,
caster.global_origin(),
bike_xform.translation + com.0,
);
} else {
xform.translation = config.attach + (caster.direction.as_vec3() * config.rest_dist);
state.reset();
@ -268,7 +272,7 @@ mod systems {
}
}
pub(super) fn wheel_action(
pub(super) fn steering(
bike_query: Single<
(
&Transform,
@ -287,7 +291,7 @@ mod systems {
let bike_vel = bike_vel.0;
let dt = time.delta().as_secs_f32();
let max_thrust = 500.0;
let max_thrust = 1000.0;
let yaw_angle = -yaw_to_angle(input.yaw);
for (mut state, config, wheel) in wheels.iter_mut() {
@ -300,8 +304,8 @@ mod systems {
let forward = normal.cross(*bike_xform.right());
let thrust_mag = input.throttle * max_thrust * dt;
let (thrust_dir, thrust_force) = match wheel {
CyberWheel::Rear => (forward, thrust_mag),
CyberWheel::Front => (rot * forward, thrust_mag * 0.1),
CyberWheel::Rear => (forward, thrust_mag * 0.5),
CyberWheel::Front => (rot * forward, thrust_mag),
};
let thrust = thrust_force * thrust_dir;
@ -364,11 +368,11 @@ mod systems {
let dt = time.delta_secs();
let speed = vel.length_squared();
let dspeed = speed * 0.0000001;
let speed = vel.length();
let dspeed = speed.powf(1.2) * 0.01;
let dir = vel.normalize_or_zero();
let drag = -dspeed * dt * DRAG_COEFF * dir;
if speed > 0.1 {
if speed > 1.0 {
force.apply_force_at_point(
drag,
xform.translation - (0.2 * *xform.down()),
@ -376,7 +380,7 @@ mod systems {
);
}
bevy::log::debug!("speed: {}, drag force: {}", vel.length(), drag.length());
bevy::log::debug!("speed: {}, drag force: {}", speed, drag.length());
gizmos.arrow(
xform.translation,
@ -423,7 +427,7 @@ mod systems {
}
}
use systems::{apply_lean, calculate_lean, drag, suspension, tweak, wheel_action};
use systems::{apply_lean, calculate_lean, drag, steering, suspension, tweak};
pub struct CyberPhysicsPlugin;
@ -438,7 +442,7 @@ impl Plugin for CyberPhysicsPlugin {
})
.add_systems(
FixedUpdate,
(calculate_lean, apply_lean, suspension, wheel_action, drag).chain(),
(calculate_lean, apply_lean, suspension, steering, drag).chain(),
)
.add_systems(Update, tweak);
}