Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,9 @@
"physics_rapier_instancing",
"physics_rapier_joints",
"physics_rapier_character_controller",
"physics_rapier_vehicle_controller"
"physics_rapier_vehicle_controller",
"physics_rapier_terrain"

],
"misc": [
"misc_animation_groups",
Expand Down
42 changes: 39 additions & 3 deletions examples/jsm/physics/RapierPhysics.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ function getShape( geometry ) {
} else if ( geometry.type === 'CylinderGeometry' ) {

const radius = parameters.radiusBottom !== undefined ? parameters.radiusBottom : 0.5;
const length = parameters.length !== undefined ? parameters.length : 0.5;
const length = parameters.height !== undefined ? parameters.height : 0.5;
Copy link
Collaborator

@Mugen87 Mugen87 Apr 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While testing I have notice the collisions for the cylinder meshes were incorrect. Turns out wrong parameters were evaluated.


return RAPIER.ColliderDesc.cylinder( length / 2, radius );

} else if ( geometry.type === 'CapsuleGeometry' ) {

const radius = parameters.radius !== undefined ? parameters.radius : 0.5;
const length = parameters.length !== undefined ? parameters.length : 0.5;
const length = parameters.height !== undefined ? parameters.height : 0.5;

return RAPIER.ColliderDesc.capsule( length / 2, radius );

Expand Down Expand Up @@ -209,6 +209,24 @@ async function RapierPhysics() {

}

function addHeightfield( mesh, width, depth, heights, scale ) {

const shape = RAPIER.ColliderDesc.heightfield( width, depth, heights, scale );

const bodyDesc = RAPIER.RigidBodyDesc.fixed();
bodyDesc.setTranslation( mesh.position.x, mesh.position.y, mesh.position.z );
bodyDesc.setRotation( mesh.quaternion );

const body = world.createRigidBody( bodyDesc );
world.createCollider( shape, body );

if ( ! mesh.userData.physics ) mesh.userData.physics = {};
mesh.userData.physics.body = body;

return body;

}

//

const clock = new Clock();
Expand Down Expand Up @@ -309,7 +327,25 @@ async function RapierPhysics() {
* @param {Vector3} velocity - The new velocity.
* @param {number} [index=0] - If the mesh is instanced, the index represents the instanced ID.
*/
setMeshVelocity: setMeshVelocity
setMeshVelocity: setMeshVelocity,

/**
* Adds a heightfield terrain to the physics simulation.
*
* @method
* @name RapierPhysics#addHeightfield
* @param {Mesh} mesh - The Three.js mesh representing the terrain.
* @param {number} width - The number of vertices along the width (x-axis) of the heightfield.
* @param {number} depth - The number of vertices along the depth (z-axis) of the heightfield.
* @param {Float32Array} heights - Array of height values for each vertex in the heightfield.
* @param {Object} scale - Scale factors for the heightfield dimensions.
* @param {number} scale.x - Scale factor for width.
* @param {number} scale.y - Scale factor for height.
* @param {number} scale.z - Scale factor for depth.
* @returns {RigidBody} The created Rapier rigid body for the heightfield.
*/
addHeightfield: addHeightfield

};

}
Expand Down
Loading