add rope support

This commit is contained in:
Asraelite 2016-03-26 18:43:34 +00:00
parent ccc77490ea
commit ff0ac094cf
7 changed files with 91 additions and 19 deletions

View file

@ -4,6 +4,12 @@
// so most changes done to it should be mirrored there to keep consistent
// physics between client and server.
// Note:
// b2Body.GetWorldPoint is broken or something and to get it to work, you
// must pass an object as the second argument. I don't think it matters what
// object but numbers and strings don't work, so do something like
// b2body.GetWorldPoint(new b2Vec2(x, y), {});
const SCALE = 32;
const Box2D = require('box2d-html5');
@ -19,11 +25,15 @@ class Physics {
let bodya = contact.GetFixtureA().GetBody().GetUserData();
let bodyb = contact.GetFixtureB().GetBody().GetUserData();
bodya.applyDelta();
bodyb.applyDelta();
if (bodya) {
bodya.applyDelta();
bodya.contact(bodyb);
}
bodya.contact(bodyb);
bodyb.contact(bodya);
if (bodyb) {
bodyb.applyDelta();
bodyb.contact(bodya);
}
}
let listener = new Box2D.b2ContactListener();
@ -66,11 +76,37 @@ class Physics {
}
body.b2body = b2body;
//if (body.type == 'ship') console.log(b2body.GetLocalCenter());
}
// TODO: Make this shorter somehow.
createCopula(copula) {
if (copula.type == 'rope') {
let b1 = copula.bodyA.b2body;
let b2 = copula.bodyB.b2body;
let p1 = copula.pointA;
let p2 = copula.pointB;
// See top of file.
let start = b1.GetWorldPoint(b1.GetLocalCenter(), {});
let end = b2.GetWorldPoint(new b2Vec2(p2.x, p2.y), {});
console.log(start, end);
let dx = start.x - end.x
let dy = start.y - end.y;
let len = Math.sqrt(dx * dx + dy * dy);
let ropeDef = new Box2D.b2RopeJointDef();
ropeDef.bodyA = b1;
ropeDef.bodyB = b2;
ropeDef.maxLength = len;
ropeDef.collideConnected = true;
ropeDef.worldAnchorA = new b2Vec2(start);
ropeDef.worldAnchorB = new b2Vec2(end);
let b2joint = this.world.CreateJoint(ropeDef);
copula.b2joint = b2joint;
}
}
raycast(start, end) {
@ -84,7 +120,7 @@ class Physics {
};
let i = 0;
this.world.RayCast((fixture, point, normal, fraction) => {
let body = fixture.GetBody().GetUserData();;
let body = fixture.GetBody().GetUserData();
if (fraction <= closest.fraction) {
closest = {
body: body,
@ -102,6 +138,10 @@ class Physics {
this.toRemove.push(body);
}
removeCopula(copula) {
this.toRemove.push.apply(this.toRemove, copula.bodies);
}
step() {
this.world.Step(1, 5, 1 / 60);