Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Inlined vbn in Arbiter.applyImpulse. 1% improvement

  • Loading branch information...
commit 2b4e8cbec6ecef3b33b21d62d2f9ff067ddec470 1 parent 3877f5b
@josephg josephg authored
Showing with 56 additions and 37 deletions.
  1. +38 −28 benchmark/bench.js
  2. +11 −1 cp.js
  3. +6 −8 lib/cpArbiter.js
  4. +1 −0  lib/cpCollision.js
View
66 benchmark/bench.js
@@ -20,8 +20,8 @@ var simple_terrain_verts = [
];
var frand_unit_circle = function(){
- var v = new Vect(mersenne.rand_real()*2 - 1, mersenne.rand_real()*2 - 1);
- return (vlengthsq(v) < 1 ? v : frand_unit_circle());
+ var vv = new Vect(mersenne.rand_real()*2 - 1, mersenne.rand_real()*2 - 1);
+ return (vlengthsq(vv) < 1 ? vv : frand_unit_circle());
};
var add_circle = function(i, radius){
@@ -271,7 +271,8 @@ add_benchmark('BouncyTerrainCircles 500', function(){
var mass = radius*radius;
var body = space.addBody(new Body(mass, momentForCircle(mass, 0, radius, vzero)));
body.p = vadd(vmult(frand_unit_circle(), 130), vzero);
- body.setVelocity(vmult(frand_unit_circle(), 50));
+ var vv = vmult(frand_unit_circle(), 50);
+ body.vx = vv.x; body.vy = vv.y;
var shape = space.addShape(new CircleShape(body, radius, vzero));
shape.e = 1;
@@ -302,7 +303,8 @@ add_benchmark('BouncyTerrainHexagons 500', function(){
var mass = radius*radius;
var body = space.addBody(new Body(mass, momentForPoly(mass, hexagon, vzero)));
body.p = vadd(vmult(frand_unit_circle(), 130), vzero);
- body.setVelocity(vmult(frand_unit_circle(), 50));
+ var vv = vmult(frand_unit_circle(), 50);
+ body.vx = vv.x; body.vy = vv.y;
var shape = space.addShape(new PolyShape(body, hexagon, vzero));
shape.e = 1;
@@ -341,7 +343,7 @@ add_benchmark('NoCollide', function(){
var mass = 7;
var body = space.addBody(new Body(mass, momentForCircle(mass, 0, radius, vzero)));
body.p = v(-320, y);
- body.setVelocity(v(100, 0));
+ body.vx = 100;
var shape = space.addShape(new CircleShape(body, radius, vzero));
shape.e = 1;
@@ -352,7 +354,7 @@ add_benchmark('NoCollide', function(){
var mass = 7;
var body = space.addBody(new Body(mass, momentForCircle(mass, 0, radius, vzero)));
body.p = v(x, -240);
- body.setVelocity(v(0, 100));
+ body.vy = 100;
var shape = space.addShape(new CircleShape(body, radius, vzero));
shape.e = 1;
@@ -442,53 +444,61 @@ var run_bench = function(bench, num) {
return end - start;
};
+if(typeof(print) === 'undefined') {
+ var print = console.warn;
+}
+
var bench = function(){
var NUM = 200;
- console.log("Running " + NUM + " steps of each simulation with a seed of " + SEED);
+ print("Running " + NUM + " steps of each simulation with a seed of " + SEED);
for(var i = 0; i < bench_list.length; i++){
+ //var i = bench_list.length - 1; {
var bench = bench_list[i];
- console.warn(bench.name);
+ print(bench.name);
- sample = new Array(5);
- for(var run = 0; run < 5; run++) {
+ sample = new Array(9);
+ for(var run = 0; run < sample.length; run++) {
sample[run] = run_bench(bench, NUM);
- console.warn("Run " + (run+1) + ": " + sample[run])
+ print("Run " + (run+1) + ": " + sample[run])
}
sample.sort();
- bench.time = (sample[1] + sample[2] + sample[3]) / 3;
+ bench.time = (sample[3] + sample[4] + sample[5]) / 3;
- console.warn(bench.name + " in " + bench.time + " ms");
- console.warn();
+ print(bench.name + " in " + bench.time + " ms");
+ print();
}
+
for(var i = 0; i < bench_list.length; i++){
var bench = bench_list[i];
- console.log(bench.time);
+ print(bench.time);
}
};
var profile = function(){
- //run_bench(bench_list[bench_list.length - 1], 500);
+ //run_bench(bench_list[bench_list.length - 1], 5000);
run_bench(bench_list[0], 50);
};
-//bench();
-profile();
+bench();
+//profile();
+
+
+print('vects: ' + numVects);
+print('contacts: ' + numContacts);
+print('node: ' + numNodes);
+print('leaf: ' + numLeaves);
+print('bb: ' + numBB);
-console.log('vects: ' + numVects);
-console.log('contacts: ' + numContacts);
-console.log('node: ' + numNodes);
-console.log('leaf: ' + numLeaves);
-console.log('bb: ' + numBB);
+print(numVects);
+print(numContacts);
+print(numNodes);
+print(numLeaves);
+print(numBB);
-console.log(numVects);
-console.log(numContacts);
-console.log(numNodes);
-console.log(numLeaves);
-console.log(numBB);
var tracesArr = [];
for(trace in traces) {
View
12 cp.js
@@ -2932,6 +2932,14 @@ Arbiter.prototype.applyImpulse = function()
//var vbn = vdot(vsub(vb2, vb1), n);
var vbn = vdot2(vb2x-vb1x, vb2y-vb1y, n.x, n.y);
+
+ //var vbn = n.x*(b.v_biasx - r2.y * b.w_bias - a.v_biasx + r1.y * a.w_bias) +
+ // n.y*(r2.x*b.w_bias + b.v_biasy - r1.x * a.w_bias - a.v_biasy);
+
+ //if (vbn != vbn2) {
+ // throw new Error('asdfdsf ' + vbn + ' ' + vbn2);
+ //}
+
var vrn = vdot2(vrx, vry, n.x, n.y);
//var vrt = vdot(vadd(vr, surface_vr), vperp(n));
var vrt = vdot2(vrx + surface_vr.x, vry + surface_vr.y, -n.y, n.x);
@@ -2958,8 +2966,9 @@ Arbiter.prototype.applyImpulse = function()
//apply_impulses(a, b, r1, r2, vrotate(n, new Vect(con.jnAcc - jnOld, con.jtAcc - jtOld)));
var rot_x = con.jnAcc - jnOld;
var rot_y = con.jtAcc - jtOld;
- apply_impulses(a, b, r1, r2, n.x*rot_x - n.y*rot_y, n.x*rot_y + n.y*rot_x);
+ // Inlining apply_impulses decreases speed for some reason :/
+ apply_impulses(a, b, r1, r2, n.x*rot_x - n.y*rot_y, n.x*rot_y + n.y*rot_x);
}
};
@@ -3306,6 +3315,7 @@ var collideShapes = exports.collideShapes = function(a, b)
assert(a.collisionCode <= b.collisionCode, 'Collided shapes must be sorted by type');
return a.collisionTable[b.collisionCode](a, b);
};
+
/* Copyright (c) 2007 Scott Lembcke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
View
14 lib/cpArbiter.js
@@ -365,15 +365,12 @@ Arbiter.prototype.applyImpulse = function()
var vry = b.vy + r2.x * b.w - (a.vy + r1.x * a.w);
//var vb1 = vadd(vmult(vperp(r1), a.w_bias), a.v_bias);
- var vb1x = (-r1.y) * a.w_bias + a.v_biasx;
- var vb1y = ( r1.x) * a.w_bias + a.v_biasy;
-
//var vb2 = vadd(vmult(vperp(r2), b.w_bias), b.v_bias);
- var vb2x = (-r2.y) * b.w_bias + b.v_biasx;
- var vb2y = ( r2.x) * b.w_bias + b.v_biasy;
-
//var vbn = vdot(vsub(vb2, vb1), n);
- var vbn = vdot2(vb2x-vb1x, vb2y-vb1y, n.x, n.y);
+
+ var vbn = n.x*(b.v_biasx - r2.y * b.w_bias - a.v_biasx + r1.y * a.w_bias) +
+ n.y*(r2.x*b.w_bias + b.v_biasy - r1.x * a.w_bias - a.v_biasy);
+
var vrn = vdot2(vrx, vry, n.x, n.y);
//var vrt = vdot(vadd(vr, surface_vr), vperp(n));
var vrt = vdot2(vrx + surface_vr.x, vry + surface_vr.y, -n.y, n.x);
@@ -400,8 +397,9 @@ Arbiter.prototype.applyImpulse = function()
//apply_impulses(a, b, r1, r2, vrotate(n, new Vect(con.jnAcc - jnOld, con.jtAcc - jtOld)));
var rot_x = con.jnAcc - jnOld;
var rot_y = con.jtAcc - jtOld;
- apply_impulses(a, b, r1, r2, n.x*rot_x - n.y*rot_y, n.x*rot_y + n.y*rot_x);
+ // Inlining apply_impulses decreases speed for some reason :/
+ apply_impulses(a, b, r1, r2, n.x*rot_x - n.y*rot_y, n.x*rot_y + n.y*rot_x);
}
};
View
1  lib/cpCollision.js
@@ -328,3 +328,4 @@ var collideShapes = exports.collideShapes = function(a, b)
assert(a.collisionCode <= b.collisionCode, 'Collided shapes must be sorted by type');
return a.collisionTable[b.collisionCode](a, b);
};
+
Please sign in to comment.
Something went wrong with that request. Please try again.