use a raycast instead of shapecast

This commit is contained in:
Joe Ardent 2025-03-29 16:30:06 -07:00
parent 3e11c6439f
commit ffbe45cf8e
2 changed files with 18 additions and 18 deletions

View file

@ -187,7 +187,7 @@ fn wheel_caster(
rest_dist: Scalar,
wheel: CyberWheel,
) {
let caster = ShapeCaster::new(collider, origin, Quat::IDENTITY, direction)
let caster = RayCaster::new(origin, direction)
.with_max_distance(rest_dist)
.with_max_hits(1)
.with_query_filter(SpatialQueryFilter::from_excluded_entities([parent]));

View file

@ -174,7 +174,7 @@ mod systems {
),
Without<CyberBikeBody>,
>,
caster_query: Query<(&ShapeCaster, &ShapeHits, &CyberWheel)>,
caster_query: Query<(&RayCaster, &RayHits, &CyberWheel)>,
time: Res<Time>,
input: Res<InputState>,
mut gizmos: Gizmos,
@ -188,11 +188,11 @@ mod systems {
let yaw = input.yaw * max_yaw;
let dt = time.delta().as_secs_f32();
let mut front_caster = &ShapeCaster::default();
let mut rear_caster = &ShapeCaster::default();
let mut front_caster = &RayCaster::default();
let mut rear_caster = &RayCaster::default();
let mut front_hits = &ShapeHits::default();
let mut rear_hits = &ShapeHits::default();
let mut front_hits = &RayHits::default();
let mut rear_hits = &RayHits::default();
for (caster, hits, wheel) in caster_query.iter() {
match wheel {
@ -225,12 +225,16 @@ mod systems {
let mag = config.konstant * displacement - config.damping * damper_vel;
let mag = mag.max(0.0);
//let fdir = -caster.global_direction().as_vec3();
let fdir = hit.normal1;
let fdir = hit.normal;
let force = fdir * mag;
//dbg!(fdir, force);
let p = hit.point1 + (hit.normal1 * config.radius);
gizmos.arrow(p, p + force, Color::linear_rgb(1., 0.5, 0.2));
let hit_point = caster.global_origin() + caster.global_direction() * dist;
gizmos.arrow(
hit_point,
hit_point + force,
Color::linear_rgb(1., 0.5, 0.2),
);
bike_forces.apply_force_at_point(
force,
@ -240,7 +244,7 @@ mod systems {
//let vel = (global_xform.translation() - state.ppos) / dt;
state.ppos = global_xform.translation();
let normal = hit.normal1;
let normal = hit.normal;
let steering = match wheel {
CyberWheel::Front => normal.cross(*bike_xform.back()) * yaw,
CyberWheel::Rear => Vec3::ZERO,
@ -248,12 +252,8 @@ mod systems {
};
let thrust = normal.cross(*bike_xform.right()) * thrust;
let total = (thrust + steering) * dt;
bike_forces.apply_force_at_point(total, hit.point1, bike_xform.translation);
gizmos.arrow(
hit.point1,
hit.point1 + total,
Color::linear_rgb(1., 1., 0.2),
);
bike_forces.apply_force_at_point(total, hit_point, bike_xform.translation);
gizmos.arrow(hit_point, hit_point + total, Color::linear_rgb(1., 1., 0.2));
} else {
xform.translation = config.attach + (caster.direction.as_vec3() * config.rest_dist);
}