-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorldComponent.js
More file actions
42 lines (34 loc) · 1.19 KB
/
WorldComponent.js
File metadata and controls
42 lines (34 loc) · 1.19 KB
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
var WorldComponent = Component.$extend({
_world: null,
_debug: null,
ratio: 30,
gravity: { x: 0, y: 9.81 },
__init__: function (ratio, gravity) {
this.onFrame = bind(this.onFrame, this);
this.onFrameDebug = bind(this.onFrameDebug, this);
var gravity = new Box2D.Common.Math.b2Vec2(this.gravity.x, this.gravity.y);
this._world = new Box2D.Dynamics.b2World(gravity, true);
},
debug: function (canvas) {
this._debug = new Box2D.Dynamics.b2DebugDraw();
this._debug.SetSprite(canvas.getContext('2d'));
this._debug.SetDrawScale(this.ratio);
this._debug.SetFillAlpha(0.3);
this._debug.SetLineThickness(1.0);
this._debug.SetFlags(Box2D.Dynamics.b2DebugDraw.e_shapeBit | Box2D.Dynamics.b2DebugDraw.e_jointBit);
this._world.SetDebugDraw(this._debug);
this.container.addListener('frame', this.onFrameDebug);
},
onReady: function () {
this.container.addListener('frame', this.onFrame);
this.fps = this.container.getComponent('timer').fps;
},
onFrame: function () {
log('Stepping world');
this._world.Step(1 / this.fps, 10, 10, true);
this._world.ClearForces();
},
onFrameDebug: function () {
this._world.DrawDebugData();
}
});