add basic updating to client on physics
This commit is contained in:
parent
95e0f6b710
commit
1027d980c1
15 changed files with 160 additions and 13 deletions
|
@ -11,6 +11,7 @@ const b2Vec2 = Box2D.b2Vec2;
|
|||
class Physics {
|
||||
constructor() {
|
||||
this.world = new Box2D.b2World(new b2Vec2(0, 0), false);
|
||||
this.toRemove = [];
|
||||
}
|
||||
|
||||
createBody(body) {
|
||||
|
@ -19,7 +20,7 @@ class Physics {
|
|||
bodyDef.position = new b2Vec2(body.x || 0, body.y || 0);
|
||||
bodyDef.fixedRotation = false;
|
||||
bodyDef.active = true;
|
||||
bodyDef.linearVelocity = new b2Vec2(body.xvel || 0, body.yvel || 0);
|
||||
bodyDef.linearVelocity = new b2Vec2(body.xvel || 0.05, body.yvel || 0);
|
||||
bodyDef.angularVelocity = body.rvel || 0;
|
||||
bodyDef.type = body.type == 'static' ?
|
||||
Box2D.b2Body.b2_staticBody : Box2D.b2Body.b2_dynamicBody;
|
||||
|
@ -31,7 +32,7 @@ class Physics {
|
|||
fixtureDef.restitution = 0;
|
||||
|
||||
for (var poly of body.structure) {
|
||||
poly.map(vertex => new b2Vec2(vertex[0], vertex[1]));
|
||||
poly = poly.map(vertex => new b2Vec2(vertex[0], vertex[1]));
|
||||
fixtureDef.shape = new Box2D.b2PolygonShape();
|
||||
fixtureDef.shape.SetAsArray(poly, poly.length);
|
||||
b2body.CreateFixture(fixtureDef);
|
||||
|
@ -42,6 +43,13 @@ class Physics {
|
|||
|
||||
step() {
|
||||
this.world.Step(1, 5, 1 / 60);
|
||||
|
||||
for (var i = 0; i < this.toRemove.length; i++) {
|
||||
this.world.DestroyBody(this.toRemove[i].b2body);
|
||||
console.log(this.world.GetBodyList());
|
||||
}
|
||||
|
||||
this.toRemove = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue