-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysicsComponent.js
More file actions
57 lines (48 loc) · 2.1 KB
/
PhysicsComponent.js
File metadata and controls
57 lines (48 loc) · 2.1 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var PhysicsComponent = Component.$extend({
_b2body: null,
world: null,
get angle() { return this._b2body.GetAngle(); },
set angle(val) { this._b2body.SetAngularVelocity(val); },
// get density() { return this._b2body.density; },
// set density(val) { this._b2body.density = val; },
// get friction() { return this._b2body.friction; },
// set friction(val) { this._b2body.friction = val; },
// get restitution() { return this._b2body.restitution; },
// set restitution(val) { this._b2body.restitution = val; },
_isStatic: true,
__init__: function (world, isStatic) {
this.world = world;
this.isStatic = isStatic;
this.onFrame = bind(this.onFrame, this);
},
onReady: function() {
this.size = this.container.getComponent('size');
this.position = this.container.getComponent('position');
this.createBody();
this.container.addListener('frame', this.onFrame);
},
onFrame: function() {
log('physics: setting new coords')
this.container.fire({
type: 'setPosition',
x: this._b2body.GetPosition().x * this.world.ratio,
y: this._b2body.GetPosition().y * this.world.ratio
});
this.container.angle = this.angle;
},
createBody: function() {
var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.position.x = (this.position.x + this.size.width / 2) / this.world.ratio;
bodyDef.position.y = (this.position.y + this.size.height / 2) / this.world.ratio;
bodyDef.type = this.isStatic ? Box2D.Dynamics.b2Body.b2_staticBody : Box2D.Dynamics.b2Body.b2_dynamicBody;
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
fixtureDef.density = 1;
fixtureDef.friction = 0.3;
fixtureDef.restitution = 0.1;
fixtureDef.shape = new Box2D.Collision.Shapes.b2PolygonShape();
//fixtureDef.shape = new Box2D.Collision.Shapes.b2CircleShape(this.width / 2 / this.RATIO);
fixtureDef.shape.SetAsBox(this.size.width / 2 / this.world.ratio, this.size.height / 2 / this.world.ratio);
this._b2body = this.world._world.CreateBody(bodyDef);
this._b2body.CreateFixture(fixtureDef);
}
});