|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>JoltPhysics.js demo</title> |
| 5 | + <meta charset="utf-8"> |
| 6 | + <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> |
| 7 | + <link rel="stylesheet" type="text/css" href="style.css"> |
| 8 | + </head> |
| 9 | + <body> |
| 10 | + <div id="container">Loading...</div> |
| 11 | + <div id="info">JoltPhysics.js multi threaded conveyor belt demo<br /> |
| 12 | + This demo shows how to use the contact listener to modify object angular and linear velocity |
| 13 | + </div> |
| 14 | + |
| 15 | + <script src="js/three/three.min.js"></script> |
| 16 | + <script src="js/three/OrbitControls.js"></script> |
| 17 | + <script src="js/three/WebGL.js"></script> |
| 18 | + <script src="js/three/stats.min.js"></script> |
| 19 | + <script src="js/example.js"></script> |
| 20 | + |
| 21 | + <script id="workerScript" type="module"> |
| 22 | + import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.161.0/three.module.js'; |
| 23 | + |
| 24 | + const wrapQuat = (q) => new THREE.Quaternion(q.GetX(), q.GetY(), q.GetZ(), q.GetW()); |
| 25 | + const wrapVec3 = (v) => new THREE.Vector3(v.GetX(), v.GetY(), v.GetZ()); |
| 26 | + const DegreesToRadians = (deg) => deg * (Math.PI / 180.0); |
| 27 | + const onWorker = globalThis.onWorker = async (Jolt, args) => { |
| 28 | + const { contactListenerPtr, linearBelts, angularBelt } = args; |
| 29 | + const contactListener = Jolt.wrapPointer(contactListenerPtr, Jolt.ContactListenerJS); |
| 30 | + |
| 31 | + contactListener.OnContactAdded = (body1, body2, manifold, settings) => { |
| 32 | + body1 = Jolt.wrapPointer(body1, Jolt.Body); |
| 33 | + body2 = Jolt.wrapPointer(body2, Jolt.Body); |
| 34 | + manifold = Jolt.wrapPointer(manifold, Jolt.ContactManifold); |
| 35 | + settings = Jolt.wrapPointer(settings, Jolt.ContactSettings); |
| 36 | + |
| 37 | + const body1ID = body1.GetID().GetIndexAndSequenceNumber(); |
| 38 | + const body2ID = body2.GetID().GetIndexAndSequenceNumber(); |
| 39 | + const rotation1 = wrapQuat(body1.GetRotation()); |
| 40 | + const rotation2 = wrapQuat(body2.GetRotation()); |
| 41 | + |
| 42 | + const body1LinearBelt = linearBelts.find(belt => belt == body1ID) |
| 43 | + const body2LinearBelt = linearBelts.find(belt => belt == body2ID) |
| 44 | + if (body1LinearBelt || body2LinearBelt) { |
| 45 | + // Determine the world space surface velocity of both bodies |
| 46 | + const cLocalSpaceVelocity = new THREE.Vector3(0, 0, -10.0); |
| 47 | + const body1LinearSurfaceVelocity = body1LinearBelt ? cLocalSpaceVelocity.applyQuaternion(rotation1) : new THREE.Vector3(0, 0, 0); |
| 48 | + const body2LinearSurfaceVelocity = body2LinearBelt ? cLocalSpaceVelocity.applyQuaternion(rotation2) : new THREE.Vector3(0, 0, 0); |
| 49 | + |
| 50 | + // Calculate the relative surface velocity |
| 51 | + const v = body2LinearSurfaceVelocity.sub(body1LinearSurfaceVelocity); |
| 52 | + settings.mRelativeLinearSurfaceVelocity.Set(v.x, v.y, v.z); |
| 53 | + } |
| 54 | + |
| 55 | + const angularBodyId = angularBelt[0]; |
| 56 | + const body1Angular = body1ID == angularBodyId; |
| 57 | + const body2Angular = body2ID == angularBodyId; |
| 58 | + if (body1Angular || body2Angular) { |
| 59 | + // Determine the world space angular surface velocity of both bodies |
| 60 | + const cLocalSpaceAngularVelocity = new THREE.Vector3(0, DegreesToRadians(10.0), 0); |
| 61 | + const body1AngularSurfaceVelocity = body1Angular ? cLocalSpaceAngularVelocity.applyQuaternion(rotation1) : new THREE.Vector3(0, 0, 0); |
| 62 | + const body2AngularSurfaceVelocity = body2Angular ? cLocalSpaceAngularVelocity.applyQuaternion(rotation2) : new THREE.Vector3(0, 0, 0); |
| 63 | + |
| 64 | + // Note that the angular velocity is the angular velocity around body 1's center of mass, so we need to add the linear velocity of body 2's center of mass |
| 65 | + const COM1 = wrapVec3(body1.GetCenterOfMassPosition()); |
| 66 | + const COM2 = wrapVec3(body2.GetCenterOfMassPosition()); |
| 67 | + const body2LinearSurfaceVelocity = body2Angular ? |
| 68 | + body2AngularSurfaceVelocity.cross(COM1.clone().sub(COM2)) : new THREE.Vector3(0, 0, 0); |
| 69 | + |
| 70 | + // Calculate the relative angular surface velocity |
| 71 | + const rls = body2LinearSurfaceVelocity; |
| 72 | + settings.mRelativeLinearSurfaceVelocity.Set(rls.x, rls.y, rls.z); |
| 73 | + const ras = body2AngularSurfaceVelocity.sub(body1AngularSurfaceVelocity); |
| 74 | + settings.mRelativeAngularSurfaceVelocity.Set(ras.x, ras.y, ras.z); |
| 75 | + } |
| 76 | + |
| 77 | + }; |
| 78 | + contactListener.OnContactPersisted = (body1, body2, manifold, settings) => { |
| 79 | + // Same behavior as contact added |
| 80 | + contactListener.OnContactAdded(body1, body2, manifold, settings); |
| 81 | + }; |
| 82 | + contactListener.OnContactRemoved = (subShapePair) => { |
| 83 | + // Required for JSInterface to have this function exist |
| 84 | + }; |
| 85 | + contactListener.OnContactValidate = (body1, body2, baseOffset, collideShapeResult) => { |
| 86 | + // Required for JSInterface to have this function exist |
| 87 | + return Jolt.ValidateResult_AcceptAllContactsForThisBodyPair; |
| 88 | + }; |
| 89 | + } |
| 90 | + export default onWorker; |
| 91 | + </script> |
| 92 | + |
| 93 | + |
| 94 | + <script type="module"> |
| 95 | + // In case you haven't built the library yourself, replace URL with: https://www.unpkg.com/jolt-physics/dist/jolt-physics.multithread.wasm-compat.js |
| 96 | + import initJolt from './js/jolt-physics.multithread.wasm-compat.js'; |
| 97 | + |
| 98 | + initJolt().then(function (Jolt) { |
| 99 | + |
| 100 | + const contactListener = new Jolt.ContactListenerJS(); |
| 101 | + const workerUrl = URL.createObjectURL(new Blob([document.getElementById('workerScript').innerHTML], { type: 'text/javascript' })); |
| 102 | + const workerParams = { |
| 103 | + contactListenerPtr: Jolt.getPointer(contactListener), |
| 104 | + angularBelt: new Uint32Array(new SharedArrayBuffer(4)), |
| 105 | + linearBelts: new Uint32Array(new SharedArrayBuffer(20)) |
| 106 | + } |
| 107 | + Jolt.configureWorkerScripts(workerUrl, workerParams); |
| 108 | + |
| 109 | + // Initialize this example |
| 110 | + initExample(Jolt, null); |
| 111 | + |
| 112 | + camera.position.z += 60; |
| 113 | + camera.position.y += 20; |
| 114 | + camera.position.x += 50; |
| 115 | + |
| 116 | + // Create a basic floor |
| 117 | + createFloor(100); |
| 118 | + |
| 119 | + // Create conveyor belts |
| 120 | + const cBeltWidth = 10.0; |
| 121 | + const cBeltLength = 50.0; |
| 122 | + const linearBelts = []; |
| 123 | + for (let i = 0; i < 4; ++i) { |
| 124 | + const friction = 0.25 * (i + 1); |
| 125 | + const rot1 = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), 0.5 * Math.PI * i); |
| 126 | + const rot2 = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1, 0, 0), DegreesToRadians(1.0)); |
| 127 | + const rotation = rot1.clone().multiply(rot2); |
| 128 | + const position = new THREE.Vector3(cBeltLength, 6.0, cBeltWidth).applyQuaternion(rotation); |
| 129 | + const belt = createBox(unwrapRVec3(position), unwrapQuat(rotation), new Jolt.Vec3(cBeltWidth, 0.1, cBeltLength), Jolt.EMotionType_Static, LAYER_NON_MOVING); |
| 130 | + belt.SetFriction(friction); |
| 131 | + linearBelts.push(belt); |
| 132 | + } |
| 133 | + |
| 134 | + // Bodies with decreasing friction |
| 135 | + for (let i = 0; i <= 10; ++i) { |
| 136 | + const cargo = createBox(new Jolt.RVec3(-cBeltLength + i * 10.0, 10.0, -cBeltLength), |
| 137 | + Jolt.Quat.prototype.sIdentity(), new Jolt.Vec3(2, 2, 2), Jolt.EMotionType_Dynamic, LAYER_MOVING); |
| 138 | + cargo.SetFriction(Math.max((0.0, 1.0 - 0.1 * i))); |
| 139 | + } |
| 140 | + |
| 141 | + // Create 2 cylinders |
| 142 | + let cyclinderSettings = new Jolt.BodyCreationSettings(new Jolt.CylinderShape(6.0, 1.0, 0.05), |
| 143 | + new Jolt.RVec3(-25.0, 1.0, -20.0), Jolt.Quat.prototype.sRotation(new Jolt.Vec3(0, 0, 1), 0.5 * Math.PI), Jolt.EMotionType_Dynamic, LAYER_MOVING); |
| 144 | + addToScene(bodyInterface.CreateBody(cyclinderSettings), 0xff0000); |
| 145 | + cyclinderSettings.mPosition.SetZ(20); |
| 146 | + addToScene(bodyInterface.CreateBody(cyclinderSettings), 0xff0000); |
| 147 | + |
| 148 | + { |
| 149 | + // Let a dynamic belt rest on it |
| 150 | + const dynamicBelt = createBox(new Jolt.RVec3(-25.0, 3.0, 0), Jolt.Quat.prototype.sIdentity(), new Jolt.Vec3(5.0, 0.1, 25.0), Jolt.EMotionType_Dynamic, LAYER_MOVING, 0x333399) |
| 151 | + linearBelts.push(dynamicBelt); |
| 152 | + |
| 153 | + // Create cargo on the dynamic belt |
| 154 | + const cargo = createBox(new Jolt.RVec3(-25.0, 6.0, 15.0), |
| 155 | + Jolt.Quat.prototype.sIdentity(), new Jolt.Vec3(2, 2, 2), Jolt.EMotionType_Dynamic, LAYER_MOVING, 0x990099); |
| 156 | + cargo.SetFriction(1); |
| 157 | + } |
| 158 | + |
| 159 | + // Create an angular belt |
| 160 | + const angularBelt = createBox(new Jolt.RVec3(10.0, 3.0, 0), Jolt.Quat.prototype.sIdentity(), new Jolt.Vec3(20.0, 0.1, 20.0), Jolt.EMotionType_Static, LAYER_NON_MOVING, 0x993333) |
| 161 | + |
| 162 | + // Bodies with decreasing friction dropping on the angular belt |
| 163 | + for (let i = 0; i <= 6; ++i) { |
| 164 | + const cargo = createBox(new Jolt.RVec3(10.0, 10.0, -15.0 + 5.0 * i), |
| 165 | + Jolt.Quat.prototype.sIdentity(), new Jolt.Vec3(2, 2, 2), Jolt.EMotionType_Dynamic, LAYER_MOVING, 0x339999); |
| 166 | + cargo.SetFriction(Math.max((0.0, 1.0 - 0.1 * i))); |
| 167 | + } |
| 168 | + |
| 169 | + // Register contact listener |
| 170 | + |
| 171 | + workerParams.angularBelt[0] = angularBelt.GetID().GetIndexAndSequenceNumber(); |
| 172 | + workerParams.linearBelts.set(linearBelts.map(belt => belt.GetID().GetIndexAndSequenceNumber())); |
| 173 | + onWorker(Jolt, workerParams); |
| 174 | + |
| 175 | + physicsSystem.SetContactListener(contactListener); |
| 176 | + }); |
| 177 | + </script> |
| 178 | + </body> |
| 179 | +</html> |
0 commit comments