Created
April 3, 2019 08:23
-
-
Save BlueMagnificent/19ef266f16a4610a4c237d2168fd647d to your computer and use it in GitHub Desktop.
Javascript 3D Create Joint Objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createJointObjects(){ | |
let pos1 = {x: -1, y: 15, z: 0}; | |
let pos2 = {x: -1, y: 10, z: 0}; | |
let radius = 2; | |
let scale = {x: 5, y: 2, z: 2}; | |
let quat = {x: 0, y: 0, z: 0, w: 1}; | |
let mass1 = 0; | |
let mass2 = 1; | |
let transform = new Ammo.btTransform(); | |
//Sphere Graphics | |
let ball = new THREE.Mesh(new THREE.SphereBufferGeometry(radius), new THREE.MeshPhongMaterial({color: 0xb846db})); | |
ball.position.set(pos1.x, pos1.y, pos1.z); | |
ball.castShadow = true; | |
ball.receiveShadow = true; | |
scene.add(ball); | |
//Sphere Physics | |
transform.setIdentity(); | |
transform.setOrigin( new Ammo.btVector3( pos1.x, pos1.y, pos1.z ) ); | |
transform.setRotation( new Ammo.btQuaternion( quat.x, quat.y, quat.z, quat.w ) ); | |
let motionState = new Ammo.btDefaultMotionState( transform ); | |
let sphereColShape = new Ammo.btSphereShape( radius ); | |
sphereColShape.setMargin( 0.05 ); | |
let localInertia = new Ammo.btVector3( 0, 0, 0 ); | |
sphereColShape.calculateLocalInertia( mass1, localInertia ); | |
let rbInfo = new Ammo.btRigidBodyConstructionInfo( mass1, motionState, sphereColShape, localInertia ); | |
let sphereBody = new Ammo.btRigidBody( rbInfo ); | |
physicsWorld.addRigidBody( sphereBody, colGroupGreenBall, colGroupRedBall ); | |
ball.userData.physicsBody = sphereBody; | |
rigidBodies.push(ball); | |
//Block Graphics | |
let block = new THREE.Mesh(new THREE.BoxBufferGeometry(), new THREE.MeshPhongMaterial({color: 0xf78a1d})); | |
block.position.set(pos2.x, pos2.y, pos2.z); | |
block.scale.set(scale.x, scale.y, scale.z); | |
block.castShadow = true; | |
block.receiveShadow = true; | |
scene.add(block); | |
//Block Physics | |
transform.setIdentity(); | |
transform.setOrigin( new Ammo.btVector3( pos2.x, pos2.y, pos2.z ) ); | |
transform.setRotation( new Ammo.btQuaternion( quat.x, quat.y, quat.z, quat.w ) ); | |
motionState = new Ammo.btDefaultMotionState( transform ); | |
let blockColShape = new Ammo.btBoxShape( new Ammo.btVector3( scale.x * 0.5, scale.y * 0.5, scale.z * 0.5 ) ); | |
blockColShape.setMargin( 0.05 ); | |
localInertia = new Ammo.btVector3( 0, 0, 0 ); | |
blockColShape.calculateLocalInertia( mass2, localInertia ); | |
rbInfo = new Ammo.btRigidBodyConstructionInfo( mass2, motionState, blockColShape, localInertia ); | |
let blockBody = new Ammo.btRigidBody( rbInfo ); | |
physicsWorld.addRigidBody( blockBody, colGroupGreenBall, colGroupRedBall ); | |
block.userData.physicsBody = blockBody; | |
rigidBodies.push(block); | |
//Create Joints | |
let spherePivot = new Ammo.btVector3( 0, - radius, 0 ); | |
let blockPivot = new Ammo.btVector3( - scale.x * 0.5, 1, 1 ); | |
let p2p = new Ammo.btPoint2PointConstraint( sphereBody, blockBody, spherePivot, blockPivot); | |
physicsWorld.addConstraint( p2p, false ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment