checkpoint
This commit is contained in:
parent
5852df32f7
commit
8978a25965
2 changed files with 14 additions and 7 deletions
|
@ -22,11 +22,11 @@ pub enum CyberWheel {
|
||||||
|
|
||||||
#[derive(Component, Clone, Copy, Debug, Reflect, Default)]
|
#[derive(Component, Clone, Copy, Debug, Reflect, Default)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
#[require(LinearVelocity, RigidBody(|| RigidBody::Kinematic))]
|
|
||||||
pub struct WheelState {
|
pub struct WheelState {
|
||||||
pub displacement: Scalar,
|
pub displacement: Scalar,
|
||||||
pub sliding: bool,
|
pub sliding: bool,
|
||||||
pub grounded: bool,
|
pub grounded: bool,
|
||||||
|
pub ppos: Vec3,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Clone, Copy, Debug, Reflect)]
|
#[derive(Component, Clone, Copy, Debug, Reflect)]
|
||||||
|
@ -126,7 +126,7 @@ fn spawn_wheels(
|
||||||
&mut materials,
|
&mut materials,
|
||||||
front_wheel_pos,
|
front_wheel_pos,
|
||||||
mesh.clone(),
|
mesh.clone(),
|
||||||
WheelConfig::new(FRONT_ATTACH, REST_DISTANCE, 1200., -160., 0.5),
|
WheelConfig::new(FRONT_ATTACH, REST_DISTANCE, 800., -160., 0.5),
|
||||||
CyberWheel::Front,
|
CyberWheel::Front,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -148,7 +148,7 @@ fn spawn_wheels(
|
||||||
&mut materials,
|
&mut materials,
|
||||||
rear_wheel_pos,
|
rear_wheel_pos,
|
||||||
mesh,
|
mesh,
|
||||||
WheelConfig::new(REAR_ATTACH, REST_DISTANCE, 1200., -160., 0.5),
|
WheelConfig::new(REAR_ATTACH, REST_DISTANCE, 800., -160., 0.5),
|
||||||
CyberWheel::Rear,
|
CyberWheel::Rear,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -163,8 +163,8 @@ mod systems {
|
||||||
(
|
(
|
||||||
&mut Transform,
|
&mut Transform,
|
||||||
&mut WheelState,
|
&mut WheelState,
|
||||||
|
&GlobalTransform,
|
||||||
&WheelConfig,
|
&WheelConfig,
|
||||||
&LinearVelocity,
|
|
||||||
&CyberWheel,
|
&CyberWheel,
|
||||||
),
|
),
|
||||||
Without<CyberBikeBody>,
|
Without<CyberBikeBody>,
|
||||||
|
@ -202,21 +202,23 @@ mod systems {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let (bike_xform, mut bike_forces) = bike_body_query.single_mut();
|
let (bike_xform, mut bike_forces) = bike_body_query.single_mut();
|
||||||
for (mut xform, mut state, config, velocity, wheel) in wheel_mesh_query.iter_mut() {
|
for (mut xform, mut state, global_xform, config, wheel) in wheel_mesh_query.iter_mut() {
|
||||||
let (caster, hits) = match wheel {
|
let (caster, hits) = match wheel {
|
||||||
CyberWheel::Front => (front_caster, front_hits),
|
CyberWheel::Front => (front_caster, front_hits),
|
||||||
CyberWheel::Rear => (rear_caster, rear_hits),
|
CyberWheel::Rear => (rear_caster, rear_hits),
|
||||||
};
|
};
|
||||||
|
|
||||||
let prev = &mut state.displacement;
|
let prev = &mut state.displacement;
|
||||||
for hit in hits.iter() {
|
if let Some(hit) = hits.iter().next() {
|
||||||
let force = suspension_force(caster, hit, config, prev, dt, &mut xform);
|
let force = suspension_force(caster, hit, config, prev, dt, &mut xform);
|
||||||
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,
|
||||||
);
|
);
|
||||||
dbg!(velocity);
|
let vel = (global_xform.translation() - state.ppos) / dt;
|
||||||
|
dbg!(vel);
|
||||||
|
state.ppos = global_xform.translation();
|
||||||
let normal = hit.normal1;
|
let normal = hit.normal1;
|
||||||
let steering = match wheel {
|
let steering = match wheel {
|
||||||
CyberWheel::Front => normal.cross(*bike_xform.back()) * yaw,
|
CyberWheel::Front => normal.cross(*bike_xform.back()) * yaw,
|
||||||
|
@ -230,6 +232,8 @@ mod systems {
|
||||||
hit.point1 + total,
|
hit.point1 + total,
|
||||||
Color::linear_rgb(1., 1., 0.2),
|
Color::linear_rgb(1., 1., 0.2),
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
xform.translation = config.attach + (caster.direction.as_vec3() * config.rest_dist);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -274,6 +278,9 @@ impl Plugin for CyberPhysicsPlugin {
|
||||||
.register_type::<CyberLean>()
|
.register_type::<CyberLean>()
|
||||||
.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default()))
|
.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default()))
|
||||||
.insert_resource(SubstepCount(12))
|
.insert_resource(SubstepCount(12))
|
||||||
|
.add_systems(Startup, |mut gravity: ResMut<Gravity>| {
|
||||||
|
gravity.0 *= 0.02;
|
||||||
|
})
|
||||||
.add_systems(
|
.add_systems(
|
||||||
FixedUpdate,
|
FixedUpdate,
|
||||||
(calculate_lean, apply_lean, suspension).chain(),
|
(calculate_lean, apply_lean, suspension).chain(),
|
||||||
|
|
Loading…
Reference in a new issue