Skip to content

Instantly share code, notes, and snippets.

@lotus128
Created November 23, 2024 22:13
Show Gist options
  • Select an option

  • Save lotus128/66f4eaf7df0b9cc1bbd8a3bbe03c3fd3 to your computer and use it in GitHub Desktop.

Select an option

Save lotus128/66f4eaf7df0b9cc1bbd8a3bbe03c3fd3 to your computer and use it in GitHub Desktop.
bevy_rapier3d character controller heel surface
pub fn determine_next_heel_stage_surface(
rapier_context: &Res<RapierContext>,
root_position: Vec3,
root_rotation: Quat,
cast_origin_forward: f32,
cast_origin_down: f32,
cast_distance: f32,
cast_max_angle: f32,
) -> Option<StageSurfaceData> {
let cast_direction = root_rotation * -Dir3::Z;
let cast_origin = root_position
+ root_rotation
* Vec3 {
x: 0.0,
y: -cast_origin_down,
z: cast_origin_forward,
};
let cast_max_distance = cast_distance;
let query_filter = QueryFilter::default();
let cast_hit_option = rapier_context.cast_ray_and_get_normal(
cast_origin,
*cast_direction,
cast_max_distance,
true,
query_filter,
);
let Some((cast_hit_e, cast_hit)) = cast_hit_option else {
return None;
};
if cast_hit.time_of_impact == 0.0 {
return None;
}
let Ok(cast_hit_normal) = Dir3::new(cast_hit.normal) else {
return None;
};
let cast_hit_angle = Vec3::angle_between(*cast_direction, -*cast_hit_normal);
if cast_hit_angle > cast_max_angle {
return None;
}
let cast_hit_translation =
cast_origin + cast_direction * cast_hit.time_of_impact;
return Some(StageSurfaceData {
entity: cast_hit_e,
normal: cast_hit_normal,
translation: cast_hit_translation,
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment