Skip to content

Commit

Permalink
Initial update to Bevy 12.1 (#27)
Browse files Browse the repository at this point in the history
* updated to bevy 12.1 and bevy_rapier3d 23.0

* forgot to add toml

* fixed toi details unwrap

* fixed example
  • Loading branch information
Zrocket committed Dec 23, 2023
1 parent db8aec8 commit 42a3fb0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ readme = "README.md"
description = "Bevy plugin that adds a Source engine inspired FPS movement controller"

[dependencies]
bevy = "0.11.3"
bevy_rapier3d = "0.22.0"
bevy = "0.12.1"
bevy_rapier3d = "0.23.0"

[[example]]
name = "minimal"
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn scene_colliders(
..default()
});
for node in &gltf.nodes {
let node = gltf_node_assets.get(&node).unwrap();
let node = gltf_node_assets.get(node).unwrap();
if let Some(gltf_mesh) = node.mesh.clone() {
let gltf_mesh = gltf_mesh_assets.get(&gltf_mesh).unwrap();
for mesh_primitive in &gltf_mesh.primitives {
Expand Down
20 changes: 16 additions & 4 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ pub fn fps_controller_move(
-Vec3::Y,
&cast_capsule,
0.125,
true,
filter,
);

Expand All @@ -270,8 +271,8 @@ pub fn fps_controller_move(
};
wish_speed = f32::min(wish_speed, max_speed);

if let Some((_, toi)) = ground_cast {
let has_traction = Vec3::dot(toi.normal1, Vec3::Y) > controller.traction_normal_cutoff;
if let Some((toi, toi_details)) = toi_details_unwrap(ground_cast) {
let has_traction = Vec3::dot(toi_details.normal1, Vec3::Y) > controller.traction_normal_cutoff;

// Only apply friction after at least one tick, allows b-hopping without losing speed
if controller.ground_tick >= 1 && has_traction {
Expand Down Expand Up @@ -304,7 +305,7 @@ pub fn fps_controller_move(

if has_traction {
let linvel = velocity.linvel;
velocity.linvel -= Vec3::dot(linvel, toi.normal1) * toi.normal1;
velocity.linvel -= Vec3::dot(linvel, toi_details.normal1) * toi_details.normal1;

if input.jump {
velocity.linvel.y = controller.jump_speed;
Expand Down Expand Up @@ -391,6 +392,15 @@ pub fn fps_controller_move(
}
}

fn toi_details_unwrap(ground_cast: Option<(Entity, Toi)>) -> Option<(Toi, ToiDetails)> {
if let Some((_, toi)) = ground_cast {
if let Some(details) = toi.details {
return Some((toi, details));
}
}
None
}

fn overhang_component(entity: Entity, transform: &Transform, physics_context: &RapierContext, velocity: Vec3, dt: f32) -> Option<Vec3> {
// Cast a segment (zero radius on capsule) from our next position back towards us
// If there is a ledge in front of us we will hit the edge of it
Expand All @@ -403,9 +413,11 @@ fn overhang_component(entity: Entity, transform: &Transform, physics_context: &R
-velocity,
&cast_capsule,
0.5,
true,
filter,
);
if let Some((_, toi)) = cast {
let toi_details = toi.details.unwrap();
let cast = physics_context.cast_ray(
future_position + Vec3::Y * 0.125, -Vec3::Y,
0.375,
Expand All @@ -414,7 +426,7 @@ fn overhang_component(entity: Entity, transform: &Transform, physics_context: &R
);
// Make sure that this is actually a ledge, e.g. there is no ground in front of us
if cast.is_none() {
let normal = -toi.normal1;
let normal = -toi_details.normal1;
let alignment = Vec3::dot(velocity, normal);
return Some(alignment * normal);
}
Expand Down

0 comments on commit 42a3fb0

Please sign in to comment.