feat: add a vertical wall to the 2D and 3D character controller examples

This commit is contained in:
Sébastien Crozet
2024-05-04 17:51:35 +02:00
committed by Sébastien Crozet
parent 7565e5e4ef
commit fcd4e71b43
2 changed files with 63 additions and 28 deletions

View File

@@ -27,7 +27,7 @@ pub fn init_world(testbed: &mut Testbed) {
*/ */
let rigid_body = RigidBodyBuilder::kinematic_position_based().translation(vector![-3.0, 5.0]); let rigid_body = RigidBodyBuilder::kinematic_position_based().translation(vector![-3.0, 5.0]);
let character_handle = bodies.insert(rigid_body); let character_handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(0.15, 0.3); let collider = ColliderBuilder::capsule_y(0.3, 0.15);
colliders.insert_with_parent(collider, character_handle, &mut bodies); colliders.insert_with_parent(collider, character_handle, &mut bodies);
/* /*
@@ -94,14 +94,18 @@ pub fn init_world(testbed: &mut Testbed) {
*/ */
let wall_angle = PI / 2.; let wall_angle = PI / 2.;
let wall_size = 2.0; let wall_size = 2.0;
let collider = ColliderBuilder::cuboid(wall_size, ground_height) let wall_pos = vector![
.translation(vector![
ground_size + slope_size * 2.0 + impossible_slope_size + 0.35, ground_size + slope_size * 2.0 + impossible_slope_size + 0.35,
-ground_height + 2.5 * 2.3 -ground_height + 2.5 * 2.3
]) ];
let collider = ColliderBuilder::cuboid(wall_size, ground_height)
.translation(wall_pos)
.rotation(wall_angle); .rotation(wall_angle);
colliders.insert(collider); colliders.insert(collider);
let collider = ColliderBuilder::cuboid(wall_size, ground_height).translation(wall_pos);
colliders.insert(collider);
/* /*
* Create a moving platform. * Create a moving platform.
*/ */

View File

@@ -13,21 +13,37 @@ pub fn init_world(testbed: &mut Testbed) {
/* /*
* Ground * Ground
*/ */
let scale = 1.0; // Set to a larger value to check if it still works with larger units.
let ground_size = 5.0; let ground_size = 5.0;
let ground_height = 0.1; let ground_height = 0.1;
let rigid_body = RigidBodyBuilder::fixed().translation(vector![0.0, -ground_height, 0.0]); let rigid_body =
RigidBodyBuilder::fixed().translation(vector![0.0, -ground_height, 0.0] * scale);
let floor_handle = bodies.insert(rigid_body); let floor_handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(ground_size, ground_height, ground_size); let collider = ColliderBuilder::cuboid(
ground_size * scale,
ground_height * scale,
ground_size * scale,
);
colliders.insert_with_parent(collider, floor_handle, &mut bodies);
let rigid_body =
RigidBodyBuilder::fixed().translation(vector![0.0, -ground_height, -ground_size] * scale); //.rotation(vector![-0.1, 0.0, 0.0]);
let floor_handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(
ground_size * scale,
ground_size * scale,
ground_height * scale,
);
colliders.insert_with_parent(collider, floor_handle, &mut bodies); colliders.insert_with_parent(collider, floor_handle, &mut bodies);
/* /*
* Character we will control manually. * Character we will control manually.
*/ */
let rigid_body = let rigid_body =
RigidBodyBuilder::kinematic_position_based().translation(vector![-3.0, 5.0, 0.0]); RigidBodyBuilder::kinematic_position_based().translation(vector![-3.0, 5.0, 0.0] * scale);
let character_handle = bodies.insert(rigid_body); let character_handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::capsule_y(0.3, 0.15); // 0.15, 0.3, 0.15); let collider = ColliderBuilder::capsule_y(0.3 * scale, 0.15 * scale); // 0.15, 0.3, 0.15);
colliders.insert_with_parent(collider, character_handle, &mut bodies); colliders.insert_with_parent(collider, character_handle, &mut bodies);
/* /*
@@ -47,9 +63,9 @@ pub fn init_world(testbed: &mut Testbed) {
let y = j as f32 * shift + centery; let y = j as f32 * shift + centery;
let z = k as f32 * shift + centerx; let z = k as f32 * shift + centerx;
let rigid_body = RigidBodyBuilder::dynamic().translation(vector![x, y, z]); let rigid_body = RigidBodyBuilder::dynamic().translation(vector![x, y, z] * scale);
let handle = bodies.insert(rigid_body); let handle = bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(rad, rad, rad); let collider = ColliderBuilder::cuboid(rad * scale, rad * scale, rad * scale);
colliders.insert_with_parent(collider, handle, &mut bodies); colliders.insert_with_parent(collider, handle, &mut bodies);
} }
} }
@@ -64,8 +80,12 @@ pub fn init_world(testbed: &mut Testbed) {
let x = i as f32 * stair_width / 2.0; let x = i as f32 * stair_width / 2.0;
let y = i as f32 * stair_height * 1.5 + 3.0; let y = i as f32 * stair_height * 1.5 + 3.0;
let collider = ColliderBuilder::cuboid(stair_width / 2.0, stair_height / 2.0, stair_width) let collider = ColliderBuilder::cuboid(
.translation(vector![x, y, 0.0]); stair_width / 2.0 * scale,
stair_height / 2.0 * scale,
stair_width * scale,
)
.translation(vector![x * scale, y * scale, 0.0]);
colliders.insert(collider); colliders.insert(collider);
} }
@@ -74,8 +94,12 @@ pub fn init_world(testbed: &mut Testbed) {
*/ */
let slope_angle = 0.2; let slope_angle = 0.2;
let slope_size = 2.0; let slope_size = 2.0;
let collider = ColliderBuilder::cuboid(slope_size, ground_height, slope_size) let collider = ColliderBuilder::cuboid(
.translation(vector![ground_size + slope_size, -ground_height + 0.4, 0.0]) slope_size * scale,
ground_height * scale,
slope_size * scale,
)
.translation(vector![ground_size + slope_size, -ground_height + 0.4, 0.0] * scale)
.rotation(Vector::z() * slope_angle); .rotation(Vector::z() * slope_angle);
colliders.insert(collider); colliders.insert(collider);
@@ -84,22 +108,29 @@ pub fn init_world(testbed: &mut Testbed) {
*/ */
let impossible_slope_angle = 0.9; let impossible_slope_angle = 0.9;
let impossible_slope_size = 2.0; let impossible_slope_size = 2.0;
let collider = ColliderBuilder::cuboid(slope_size, ground_height, ground_size) let collider = ColliderBuilder::cuboid(
.translation(vector![ slope_size * scale,
ground_height * scale,
ground_size * scale,
)
.translation(
vector![
ground_size + slope_size * 2.0 + impossible_slope_size - 0.9, ground_size + slope_size * 2.0 + impossible_slope_size - 0.9,
-ground_height + 2.3, -ground_height + 2.3,
0.0 0.0
]) ] * scale,
)
.rotation(Vector::z() * impossible_slope_angle); .rotation(Vector::z() * impossible_slope_angle);
colliders.insert(collider); colliders.insert(collider);
/* /*
* Create a moving platform. * Create a moving platform.
*/ */
let body = RigidBodyBuilder::kinematic_velocity_based().translation(vector![-8.0, 1.5, 0.0]); let body =
RigidBodyBuilder::kinematic_velocity_based().translation(vector![-8.0, 1.5, 0.0] * scale);
// .rotation(-0.3); // .rotation(-0.3);
let platform_handle = bodies.insert(body); let platform_handle = bodies.insert(body);
let collider = ColliderBuilder::cuboid(2.0, ground_height, 2.0); let collider = ColliderBuilder::cuboid(2.0 * scale, ground_height * scale, 2.0 * scale);
colliders.insert_with_parent(collider, platform_handle, &mut bodies); colliders.insert_with_parent(collider, platform_handle, &mut bodies);
/* /*
@@ -113,18 +144,18 @@ pub fn init_world(testbed: &mut Testbed) {
+ (j as f32 * ground_size.z / (nsubdivs as f32) / 2.0).cos() + (j as f32 * ground_size.z / (nsubdivs as f32) / 2.0).cos()
}); });
let collider = let collider = ColliderBuilder::heightfield(heights, ground_size * scale)
ColliderBuilder::heightfield(heights, ground_size).translation(vector![-8.0, 5.0, 0.0]); .translation(vector![-8.0, 5.0, 0.0] * scale);
colliders.insert(collider); colliders.insert(collider);
/* /*
* A tilting dynamic body with a limited joint. * A tilting dynamic body with a limited joint.
*/ */
let ground = RigidBodyBuilder::fixed().translation(vector![0.0, 5.0, 0.0]); let ground = RigidBodyBuilder::fixed().translation(vector![0.0, 5.0, 0.0] * scale);
let ground_handle = bodies.insert(ground); let ground_handle = bodies.insert(ground);
let body = RigidBodyBuilder::dynamic().translation(vector![0.0, 5.0, 0.0]); let body = RigidBodyBuilder::dynamic().translation(vector![0.0, 5.0, 0.0] * scale);
let handle = bodies.insert(body); let handle = bodies.insert(body);
let collider = ColliderBuilder::cuboid(1.0, 0.1, 2.0); let collider = ColliderBuilder::cuboid(1.0 * scale, 0.1 * scale, 2.0 * scale);
colliders.insert_with_parent(collider, handle, &mut bodies); colliders.insert_with_parent(collider, handle, &mut bodies);
let joint = RevoluteJointBuilder::new(Vector::z_axis()).limits([-0.3, 0.3]); let joint = RevoluteJointBuilder::new(Vector::z_axis()).limits([-0.3, 0.3]);
impulse_joints.insert(ground_handle, handle, joint, true); impulse_joints.insert(ground_handle, handle, joint, true);
@@ -137,7 +168,7 @@ pub fn init_world(testbed: &mut Testbed) {
(run_state.time * 2.0).sin() * 2.0, (run_state.time * 2.0).sin() * 2.0,
(run_state.time * 5.0).sin() * 1.5, (run_state.time * 5.0).sin() * 1.5,
0.0 0.0
]; ] * scale;
// let angvel = run_state.time.sin() * 0.5; // let angvel = run_state.time.sin() * 0.5;
// Update the velocity-based kinematic body by setting its velocity. // Update the velocity-based kinematic body by setting its velocity.