Certainly! Below is a complete HTML file that uses Babylon.js to create a simple 3D ragdoll made from basic shapes. The ragdoll has physics applied to it using Ammo.js, and includes controls to reset the ragdoll and apply random forces for testing purposes.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Babylon.js Ragdoll Example</title>
<!-- Include Babylon.js CDN -->
<script src="https://cdn.babylonjs.com/babylon.js"></script>
<!-- Include the Ammo.js physics engine -->
<script src="https://cdn.babylonjs.com/ammo.js"></script>
<!-- Include Babylon.js physics plugin -->
<script src="https://cdn.babylonjs.com/babylonjs.loaders.min.js"></script>
<style>
html, body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
touch-action: none;
background-color: #202020;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
#controls {
position: absolute;
top: 10px;
left: 10px;
color: white;
z-index: 1;
}
#controls button {
margin-right: 10px;
padding: 10px 15px;
font-size: 14px;
}
</style>
</head>
<body>
<div id="controls">
<button id="resetButton">Reset Ragdoll</button>
<button id="applyForceButton">Apply Random Force</button>
</div>
<canvas id="renderCanvas"></canvas>
<script>
// Get the canvas element
var canvas = document.getElementById("renderCanvas");
// Generate the BABYLON 3D engine
var engine = new BABYLON.Engine(canvas, true);
var scene;
var ragdollParts;
// Create the scene
var createScene = async function () {
scene = new BABYLON.Scene(engine);
// Enable physics
var ammoJSPlugin = new BABYLON.AmmoJSPlugin(true);
await Ammo(); // Ensure Ammo.js is loaded
scene.enablePhysics(new BABYLON.Vector3(0, -9.81, 0), ammoJSPlugin);
// Create camera
var camera = new BABYLON.ArcRotateCamera("camera", -Math.PI / 2, Math.PI / 3, 20, new BABYLON.Vector3(0, 1, 0), scene);
camera.attachControl(canvas, true);
// Create light
var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
light.intensity = 0.7;
// Create ground
var groundMaterial = new BABYLON.StandardMaterial("groundMaterial", scene);
groundMaterial.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
var ground = BABYLON.MeshBuilder.CreateGround("ground", { width: 50, height: 50 }, scene);
ground.position.y = -1;
ground.material = groundMaterial;
ground.physicsImpostor = new BABYLON.PhysicsImpostor(
ground,
BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 0, restitution: 0.8, friction: 1 },
scene
);
// Build the ragdoll
ragdollParts = buildRagdoll(scene);
return scene;
};
// Function to build the ragdoll
function buildRagdoll(scene) {
// Create material for the ragdoll
var material = new BABYLON.StandardMaterial("ragdollMaterial", scene);
material.diffuseColor = new BABYLON.Color3(1, 0.8, 0.7);
var parts = {}; // Object to hold all body parts
// Torso
parts.torso = BABYLON.MeshBuilder.CreateBox("torso", { height: 2, width: 1, depth: 0.5 }, scene);
parts.torso.position.y = 5;
parts.torso.material = material;
parts.torso.physicsImpostor = new BABYLON.PhysicsImpostor(
parts.torso,
BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 10, friction: 0.5, restitution: 0 },
scene
);
// Head
parts.head = BABYLON.MeshBuilder.CreateSphere("head", { diameter: 0.8 }, scene);
parts.head.position.y = parts.torso.position.y + 1.4;
parts.head.material = material;
parts.head.physicsImpostor = new BABYLON.PhysicsImpostor(
parts.head,
BABYLON.PhysicsImpostor.SphereImpostor,
{ mass: 2, friction: 0.5, restitution: 0 },
scene
);
// Left Upper Arm
parts.leftUpperArm = BABYLON.MeshBuilder.CreateBox("leftUpperArm", { height: 1.2, width: 0.3, depth: 0.3 }, scene);
parts.leftUpperArm.position = parts.torso.position.add(new BABYLON.Vector3(-0.75, 0.5, 0));
parts.leftUpperArm.material = material;
parts.leftUpperArm.rotation.z = Math.PI / 2;
parts.leftUpperArm.physicsImpostor = new BABYLON.PhysicsImpostor(
parts.leftUpperArm,
BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 2, friction: 0.5, restitution: 0 },
scene
);
// Left Lower Arm
parts.leftLowerArm = BABYLON.MeshBuilder.CreateBox("leftLowerArm", { height: 1, width: 0.25, depth: 0.25 }, scene);
parts.leftLowerArm.position = parts.leftUpperArm.position.add(new BABYLON.Vector3(-1, 0, 0));
parts.leftLowerArm.material = material;
parts.leftLowerArm.rotation.z = Math.PI / 2;
parts.leftLowerArm.physicsImpostor = new BABYLON.PhysicsImpostor(
parts.leftLowerArm,
BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 1.5, friction: 0.5, restitution: 0 },
scene
);
// Right Upper Arm
parts.rightUpperArm = parts.leftUpperArm.clone("rightUpperArm");
parts.rightUpperArm.position.x = -parts.leftUpperArm.position.x;
parts.rightUpperArm.physicsImpostor = new BABYLON.PhysicsImpostor(
parts.rightUpperArm,
BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 2, friction: 0.5, restitution: 0 },
scene
);
// Right Lower Arm
parts.rightLowerArm = parts.leftLowerArm.clone("rightLowerArm");
parts.rightLowerArm.position.x = -parts.leftLowerArm.position.x;
parts.rightLowerArm.physicsImpostor = new BABYLON.PhysicsImpostor(
parts.rightLowerArm,
BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 1.5, friction: 0.5, restitution: 0 },
scene
);
// Left Upper Leg
parts.leftUpperLeg = BABYLON.MeshBuilder.CreateBox("leftUpperLeg", { height: 1.5, width: 0.4, depth: 0.4 }, scene);
parts.leftUpperLeg.position = parts.torso.position.add(new BABYLON.Vector3(-0.3, -1.75, 0));
parts.leftUpperLeg.material = material;
parts.leftUpperLeg.physicsImpostor = new BABYLON.PhysicsImpostor(
parts.leftUpperLeg,
BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 4, friction: 0.5, restitution: 0 },
scene
);
// Left Lower Leg
parts.leftLowerLeg = BABYLON.MeshBuilder.CreateBox("leftLowerLeg", { height: 1.4, width: 0.35, depth: 0.35 }, scene);
parts.leftLowerLeg.position = parts.leftUpperLeg.position.add(new BABYLON.Vector3(0, -1.2, 0));
parts.leftLowerLeg.material = material;
parts.leftLowerLeg.physicsImpostor = new BABYLON.PhysicsImpostor(
parts.leftLowerLeg,
BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 3, friction: 0.5, restitution: 0 },
scene
);
// Right Upper Leg
parts.rightUpperLeg = parts.leftUpperLeg.clone("rightUpperLeg");
parts.rightUpperLeg.position.x = -parts.leftUpperLeg.position.x;
parts.rightUpperLeg.physicsImpostor = new BABYLON.PhysicsImpostor(
parts.rightUpperLeg,
BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 4, friction: 0.5, restitution: 0 },
scene
);
// Right Lower Leg
parts.rightLowerLeg = parts.leftLowerLeg.clone("rightLowerLeg");
parts.rightLowerLeg.position.x = -parts.leftLowerLeg.position.x;
parts.rightLowerLeg.physicsImpostor = new BABYLON.PhysicsImpostor(
parts.rightLowerLeg,
BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 3, friction: 0.5, restitution: 0 },
scene
);
// Connect the body parts with joints
connectJoints(scene, parts.head, parts.torso, new BABYLON.Vector3(0, -0.4, 0), new BABYLON.Vector3(0, 1, 0)); // Neck
connectJoints(scene, parts.leftUpperArm, parts.torso, new BABYLON.Vector3(0, -0.6, 0), new BABYLON.Vector3(-0.5, 0.5, 0)); // Left shoulder
connectJoints(scene, parts.leftLowerArm, parts.leftUpperArm, new BABYLON.Vector3(0, -0.5, 0), new BABYLON.Vector3(0, 0.6, 0)); // Left elbow
connectJoints(scene, parts.rightUpperArm, parts.torso, new BABYLON.Vector3(0, -0.6, 0), new BABYLON.Vector3(0.5, 0.5, 0)); // Right shoulder
connectJoints(scene, parts.rightLowerArm, parts.rightUpperArm, new BABYLON.Vector3(0, -0.5, 0), new BABYLON.Vector3(0, 0.6, 0)); // Right elbow
connectJoints(scene, parts.leftUpperLeg, parts.torso, new BABYLON.Vector3(0, 0.75, 0), new BABYLON.Vector3(-0.3, -1, 0)); // Left hip
connectJoints(scene, parts.leftLowerLeg, parts.leftUpperLeg, new BABYLON.Vector3(0, 0.7, 0), new BABYLON.Vector3(0, -0.7, 0)); // Left knee
connectJoints(scene, parts.rightUpperLeg, parts.torso, new BABYLON.Vector3(0, 0.75, 0), new BABYLON.Vector3(0.3, -1, 0)); // Right hip
connectJoints(scene, parts.rightLowerLeg, parts.rightUpperLeg, new BABYLON.Vector3(0, 0.7, 0), new BABYLON.Vector3(0, -0.7, 0)); // Right knee
return parts;
}
// Function to connect two body parts with a joint
function connectJoints(scene, partA, partB, pivotA, pivotB) {
var joint = new BABYLON.MotorEnabledJoint(BABYLON.PhysicsJoint.BallAndSocketJoint, {
mainPivot: pivotA,
connectedPivot: pivotB,
mainAxis: new BABYLON.Vector3(0, 1, 0),
connectedAxis: new BABYLON.Vector3(0, 1, 0)
});
partA.physicsImpostor.addJoint(partB.physicsImpostor, joint);
}
// Initialize the scene
createScene().then(function(){
engine.runRenderLoop(function(){
scene.render();
});
});
// Resize the engine on window resize
window.addEventListener("resize", function () {
engine.resize();
});
// Event listener for reset button
var resetButton = document.getElementById("resetButton");
resetButton.onclick = function(){
// Dispose existing ragdoll parts
for(var key in ragdollParts){
ragdollParts[key].dispose();
}
// Rebuild the ragdoll
ragdollParts = buildRagdoll(scene);
};
// Event listener for apply force button
var applyForceButton = document.getElementById("applyForceButton");
applyForceButton.onclick = function(){
// Apply random force to the torso
var forceDirection = new BABYLON.Vector3(
Math.random() - 0.5,
Math.random(),
Math.random() - 0.5
).normalize().scale(1000);
ragdollParts.torso.physicsImpostor.applyImpulse(forceDirection, ragdollParts.torso.getAbsolutePosition());
};
</script>
</body>
</html>
Explanation:
-
Babylon.js Setup: The script includes Babylon.js and Ammo.js via CDN links in the <head> section. Styles are added to ensure the canvas and controls display correctly.
-
Scene Creation: The createScene function initializes the scene, camera, light, and ground. Physics is enabled using Ammo.js, and gravity is set to simulate Earth's gravity.
-
Ragdoll Construction: The buildRagdoll function creates the ragdoll parts using simple shapes (boxes and spheres). Each part is assigned a physics impostor with appropriate mass and physical properties.
-
Connecting Joints: The connectJoints function connects body parts using BallAndSocketJoint, which allows rotation in all directions, similar to human joints.
-
Controls for Testing:
- Reset Button: Rebuilds the ragdoll by disposing of existing parts and reconstructing them.
- Apply Random Force Button: Applies a random impulse to the torso to simulate an external force and observe the ragdoll's reaction.
-
Event Listeners: The script attaches event listeners to the buttons to handle user interactions.
-
Render Loop: The engine's render loop continuously renders the scene, allowing for real-time interaction and physics simulation.
Usage:
- Reset Ragdoll: Click this button to reset the ragdoll to its initial position.
- Apply Random Force: Click this button to apply a random force to the ragdoll's torso, causing it to react and collapse under physics.
Note:
This implementation provides a simplified ragdoll using basic physics joints. While it may not fully replicate the advanced behaviors of Euphoria's physics engine, it demonstrates how to create and manipulate a ragdoll using Babylon.js and Ammo.js.
Feel free to modify the code to add more features or adjust the physics parameters to achieve different behaviors.