forked from schteppe/cannon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotionstates.html
More file actions
82 lines (69 loc) · 2.67 KB
/
Copy pathmotionstates.html
File metadata and controls
82 lines (69 loc) · 2.67 KB
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
<!DOCTYPE html>
<html>
<head>
<title>cannon.js - motion states demo</title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<script src="../build/cannon.js"></script>
<script src="../build/cannon.demo.js"></script>
<script src="../libs/dat.gui.js"></script>
<script src="../libs/Three.js"></script>
<script src="../libs/Detector.js"></script>
<script src="../libs/Stats.js"></script>
<script src="../libs/smoothie.js"></script>
<script>
/**
* Demos of the Body.motionstate types.
*/
var demo = new CANNON.Demo();
var size = 2;
demo.addScene("Moving box",function(){
var world = setupWorld(demo);
var boxShape = new CANNON.Box(new CANNON.Vec3(size,size,size));
var sphereShape = new CANNON.Sphere(size);
var mass = 5, boxMass = 0;
// Kinematic Box
// Does only collide with dynamic bodies, but does not respond to any force.
// Its movement can be controlled by setting its velocity.
var b1 = new CANNON.RigidBody(boxMass,boxShape);
b1.motionstate = CANNON.Body.KINEMATIC;
b1.position.set(0,0,0.5*size);
world.add(b1);
demo.addVisual(b1);
// To control the box movement we must set its velocity
b1.velocity.set(0,0,5);
setInterval(function(){
if(b1.velocity.z<0)
b1.velocity.set(0,0,5);
else
b1.velocity.set(0,0,-5);
},1000);
// Dynamic Sphere
// Dynamic bodies can collide with bodies of all other motionstates.
var b2 = new CANNON.RigidBody(mass,sphereShape);
b2.position.set(0,0,3*size);
world.add(b2);
demo.addVisual(b2);
});
demo.start();
function setupWorld(demo){
var world = demo.getWorld();
world.gravity.set(0,0,-40);
world.broadphase = new CANNON.NaiveBroadphase();
world.solver.iterations = 10;
world.defaultContactMaterial.contactEquationStiffness = 1e8;
world.defaultContactMaterial.contactEquationRegularizationTime = 10;
// Static ground plane
// Static bodies only interacts with dynamic bodies. Velocity is always zero.
var groundShape = new CANNON.Plane();
var mass = 0; // mass=0 will produce a static body automatically
var groundBody = new CANNON.RigidBody(mass,groundShape);
world.add(groundBody);
demo.addVisual(groundBody);
return world;
}
</script>
</body>
</html>