-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathcar.html
89 lines (76 loc) · 3.53 KB
/
car.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
<!DOCTYPE html>
<html>
<head>
<title>Car 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="How to make a simple box car with a motor.">
<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] // Set gravity to -10 in y direction
});
this.setWorld(world);
// Set high friction so the wheels don't slip
world.defaultContactMaterial.friction = 100;
// Create ground shape (plane)
planeShape = new p2.Plane();
// Create a body for the ground
planeBody = new p2.Body({
mass:0 // Mass == 0 makes the body static
});
planeBody.addShape(planeShape); // Add the shape to the body
world.addBody(planeBody); // Add the body to the World
// Add circle bump in the center
circleShape = new p2.Circle({ radius: 2 }); // Circle shape of radius 2
circleBody = new p2.Body({
position:[0, -1] // Set initial position
});
circleBody.addShape(circleShape);
world.addBody(circleBody);
// Create chassis for our car
chassisBody = new p2.Body({
mass : 1, // Setting mass > 0 makes it dynamic
position: [-4,1] // Initial position
});
chassisShape = new p2.Box({ width: 1, height: 0.5 }); // Chassis shape is a rectangle
chassisBody.addShape(chassisShape);
world.addBody(chassisBody);
// Create wheels
wheelBody1 = new p2.Body({ mass : 1, position:[chassisBody.position[0] - 0.5,0.7] });
wheelBody2 = new p2.Body({ mass : 1, position:[chassisBody.position[0] + 0.5,0.7] });
wheelShape1 = new p2.Circle({ radius: 0.2 });
wheelShape2 = new p2.Circle({ radius: 0.2 });
wheelBody1.addShape(wheelShape1);
wheelBody2.addShape(wheelShape2);
world.addBody(wheelBody1);
world.addBody(wheelBody2);
// Constrain wheels to chassis with revolute constraints.
// Revolutes lets the connected bodies rotate around a shared point.
revoluteBack = new p2.RevoluteConstraint(chassisBody, wheelBody1, {
localPivotA: [-0.5, -0.3], // Where to hinge first wheel on the chassis
localPivotB: [0, 0],
collideConnected: false
});
revoluteFront = new p2.RevoluteConstraint(chassisBody, wheelBody2, {
localPivotA: [0.5, -0.3], // Where to hinge second wheel on the chassis
localPivotB: [0, 0], // Where the hinge is in the wheel (center)
collideConnected: false
});
world.addConstraint(revoluteBack);
world.addConstraint(revoluteFront);
// Enable the constraint motor for the back wheel
revoluteBack.enableMotor();
revoluteBack.setMotorSpeed(10); // Rotational speed in radians per second
this.frame(0, 0, 8, 6);
});
</script>
</body>
</html>