Last active
December 28, 2019 12:18
-
-
Save BlueMagnificent/9d2bbc0cea7ddb9a486291b71f422a55 to your computer and use it in GitHub Desktop.
Javascript 3D Physics Tut 2 Create Kinematic Box
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 createKinematicBox(){ | |
let pos = {x: 40, y: 6, z: 5}; | |
let scale = {x: 10, y: 10, z: 10}; | |
let quat = {x: 0, y: 0, z: 0, w: 1}; | |
let mass = 0; | |
//threeJS Section | |
kObject = new THREE.Mesh(new THREE.BoxBufferGeometry(), new THREE.MeshPhongMaterial({color: 0x30ab78})); | |
kObject.position.set(pos.x, pos.y, pos.z); | |
kObject.scale.set(scale.x, scale.y, scale.z); | |
kObject.castShadow = true; | |
kObject.receiveShadow = true; | |
scene.add(kObject); | |
//Ammojs Section | |
let transform = new Ammo.btTransform(); | |
transform.setIdentity(); | |
transform.setOrigin( new Ammo.btVector3( pos.x, pos.y, pos.z ) ); | |
transform.setRotation( new Ammo.btQuaternion( quat.x, quat.y, quat.z, quat.w ) ); | |
let motionState = new Ammo.btDefaultMotionState( transform ); | |
let colShape = new Ammo.btBoxShape( new Ammo.btVector3( scale.x * 0.5, scale.y * 0.5, scale.z * 0.5 ) ); | |
colShape.setMargin( 0.05 ); | |
let localInertia = new Ammo.btVector3( 0, 0, 0 ); | |
colShape.calculateLocalInertia( mass, localInertia ); | |
let rbInfo = new Ammo.btRigidBodyConstructionInfo( mass, motionState, colShape, localInertia ); | |
let body = new Ammo.btRigidBody( rbInfo ); | |
body.setFriction(4); | |
body.setRollingFriction(10); | |
body.setActivationState( STATE.DISABLE_DEACTIVATION ); | |
body.setCollisionFlags( FLAGS.CF_KINEMATIC_OBJECT ); | |
physicsWorld.addRigidBody( body ); | |
kObject.userData.physicsBody = body; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment