/
friction.html
75 lines (66 loc) · 2.42 KB
/
friction.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
<!DOCTYPE html>
<html>
<head>
<title>Friction demo - p2.js physics engine</title>
<script src="../build/p2.js"></script>
<script src="../build/p2.renderer.js"></script>
<link href="css/demo.css" rel="stylesheet"/>
<meta name="description" content="Shows how to add friction to a body.">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body>
<script>
// Create demo application
var app = new p2.WebGLRenderer(function(){
// Create a world
var world = new p2.World({
gravity : [0, -10]
});
this.setWorld(world);
// Create a circle
var shape = new p2.Circle({
radius: 0.5,
material: new p2.Material()
});
var p = new p2.Body({
mass: 1,
position: [0, 1.5],
});
p.addShape(shape);
world.addBody(p);
// Create a slippery circle
var slipperyShape = new p2.Circle({
radius: 0.5,
material: new p2.Material()
});
var p = new p2.Body({
mass: 1,
position: [-1.5, 1.5],
});
p.addShape(slipperyShape);
world.addBody(p);
// Create ground
var planeShape = new p2.Plane({
material: new p2.Material()
});
var plane = new p2.Body({
angle: Math.PI / 16
});
plane.addShape(planeShape);
world.addBody(plane);
// When the materials of the plane and the first circle meet, they should yield
// a contact friction of 0.3. We tell p2 this by creating a ContactMaterial.
frictionContactMaterial = new p2.ContactMaterial(planeShape.material, shape.material, {
friction: 0.3
});
world.addContactMaterial(frictionContactMaterial);
// When the plane and the slippery circle meet, the friction should be 0 (slippery). Add a new ContactMaterial.
slipperyContactMaterial = new p2.ContactMaterial(planeShape.material, slipperyShape.material, {
friction: 0
});
world.addContactMaterial(slipperyContactMaterial);
});
</script>
</body>
</html>