forked from schteppe/cannon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleFriction.html
116 lines (96 loc) · 3.8 KB
/
simpleFriction.html
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
<!DOCTYPE html>
<html>
<head>
<title>cannon.js - friction demo</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css" type="text/css"/>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</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/TrackballControls.js"></script>
<script src="../libs/Detector.js"></script>
<script src="../libs/Stats.js"></script>
<script src="../libs/smoothie.js"></script>
<script>
/**
* How to set friction per material.
*/
var demo = new CANNON.Demo(), size = 1.0;
demo.addScene("Friction", function(){
var shape = new CANNON.Box(new CANNON.Vec3(size,size,size));
// Create world
var world = demo.getWorld();
world.broadphase = new CANNON.NaiveBroadphase();
world.iterations = 10;
// Materials
var groundMaterial = new CANNON.Material();
groundMaterial.friction = 0.3;
// ground plane
var groundShape = new CANNON.Plane();
var groundBody = new CANNON.Body({ mass: 0, material: groundMaterial });
groundBody.addShape(groundShape);
world.addBody(groundBody);
demo.addVisual(groundBody);
// Create a slippery material (friction coefficient = 0.0)
var slipperyMaterial = new CANNON.Material();
slipperyMaterial.friction = 0;
// Create slippery box
var boxBody = new CANNON.Body({ mass: 1, material: slipperyMaterial });
boxBody.addShape(shape);
boxBody.position.set(0,0,5);
world.addBody(boxBody);
demo.addVisual(boxBody);
// Create box made of groundMaterial
var boxBody2 = new CANNON.Body({ mass: 10, material: groundMaterial });
boxBody2.addShape(shape);
boxBody2.position.set(size*4,0,5);
world.addBody(boxBody2);
demo.addVisual(boxBody2);
// Change gravity so the boxes will slide along x axis
world.gravity.set(-3,0,-60);
});
demo.addScene("PerShape", function(){
// Create world
var world = demo.getWorld();
world.broadphase = new CANNON.NaiveBroadphase();
world.iterations = 10;
// Materials
var groundMaterial = new CANNON.Material();
groundMaterial.friction = 0.3;
// ground plane
var groundShape = new CANNON.Plane();
groundShape.material = groundMaterial;
var groundBody = new CANNON.Body({ mass: 0 });
groundBody.addShape(groundShape);
world.addBody(groundBody);
demo.addVisual(groundBody);
// Create a slippery material (friction coefficient = 0.0)
var slipperyMaterial = new CANNON.Material();
slipperyMaterial.friction = 0;
// Create slippery box - will slide on the plane
var boxBody = new CANNON.Body({ mass: 1 });
var shape = new CANNON.Box(new CANNON.Vec3(size,size,size));
shape.material = slipperyMaterial;
boxBody.addShape(shape);
boxBody.position.set(0,0,5);
world.addBody(boxBody);
demo.addVisual(boxBody);
// Create box made of groundMaterial - will not slide on the plane
var boxBody2 = new CANNON.Body({ mass: 10 });
var shape2 = new CANNON.Box(new CANNON.Vec3(size,size,size));
shape2.material = groundMaterial;
boxBody2.addShape(shape2);
boxBody2.position.set(size*4,0,5);
world.addBody(boxBody2);
demo.addVisual(boxBody2);
// Change gravity so the boxes will slide along x axis
world.gravity.set(-3,0,-60);
});
demo.start();
</script>
</body>
</html>