-
Notifications
You must be signed in to change notification settings - Fork 1
/
example4.js
415 lines (304 loc) · 13.9 KB
/
example4.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
"use strict";
//GLOBAL General variables
var mouse = new THREE.Vector2();
var clock = new THREE.Clock();
var container; //DOM location
var mouseIntersects;
var ground;
var SELECTED;
var HIGHLIGHT;
var SpaceBarDown;
//GLOBAL Graphics variables
var GLOBAL ={
camera:new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.2, 2000 ) ,
scene:new THREE.Scene(),
renderer:new THREE.WebGLRenderer(),
raycaster: new THREE.Raycaster()
}
var controls;
//GLOBAL Physics variables
var physicsWorld;
var gravityConstant = -9.8;
var rigidBodies = [];
var rigidBodies_uuid_lookup ={};
var collisionConfiguration;
var dispatcher;
var broadphase;
var solver,softBodySolver;
var transformAux1 = new Ammo.btTransform();
var PHYSICS_ON = true;
//MAIN
init();// start world building
animate(); //start rendering loop
function init() {
container = document.getElementById( 'container' );
var info = document.createElement( 'div' );
info.style.position = 'absolute';
info.style.top = '10px';
info.style.width = '100%';
info.style.textAlign = 'center';
info.innerHTML = '<b>Click + Hold</b> to Drag and move cube<br>Press <b>Spacebar</b> for thrust';
var force = document.createElement( 'div' );
force.setAttribute('id','force');
force.style.position = 'absolute';
// force.style.top = '10px';
force.style.width = '100%';
force.style.textAlign = 'center';
container.appendChild( info );
container.appendChild( force );
initGraphics();
initPhysics();
createObjects();
initInput();
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
document.addEventListener( 'mouseup', onDocumentMouseUp, false );
document.addEventListener( 'keydown', onDocumentKeyDown, false );
document.addEventListener( 'keyup', onDocumentKeyUp, false );
}
function initGraphics() {
// camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.2, 2000 );
GLOBAL.camera.position.x = -20;
GLOBAL.camera.position.y = 0;
GLOBAL.camera.position.z = -20;
//scene = new THREE.Scene();
//renderer = new THREE.WebGLRenderer();
GLOBAL.renderer.setClearColor( 0xf0f0f0 );
GLOBAL.renderer.setPixelRatio( window.devicePixelRatio );
GLOBAL.renderer.setSize( window.innerWidth, window.innerHeight );
var ambientLight = new THREE.AmbientLight( 0x404040 );
GLOBAL.scene.add( ambientLight );
container.appendChild( GLOBAL.renderer.domElement );
}
/* makes and returns a red cone graphic*/
function redCone() {
var geometry = new THREE.ConeGeometry( 1,3, 32 );
var material = new THREE.MeshBasicMaterial( {color: "rgb(90%, 5%, 5%)"} );
var cone = new THREE.Mesh( geometry, material );
return cone;
}
function createObjects() {
var x=2;//meters
var y=2;//meters
var z=2;//meters
var mass = 5;//kg
var pos = new THREE.Vector3(0,20,0);
var quat = new THREE.Quaternion();
//create a graphic and physic component for our cube
var cube = createGrapicPhysicBox(x,y,z,mass,pos,quat);
console.log(cube);//inspect to see whats availible
/*create a new graphic object inside our cube. we will
make the 'flame' graphic for our rocket cube!*/
cube.flame = redCone();
//set some props for our 'flame' we don't wan't it always on. Only when the cube is 'blasting off'
cube.flame.visible = false;//three.js visibility prop for an object
cube.userData.physicsBody.mass = mass//custom prop
//add our cube to our array, scene and physics world.
rigidBodies.push(cube);
GLOBAL.scene.add( cube );
/*add our flame to the scene. note that just adding cube won't work. also don't add it to the physics world. it's for decoration only!*/
GLOBAL.scene.add( cube.flame );
physicsWorld.addRigidBody( cube.userData.physicsBody );
//recycle pos and use for the ground's location
pos.set( 0, - 0.5, 0 );
//create object for our ground, but define the materialmeshs and color. Don't use the default inside of createGraphicPhysicsBox()
//IMPORTANT! we are passing a mass = 0 for the ground. This makes it so the ground is not able to move in our physics simulator but other objects can interact with it.
ground = createGrapicPhysicBox(20,1,20,0,pos,quat,new THREE.MeshBasicMaterial( { color: "rgb(0%, 50%, 50%)"}) );
//add the ground to our array, scene and physics world.
rigidBodies.push(ground);
GLOBAL.scene.add( ground );
physicsWorld.addRigidBody( ground.userData.physicsBody );
//create our helper image of where user is moving the cube
var HIGHLIGHTGeo = new THREE.BoxGeometry( 2, 2, 2 );
var HIGHLIGHTMaterial = new THREE.MeshBasicMaterial( { color: "rgb(0%,100%,0%)", opacity: 0.5, transparent: true } );
HIGHLIGHT = new THREE.Mesh( HIGHLIGHTGeo, HIGHLIGHTMaterial );
HIGHLIGHT.visible = false;
//note we don't want physics for this obj, it's just a helper so don't need to have it in physics world or rigidbodies.
GLOBAL.scene.add( HIGHLIGHT );
}
/* createGrapicPhysicBox()
input: dimentions of a box, mass, position in world, orientation in world and material type.
output: box object which has a graphic component found and a physics component found in obj.userData.physicsBody
*/
function createGrapicPhysicBox (sx, sy, sz, mass, pos, quat, material){
//GRAPHIC COMPONENT
/***************************************************************/
var geometry = new THREE.BoxGeometry(sx, sy, sz );
material = material || new THREE.MeshBasicMaterial( { color: "rgb(34%, 34%, 33%)"} );
var Cube = new THREE.Mesh(geometry, material);
//PHYSICS COMPONENT /******************************************************************/
var physicsShape = new Ammo.btBoxShape(new Ammo.btVector3( sx * 0.5, sy * 0.5, sz * 0.5 ) );
//set the collision margin, don't use zero, default is typically 0.04
physicsShape.setMargin(0.04);
var transform = new Ammo.btTransform();
//"SetIdentity" really just sets a safe default value for each of the data members, usually (0,0,0) on a Vector3, and (0,0,0,1) on a quaternion.
transform.setIdentity();
//we want a custom location and orientation so we set with setOrigin and setRotation
transform.setOrigin( new Ammo.btVector3( pos.x, pos.y, pos.z ) );
transform.setRotation( new Ammo.btQuaternion( quat.x, quat.y, quat.z, quat.w ) );
var motionState = new Ammo.btDefaultMotionState( transform );
var localInertia = new Ammo.btVector3( 0, 0, 0 );
physicsShape.calculateLocalInertia( mass, localInertia );
//create our final physics body info
var rbInfo = new Ammo.btRigidBodyConstructionInfo( mass, motionState, physicsShape, localInertia );
var ammoCube = new Ammo.btRigidBody( rbInfo );
Cube.userData.physicsBody = ammoCube;
return Cube;
}
function initInput() {
controls = new THREE.OrbitControls( GLOBAL.camera );
controls.target.y = 2;
};
function initPhysics() {
// Physics World configurations
broadphase = new Ammo.btDbvtBroadphase();
collisionConfiguration = new Ammo.btSoftBodyRigidBodyCollisionConfiguration();
dispatcher = new Ammo.btCollisionDispatcher( collisionConfiguration );
solver = new Ammo.btSequentialImpulseConstraintSolver();
softBodySolver = new Ammo.btDefaultSoftBodySolver();
physicsWorld = new Ammo.btSoftRigidDynamicsWorld( dispatcher, broadphase, solver, collisionConfiguration, softBodySolver);
physicsWorld.setGravity( new Ammo.btVector3( 0, gravityConstant, 0 ) );
};
var ForceDisplay = document.getElementById('force');
console.log(ForceDisplay);
function updatePhysics( deltaTime ) {
//get the current state B4 step
var prevY = rigidBodies[0].userData.physicsBody.getLinearVelocity().y();
// Step world
physicsWorld.stepSimulation( deltaTime,10);
// Update graphics after step
for ( var i = 0, objThree; i < rigidBodies.length; i++ ) {
objThree = rigidBodies[ i ];
var objPhys = objThree.userData.physicsBody;
var ms = objPhys.getMotionState();
if ( ms ) {
//get the location and orientation of our object
ms.getWorldTransform( transformAux1 );
var p = transformAux1.getOrigin();
var q = transformAux1.getRotation();
//update our graphic component using data from our physics component
objThree.position.set( p.x(), p.y(), p.z() );
objThree.quaternion.set( q.x(), q.y(), q.z(), q.w() );
if (objThree.hasOwnProperty('flame')){
//use -1 on the pos.y() because we want flame below our cube
objThree.flame.position.set( p.x(), p.y()-1, p.z() );
/*determine our change in linearVelocity in Y direction. Force = mass *(delta Velocity/ delta time). We can then use Force for things like damage to our object.
for delta time bullet runs at 60 steps per sec (regardless of frame rate, they are not connected). So we know that delta time is always 0.01667
*/
var deltaV_y = Math.abs(prevY - objPhys.getLinearVelocity().y());
//round the force with Math.floor or you could use the slower Math.round()
var force_y = Math.floor(objPhys.mass * (deltaV_y/.01667));
//large velocity change
if( deltaV_y > 20){
console.log("ouch");
}
//large force
if(force_y > 500){
console.log('force ='+force_y);
document.getElementById('force').innerHTML = '<b>Impact Force: </b>'+force_y+' newtons';
}
}
};
};
};
function onDocumentMouseDown(event){
event.preventDefault();
var plane = new THREE.Plane();
var intersection = new THREE.Vector3();
// console.log(rigidBodies);
GLOBAL.raycaster.setFromCamera( mouse, GLOBAL.camera );
var intersects = GLOBAL.raycaster.intersectObjects( rigidBodies );
// console.log(intersects)
if (intersects.length >0) {
//pause our physics sim
PHYSICS_ON = false;
controls.enabled = false;
SELECTED = intersects[0];
/* FIVE Activation States:
http://bulletphysics.org/Bullet/BulletFull/btCollisionObject_8h.html
/* IF rigidBody doesn't move it's activation state changed so that it CAN"T move unless hit by object that is active.*/
//http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=9024
//http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=9&t=4991&view=previous
SELECTED.object.userData.physicsBody.setActivationState(4);//ALWAYS ACTIVE
}
};
function onDocumentMouseMove(event){
// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
var intersects = GLOBAL.raycaster.intersectObjects( rigidBodies );
if (intersects.length >0) {
mouseIntersects = intersects[ 0 ];
if (mouseIntersects.object != ground){
container.style.cursor = 'pointer';}
else{
container.style.cursor = 'auto';
}
//we have selected our cube to move
if(SELECTED != null){
HIGHLIGHT.visible = true;
//set the position of the highlight object. use .add() to add the face of the ground or other objects to the position.
//this way our highlight will be ontop of what the mouse is pointing at, not inside it.
HIGHLIGHT.position.copy( intersects[0].point ).add( intersects[0].face.normal );
}
}
}
function onDocumentMouseUp(){
//resume our physics sim
PHYSICS_ON = true;
//turn off our helper icon
HIGHLIGHT.visible = false;
//reset to normal cursor
container.style.cursor = 'auto';
//turn the view controls back on now that mouse isn't needed for placement
controls.enabled = true;
//if the mouseUp is from placement of our block and now from controlling the view
if (SELECTED != null) {
//recycle our btTransform() object "transformAux1"
//we need a btTransform object to creat new points for our block
transformAux1.setOrigin(new Ammo.btVector3( mouseIntersects.point.x, mouseIntersects.point.y, mouseIntersects.point.z));
/*you can access the blocks location in the world with getWorldTransform, but we want to update it's location so we use setWorldTransform. pass a btTransform() object to our objects setWorldTransform method to change where it is in the world*/
SELECTED.object.userData.physicsBody.setWorldTransform(transformAux1);
//Return to default activation state. Which means obj will stay active for about 2 seconds then fall asleep unless acted upon by another moving object or force.
SELECTED.object.userData.physicsBody.setActivationState(1);
}
SELECTED = null;
return;
};
function onDocumentKeyDown(event){
//spacebar is down
if (event.keyCode === 32){
SpaceBarDown = true;
//NOTE: this is a bad way to do things. I have hard coded the fact that our cube is in position 0 our rigidbodies array. but I'm doing it just for an example. You would probably want to stick with the concept of 'selecting' an object. then if spacebar is down and selected.hasOwnProperty('flame') is true apply a forece. You'd have to change the code used here for 'selected' though because it releases on mouseup. instead release on mousedown if something if selected != null.
rigidBodies[0].userData.physicsBody.applyCentralImpulse(new Ammo.btVector3( 0,5,0 ));
rigidBodies[0].flame.visible = true;
rigidBodies[0].userData.physicsBody.setActivationState(4);//ALWAYS ACTIVE
}
}
function onDocumentKeyUp(event){
if (event.keyCode === 32){
SpaceBarDown = false;
//turn off the jets!
rigidBodies[0].userData.physicsBody.applyCentralImpulse(new Ammo.btVector3( 0, 0, 0 ));
//Hide our flame
rigidBodies[0].flame.visible = false;
//RETURN TO NORMAL STATE
rigidBodies[0].userData.physicsBody.setActivationState(1);
}
}
function animate() {
render();
requestAnimationFrame( animate );
};
function render() {
var deltaTime = clock.getDelta();
GLOBAL.renderer.render( GLOBAL.scene, GLOBAL.camera );
controls.update( deltaTime );
//pause the physics sim if we are moving things around
if(PHYSICS_ON){
updatePhysics( deltaTime );
}
GLOBAL.raycaster.setFromCamera( mouse, GLOBAL.camera);
// var intersects = GLOBAL.raycaster.intersectObjects( GLOBAL.scene.children );
};