forked from kripken/box2d.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
82 lines (61 loc) · 1.79 KB
/
test.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
// e.g.
// mozjs -e "load('Box2D_v2.2.1_min.wasm.js')" test.js
Box2D().then(function(Box2D) {
function clock() {
return Date.now();
}
var DEBUG = 1;
var WARMUP = 64;
var FRAMES = 256;
var ITERATIONS = WARMUP + FRAMES;
var e_count = 40;
function bench() {
var gravity = new Box2D.b2Vec2(0.0, -10.0);
var world = new Box2D.b2World(gravity);
world.SetAllowSleeping(false);
var bd = new Box2D.b2BodyDef();
var ground = world.CreateBody(bd);
var shape0 = new Box2D.b2EdgeShape();
shape0.Set(new Box2D.b2Vec2(-40.0, 0.0), new Box2D.b2Vec2(40.0, 0.0));
ground.CreateFixture(shape0, 0.0);
var topBody;
var a = 0.5;
var shape = new Box2D.b2PolygonShape();
shape.SetAsBox(a, a);
var x = new Box2D.b2Vec2(-7.0, 0.75);
var y = new Box2D.b2Vec2();
var deltaX = new Box2D.b2Vec2(0.5625, 1);
var deltaY = new Box2D.b2Vec2(1.125, 0.0);
for (var i = 0; i < e_count; ++i) {
y.set_x(x.get_x());
y.set_y(x.get_y());
for (var j = i; j < e_count; ++j) {
var bd = new Box2D.b2BodyDef();
bd.set_type(Box2D.b2_dynamicBody);
bd.set_position(y);
var body = world.CreateBody(bd);
body.CreateFixture(shape, 5.0);
topBody = body;
y.op_add(deltaY);
}
x.op_add(deltaX);
}
var times = []
for (var i = 0; i < ITERATIONS; ++i) {
var start = clock();
world.Step(1.0/60.0, 3, 3);
var end = clock();
times.push(end - start);
if (DEBUG) print([topBody.GetPosition().get_x(), topBody.GetPosition().get_y()]);
}
// Slice off the warmup frames.
times = times.slice(WARMUP)
print(times);
var total = 0;
for (var i = 0; i < times.length; ++i) {
total += times[i];
}
print(total/FRAMES);
}
bench();
});