Skip to content

Commit 0b07a31

Browse files
committed
optimised Resolver.preSolveVelocity
1 parent fceb0ca commit 0b07a31

1 file changed

Lines changed: 35 additions & 45 deletions

File tree

src/collision/Resolver.js

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ var Resolver = {};
99
module.exports = Resolver;
1010

1111
var Vertices = require('../geometry/Vertices');
12-
var Vector = require('../geometry/Vector');
13-
var Common = require('../core/Common');
1412
var Bounds = require('../geometry/Bounds');
1513

1614
(function() {
@@ -169,61 +167,53 @@ var Bounds = require('../geometry/Bounds');
169167
* @param {pair[]} pairs
170168
*/
171169
Resolver.preSolveVelocity = function(pairs) {
172-
var i,
173-
j,
174-
pair,
175-
contacts,
176-
collision,
177-
bodyA,
178-
bodyB,
179-
normal,
180-
tangent,
181-
contact,
182-
contactVertex,
183-
normalImpulse,
184-
tangentImpulse,
185-
offset,
186-
impulse = Vector._temp[0],
187-
tempA = Vector._temp[1];
170+
var pairsLength = pairs.length,
171+
i,
172+
j;
188173

189-
for (i = 0; i < pairs.length; i++) {
190-
pair = pairs[i];
174+
for (i = 0; i < pairsLength; i++) {
175+
var pair = pairs[i];
191176

192177
if (!pair.isActive || pair.isSensor)
193178
continue;
194179

195-
contacts = pair.activeContacts;
196-
collision = pair.collision;
197-
bodyA = collision.parentA;
198-
bodyB = collision.parentB;
199-
normal = collision.normal;
200-
tangent = collision.tangent;
201-
180+
var contacts = pair.activeContacts,
181+
contactsLength = contacts.length,
182+
collision = pair.collision,
183+
bodyA = collision.parentA,
184+
bodyB = collision.parentB,
185+
normal = collision.normal,
186+
tangent = collision.tangent;
187+
202188
// resolve each contact
203-
for (j = 0; j < contacts.length; j++) {
204-
contact = contacts[j];
205-
contactVertex = contact.vertex;
206-
normalImpulse = contact.normalImpulse;
207-
tangentImpulse = contact.tangentImpulse;
208-
189+
for (j = 0; j < contactsLength; j++) {
190+
var contact = contacts[j],
191+
contactVertex = contact.vertex,
192+
normalImpulse = contact.normalImpulse,
193+
tangentImpulse = contact.tangentImpulse;
194+
209195
if (normalImpulse !== 0 || tangentImpulse !== 0) {
210196
// total impulse from contact
211-
impulse.x = (normal.x * normalImpulse) + (tangent.x * tangentImpulse);
212-
impulse.y = (normal.y * normalImpulse) + (tangent.y * tangentImpulse);
197+
var impulseX = normal.x * normalImpulse + tangent.x * tangentImpulse,
198+
impulseY = normal.y * normalImpulse + tangent.y * tangentImpulse;
213199

214200
// apply impulse from contact
215201
if (!(bodyA.isStatic || bodyA.isSleeping)) {
216-
offset = Vector.sub(contactVertex, bodyA.position, tempA);
217-
bodyA.positionPrev.x += impulse.x * bodyA.inverseMass;
218-
bodyA.positionPrev.y += impulse.y * bodyA.inverseMass;
219-
bodyA.anglePrev += Vector.cross(offset, impulse) * bodyA.inverseInertia;
202+
bodyA.positionPrev.x += impulseX * bodyA.inverseMass;
203+
bodyA.positionPrev.y += impulseY * bodyA.inverseMass;
204+
bodyA.anglePrev += bodyA.inverseInertia * (
205+
(contactVertex.x - bodyA.position.x) * impulseY
206+
- (contactVertex.y - bodyA.position.y) * impulseX
207+
);
220208
}
221-
209+
222210
if (!(bodyB.isStatic || bodyB.isSleeping)) {
223-
offset = Vector.sub(contactVertex, bodyB.position, tempA);
224-
bodyB.positionPrev.x -= impulse.x * bodyB.inverseMass;
225-
bodyB.positionPrev.y -= impulse.y * bodyB.inverseMass;
226-
bodyB.anglePrev -= Vector.cross(offset, impulse) * bodyB.inverseInertia;
211+
bodyB.positionPrev.x -= impulseX * bodyB.inverseMass;
212+
bodyB.positionPrev.y -= impulseY * bodyB.inverseMass;
213+
bodyB.anglePrev -= bodyB.inverseInertia * (
214+
(contactVertex.x - bodyB.position.x) * impulseY
215+
- (contactVertex.y - bodyB.position.y) * impulseX
216+
);
227217
}
228218
}
229219
}
@@ -281,7 +271,7 @@ var Bounds = require('../geometry/Bounds');
281271
var tangentVelocity = Vector.dot(tangent, relativeVelocity),
282272
tangentSpeed = Math.abs(tangentVelocity),
283273
tangentVelocityDirection = Common.sign(tangentVelocity);
284-
274+
285275
// raw impulses
286276
var normalImpulse = (1 + pair.restitution) * normalVelocity,
287277
normalForce = Common.clamp(pair.separation + normalVelocity, 0, 1) * Resolver._frictionNormalMultiplier;

0 commit comments

Comments
 (0)