-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathpiston.html
98 lines (84 loc) · 3.01 KB
/
piston.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
<!DOCTYPE html>
<html>
<head>
<title>Piston 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 build a small piston mechanism.">
<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>
var R = 0.7,
L = R * 3;
// Create demo application
var app = new p2.WebGLRenderer(function(){
var world = new p2.World({
gravity : [0,0]
});
this.setWorld(world);
world.solver.iterations = 30;
world.solver.tolerance = 0.01;
// Create static dummy body that we can constrain other bodies to
var dummyBody = new p2.Body({
mass: 0,
});
world.addBody(dummyBody);
// Create circle
var shape = new p2.Circle({ radius: R }),
circleBody = new p2.Body({
mass: 1,
position: [0,0],
});
circleBody.addShape(shape);
world.addBody(circleBody);
// Constrain it to the world
var c = new p2.RevoluteConstraint(circleBody, dummyBody, {
worldPivot: [0, 0],
collideConnected: false
});
c.enableMotor();
c.setMotorSpeed(5);
world.addConstraint(c);
// Create arm
var armShape = new p2.Box({ width: L, height: 0.1*L });
var armBody = new p2.Body({
mass:1,
});
armBody.addShape(armShape);
world.addBody(armBody);
// Constrain arm to circle
var c2 = new p2.RevoluteConstraint(circleBody, armBody, {
localPivotA: [R*0.7, 0],
localPivotB: [L/2,0],
collideConnected: false
});
world.addConstraint(c2);
// Piston
var pistonShape = new p2.Box({ width: 1, height: 1 });
var pistonBody = new p2.Body({
mass: 1,
});
pistonBody.addShape(pistonShape);
world.addBody(pistonBody);
// Connect piston to arm
var c3 = new p2.RevoluteConstraint(pistonBody, armBody, {
localPivotA: [0,0],
localPivotB: [-L/2,0],
collideConnected: false
});
world.addConstraint(c3);
// Prismatic constraint to keep the piston along a line
var c4 = new p2.PrismaticConstraint(dummyBody, pistonBody, {
localAnchorA : [ 0, 0],
localAnchorB : [ 0, 0],
localAxisA : [ 1, 0],
collideConnected : false
});
world.addConstraint(c4);
});
</script>
</body>
</html>