forked from schteppe/cannon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Body.js
163 lines (136 loc) · 5 KB
/
Body.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var Vec3 = require("../src/math/Vec3");
var Mat3 = require("../src/math/Mat3");
var Quaternion = require("../src/math/Quaternion");
var Box = require('../src/shapes/Box');
var Sphere = require('../src/shapes/Sphere');
var Body = require('../src/objects/Body');
module.exports = {
computeAABB : {
box: function(test){
var body = new Body({ mass: 1 });
body.addShape(new Box(new Vec3(1,1,1)));
body.computeAABB();
test.equal(body.aabb.lowerBound.x,-1);
test.equal(body.aabb.lowerBound.y,-1);
test.equal(body.aabb.lowerBound.z,-1);
test.equal(body.aabb.upperBound.x,1);
test.equal(body.aabb.upperBound.y,1);
test.equal(body.aabb.upperBound.z,1);
body.position.x = 1;
body.computeAABB();
test.equal(body.aabb.lowerBound.x,0);
test.equal(body.aabb.upperBound.x,2);
test.done();
},
boxOffset: function(test){
var quaternion = new Quaternion();
quaternion.setFromAxisAngle(new Vec3(0,0,1), Math.PI / 2);
var body = new Body({ mass: 1 });
body.addShape(new Box(new Vec3(1,1,1)), new Vec3(1,1,1));
body.computeAABB();
test.equal(body.aabb.lowerBound.x,0);
test.equal(body.aabb.lowerBound.y,0);
test.equal(body.aabb.lowerBound.z,0);
test.equal(body.aabb.upperBound.x,2);
test.equal(body.aabb.upperBound.y,2);
test.equal(body.aabb.upperBound.z,2);
body.position.x = 1;
body.computeAABB();
test.equal(body.aabb.lowerBound.x,1);
test.equal(body.aabb.upperBound.x,3);
test.done();
}
},
updateInertiaWorld : function(test){
var body = new Body({ mass: 1 });
body.addShape(new Box(new Vec3(1,1,1)));
body.quaternion.setFromEuler(Math.PI/2,0,0);
body.updateInertiaWorld();
test.done();
},
pointToLocalFrame : function(test){
var body = new Body({ mass: 1 });
body.addShape(new Sphere(1));
body.position.set(1,2,2);
var localPoint = body.pointToLocalFrame(new Vec3(1,2,3));
test.ok(localPoint.almostEquals(new Vec3(0,0,1)));
test.done();
},
pointToWorldFrame : function(test){
var body = new Body({ mass: 1 });
body.addShape(new Sphere(1));
body.position.set(1,2,2);
var worldPoint = body.pointToWorldFrame(new Vec3(1,0,0));
test.ok(worldPoint.almostEquals(new Vec3(2,2,2)));
test.done();
},
addShape : function(test){
var sphereShape = new Sphere(1);
var bodyA = new Body({
mass: 1,
shape: sphereShape
});
var bodyB = new Body({
mass: 1
});
bodyB.addShape(sphereShape);
test.deepEqual(bodyA.shapes, bodyB.shapes, 'Adding shape via options did not work.');
test.deepEqual(bodyA.inertia, bodyB.inertia);
test.done();
},
applyForce : function(test){
var sphereShape = new Sphere(1);
var body = new Body({
mass: 1,
shape: sphereShape
});
var worldPoint = new Vec3(1,0,0);
var forceVector = new Vec3(0,1,0);
body.applyForce(forceVector, worldPoint);
test.deepEqual(body.force, forceVector);
test.deepEqual(body.torque, new Vec3(0,0,1));
test.done();
},
applyLocalForce : function(test){
var sphereShape = new Sphere(1);
var body = new Body({
mass: 1,
shape: sphereShape
});
body.quaternion.setFromAxisAngle(new Vec3(1, 0, 0), Math.PI / 2);
var localPoint = new Vec3(1,0,0);
var localForceVector = new Vec3(0,1,0);
body.applyLocalForce(localForceVector, localPoint);
test.ok(body.force.almostEquals(new Vec3(0,0,1))); // The force is rotated to world space
test.done();
},
applyImpulse : function(test){
var sphereShape = new Sphere(1);
var body = new Body({
mass: 1,
shape: sphereShape
});
var f = 1000;
var dt = 1 / 60;
var worldPoint = new Vec3(0,0,0);
var impulse = new Vec3(f*dt,0,0);
body.applyImpulse(impulse, worldPoint);
test.ok(body.velocity.almostEquals(new Vec3(f*dt,0,0)));
test.done();
},
applyLocalImpulse : function(test){
var sphereShape = new Sphere(1);
var body = new Body({
mass: 1,
shape: sphereShape
});
body.quaternion.setFromAxisAngle(new Vec3(1, 0, 0), Math.PI / 2);
var f = 1000;
var dt = 1 / 60;
var localPoint = new Vec3(1,0,0);
var localImpulseVector = new Vec3(0,f*dt,0);
body.applyLocalImpulse(localImpulseVector, localPoint);
test.ok(body.velocity.almostEquals(new Vec3(0,0,f*dt))); // The force is rotated to world space
test.done();
},
};