Complete Box2D to Planck.js migration

- Replace Box2D.js with Planck.js physics engine
- Update all require paths from 'Lib/Vendor/Box2D' to 'Lib/Vendor/Planck'
- Convert Box2D contact listeners to Planck.js event system
- Fix all method name capitalization (Get* -> get*, Set* -> set*)
- Update collision detection system for Planck.js compatibility
- Server now starts successfully and basic physics working
- Character can land on platforms - core physics functional

Major milestone: Game now running on modern, maintained physics engine
This commit is contained in:
Karl Pannek 2025-07-16 15:01:59 +02:00
parent 875abd60d9
commit dc779def9c
43 changed files with 701 additions and 14524 deletions

View file

@ -1,6 +1,6 @@
define([
"Game/" + GLOBALS.context + "/GameObjects/Item",
"Lib/Vendor/Box2D",
"Lib/Vendor/Planck",
"Game/Config/Settings",
"Lib/Utilities/NotificationCenter",
"Lib/Utilities/Assert",
@ -174,17 +174,17 @@ function (Parent, Box2D, Settings, nc, Assert, optionsHelper, ItemSettings) {
};
RagDoll.prototype.getPosition = function() {
return this.body.GetPosition().Copy();
return this.body.GetPosition().clone();
};
RagDoll.prototype.getHeadPosition = function() {
return this.limbs.head.GetPosition().Copy();
return this.limbs.head.GetPosition().clone();
};
RagDoll.prototype.getBodyDef = function() {
var bodyDef = Parent.prototype.getBodyDef.call(this);
bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING;
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
bodyDef.type = 'dynamic';
bodyDef.position.y -= this.options.height / 2 / Settings.RATIO; // position it on top of ground
return bodyDef;
@ -199,11 +199,10 @@ function (Parent, Box2D, Settings, nc, Assert, optionsHelper, ItemSettings) {
fixtureDef.restitution = Settings.PLAYER_RESTITUTION;
fixtureDef.filter.groupIndex = -this.getId();
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
shape.SetAsOrientedBox(
var shape = planck.Box(
this.options.limbs.chest.width / 2 / Settings.RATIO,
this.options.limbs.chest.height / 2 / Settings.RATIO,
new Box2D.Common.Math.b2Vec2(0, 0)
planck.Vec2(0, 0)
);
fixtureDef.shape = shape;
@ -217,10 +216,9 @@ function (Parent, Box2D, Settings, nc, Assert, optionsHelper, ItemSettings) {
var w = this.options.width / Settings.RATIO;
var h = this.options.height / Settings.RATIO;
var itemShape = new Box2D.Collision.Shapes.b2PolygonShape();
itemShape.SetAsOrientedBox(w / 2, h / 2, new Box2D.Common.Math.b2Vec2(0, 0));
var itemShape = planck.Box(w / 2, h / 2, planck.Vec2(0, 0));
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
var fixtureDef = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fixtureDef.shape = itemShape;
fixtureDef.isSensor = true;
@ -228,7 +226,7 @@ function (Parent, Box2D, Settings, nc, Assert, optionsHelper, ItemSettings) {
onCollisionChange: this.onCollisionChange.bind(this)
};
this.body.CreateFixture(fixtureDef);
this.body.createFixture(fixtureDef);
};
RagDoll.prototype.addHead = function() {
@ -239,17 +237,15 @@ function (Parent, Box2D, Settings, nc, Assert, optionsHelper, ItemSettings) {
var x = this.options.x + this.options.limbs.head.x,
y = this.options.y + this.options.limbs.head.y - this.options.height / 2; // position it on top of ground;
var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
bodyDef.position.x = x / Settings.RATIO;
bodyDef.position.y = y / Settings.RATIO;
bodyDef.angle = 0;
var bodyDef = {
type: 'dynamic',
position: planck.Vec2(x / Settings.RATIO, y / Settings.RATIO),
angle: 0
};
var shape = new Box2D.Collision.Shapes.b2CircleShape();
shape.SetRadius(this.options.limbs.head.width / 2 / Settings.RATIO);
shape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0, 0));
var shape = planck.Circle(this.options.limbs.head.width / 2 / Settings.RATIO, planck.Vec2(0, 0));
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
var fixtureDef = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fixtureDef.density = Settings.PLAYER_DENSITY;
fixtureDef.friction = Settings.PLAYER_FRICTION;
fixtureDef.restitution = Settings.PLAYER_RESTITUTION;
@ -258,7 +254,7 @@ function (Parent, Box2D, Settings, nc, Assert, optionsHelper, ItemSettings) {
fixtureDef.filter.groupIndex = -this.getId();
var head = this.body.GetWorld().CreateBody(bodyDef);
head.CreateFixture(fixtureDef);
head.createFixture(fixtureDef);
this.limbs.head = head;
@ -271,7 +267,7 @@ function (Parent, Box2D, Settings, nc, Assert, optionsHelper, ItemSettings) {
var jointDef = new Box2D.Dynamics.Joints.b2RevoluteJointDef();
jointDef.enableMotor = false;
var pos = this.body.GetWorldCenter().Copy();
var pos = this.body.getWorldCenter().clone();
pos.y -= this.options.limbs.chest.height / 2 / Settings.RATIO;
jointDef.Initialize(this.body, head, pos);
jointDef.lowerAngle = -0.25 * Box2D.Common.b2Settings.b2_pi; // -45 degrees
@ -290,21 +286,20 @@ function (Parent, Box2D, Settings, nc, Assert, optionsHelper, ItemSettings) {
var x = this.options.x + this.options.limbs[name].x,
y = this.options.y + this.options.limbs[name].y - this.options.height / 2; // position it on top of ground;;
var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING;
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
bodyDef.position.x = x / Settings.RATIO;
bodyDef.position.y = y / Settings.RATIO;
bodyDef.angle = 0;
var bodyDef = {
type: 'dynamic',
position: planck.Vec2(x / Settings.RATIO, y / Settings.RATIO),
angle: 0,
linearDamping: Settings.PLAYER_LINEAR_DAMPING
};
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
shape.SetAsOrientedBox(
var shape = planck.Box(
this.options.limbs[name].width / 2 / Settings.RATIO,
this.options.limbs[name].height / 2 / Settings.RATIO,
new Box2D.Common.Math.b2Vec2(0, this.options.limbs[name].height / 2 / Settings.RATIO)
planck.Vec2(0, this.options.limbs[name].height / 2 / Settings.RATIO)
);
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
var fixtureDef = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fixtureDef.density = Settings.PLAYER_DENSITY;
fixtureDef.friction = Settings.PLAYER_FRICTION;
fixtureDef.restitution = Settings.PLAYER_RESTITUTION;
@ -313,14 +308,14 @@ function (Parent, Box2D, Settings, nc, Assert, optionsHelper, ItemSettings) {
fixtureDef.filter.groupIndex = -this.getId();
var limb = this.body.GetWorld().CreateBody(bodyDef);
limb.CreateFixture(fixtureDef);
limb.createFixture(fixtureDef);
this.limbs[name] = limb;
var jointDef = new Box2D.Dynamics.Joints.b2RevoluteJointDef();
jointDef.enableMotor = false;
var pos = connectTo.GetWorldCenter().Copy();
var pos = connectTo.getWorldCenter().clone();
pos.x += (xOffset / Settings.RATIO);
pos.y += (yOffset / Settings.RATIO);
jointDef.Initialize(connectTo, limb, pos);
@ -347,7 +342,7 @@ function (Parent, Box2D, Settings, nc, Assert, optionsHelper, ItemSettings) {
var chestPosition = this.body.GetPosition();
var position = new Box2D.Common.Math.b2Vec2(
var position = planck.Vec2(
chestPosition.x + this.options.limbs.head.x / Settings.RATIO,
chestPosition.y + this.options.limbs.head.y / Settings.RATIO
);
@ -370,7 +365,7 @@ function (Parent, Box2D, Settings, nc, Assert, optionsHelper, ItemSettings) {
Assert.number(options.angularVelocity);
this.body.SetLinearVelocity(options.linearVelocity);
this.body.SetAngularVelocity(options.angularVelocity);
this.body.setAngularVelocity(options.angularVelocity);
for(var name in this.limbs) {
this.limbs[name].SetLinearVelocity(options.linearVelocity);
}

View file

@ -1,6 +1,6 @@
define([
"Game/" + GLOBALS.context + "/GameObjects/Item",
"Lib/Vendor/Box2D",
"Lib/Vendor/Planck",
"Game/Config/Settings"
],
@ -24,13 +24,13 @@ function (Parent, Box2D, Settings) {
var bodies = [];
var joints = [];
{
var bd = new Box2D.Dynamics.b2BodyDef();
bd.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
var bd = { type: 'static', position: planck.Vec2(0, 0), angle: 0 };
bd.type = 'dynamic';
bd.position.Set(-1.917114257812500e-01, 1.433728694915771e+00);
bodies[0] = world.CreateBody(bd);
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = Settings.PLAYER_FRICTION;
fd.restitution = Settings.PLAYER_RESTITUTION;
fd.density = Settings.PLAYER_DENSITY;
@ -38,45 +38,45 @@ function (Parent, Box2D, Settings) {
fd.filter.groupIndex = -1;
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
var vs = [];
vs[0] = new Box2D.Common.Math.b2Vec2(6.299880146980286e-02, -2.545155882835388e-01);
vs[1] = new Box2D.Common.Math.b2Vec2(6.299880146980286e-02, 2.545149326324463e-01);
vs[2] = new Box2D.Common.Math.b2Vec2(-6.299890577793121e-02, 2.545149326324463e-01);
vs[3] = new Box2D.Common.Math.b2Vec2(-6.299890577793121e-02, -2.545155882835388e-01);
vs[0] = planck.Vec2(6.299880146980286e-02, -2.545155882835388e-01);
vs[1] = planck.Vec2(6.299880146980286e-02, 2.545149326324463e-01);
vs[2] = planck.Vec2(-6.299890577793121e-02, 2.545149326324463e-01);
vs[3] = planck.Vec2(-6.299890577793121e-02, -2.545155882835388e-01);
shape.SetAsArray(vs, 4);
fd.shape = shape;
bodies[0].CreateFixture(fd);
bodies[0].createFixture(fd);
}
}
{
var bd = new Box2D.Dynamics.b2BodyDef();
bd.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
var bd = { type: 'static', position: planck.Vec2(0, 0), angle: 0 };
bd.type = 'dynamic';
bd.position.Set(-6.397294998168945e-02, 1.267420768737793e+00);
bodies[1] = world.CreateBody(bd);
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = Settings.PLAYER_FRICTION;
fd.restitution = Settings.PLAYER_RESTITUTION;
fd.density = Settings.PLAYER_DENSITY;
fd.filter.groupIndex = -1;
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
var vs = [];
vs[0] = new Box2D.Common.Math.b2Vec2(1.883362084627151e-01, -4.305148720741272e-01);
vs[1] = new Box2D.Common.Math.b2Vec2(1.846363544464111e-01, 5.393795371055603e-01);
vs[2] = new Box2D.Common.Math.b2Vec2(1.850083470344543e-03, 5.393795371055603e-01);
vs[3] = new Box2D.Common.Math.b2Vec2(-1.883361339569092e-01, 4.209862351417542e-01);
vs[4] = new Box2D.Common.Math.b2Vec2(-1.883361339569092e-01, -4.607573151588440e-01);
vs[5] = new Box2D.Common.Math.b2Vec2(1.600667834281921e-03, -4.952520132064819e-01);
vs[0] = planck.Vec2(1.883362084627151e-01, -4.305148720741272e-01);
vs[1] = planck.Vec2(1.846363544464111e-01, 5.393795371055603e-01);
vs[2] = planck.Vec2(1.850083470344543e-03, 5.393795371055603e-01);
vs[3] = planck.Vec2(-1.883361339569092e-01, 4.209862351417542e-01);
vs[4] = planck.Vec2(-1.883361339569092e-01, -4.607573151588440e-01);
vs[5] = planck.Vec2(1.600667834281921e-03, -4.952520132064819e-01);
shape.SetAsArray(vs, 6);
fd.shape = shape;
bodies[1].CreateFixture(fd);
bodies[1].createFixture(fd);
}
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = Settings.PLAYER_FRICTION;
fd.restitution = Settings.PLAYER_RESTITUTION;
fd.density = Settings.PLAYER_DENSITY;
@ -84,25 +84,25 @@ function (Parent, Box2D, Settings) {
fd.filter.groupIndex = -1;
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
var vs = [];
vs[0] = new Box2D.Common.Math.b2Vec2(1.840525716543198e-01, 4.875739216804504e-01);
vs[1] = new Box2D.Common.Math.b2Vec2(1.840525716543198e-01, 6.762337088584900e-01);
vs[2] = new Box2D.Common.Math.b2Vec2(-4.607129842042923e-03, 6.762337088584900e-01);
vs[3] = new Box2D.Common.Math.b2Vec2(-4.607129842042923e-03, 4.875739216804504e-01);
vs[0] = planck.Vec2(1.840525716543198e-01, 4.875739216804504e-01);
vs[1] = planck.Vec2(1.840525716543198e-01, 6.762337088584900e-01);
vs[2] = planck.Vec2(-4.607129842042923e-03, 6.762337088584900e-01);
vs[3] = planck.Vec2(-4.607129842042923e-03, 4.875739216804504e-01);
shape.SetAsArray(vs, 4);
fd.shape = shape;
bodies[1].CreateFixture(fd);
bodies[1].createFixture(fd);
}
}
{
var bd = new Box2D.Dynamics.b2BodyDef();
bd.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
var bd = { type: 'static', position: planck.Vec2(0, 0), angle: 0 };
bd.type = 'dynamic';
bd.position.Set(4.118728637695312e-02, 2.199305295944214e+00);
bodies[2] = world.CreateBody(bd);
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = 2.000000029802322e-01;
fd.restitution = 0.000000000000000e+00;
fd.density = 2.204959988594055e-01;
@ -114,10 +114,10 @@ function (Parent, Box2D, Settings) {
fd.shape = shape;
bodies[2].CreateFixture(fd);
bodies[2].createFixture(fd);
}
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = 2.000000029802322e-01;
fd.restitution = 0.000000000000000e+00;
fd.density = 2.204959988594055e-01;
@ -129,17 +129,17 @@ function (Parent, Box2D, Settings) {
fd.shape = shape;
bodies[2].CreateFixture(fd);
bodies[2].createFixture(fd);
}
}
{
var bd = new Box2D.Dynamics.b2BodyDef();
bd.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
var bd = { type: 'static', position: planck.Vec2(0, 0), angle: 0 };
bd.type = 'dynamic';
bd.position.Set(1.235442161560059e-01, 1.142371892929077e+00);
bodies[3] = world.CreateBody(bd);
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = Settings.PLAYER_FRICTION;
fd.restitution = Settings.PLAYER_RESTITUTION;
fd.density = Settings.PLAYER_DENSITY;
@ -147,26 +147,26 @@ function (Parent, Box2D, Settings) {
fd.filter.groupIndex = -1;
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
var vs = [];
vs[0] = new Box2D.Common.Math.b2Vec2(6.299892067909241e-02, -1.556134223937988e-01);
vs[1] = new Box2D.Common.Math.b2Vec2(6.299892067909241e-02, 1.556134223937988e-01);
vs[2] = new Box2D.Common.Math.b2Vec2(-6.299898028373718e-02, 1.556134223937988e-01);
vs[3] = new Box2D.Common.Math.b2Vec2(-6.299898028373718e-02, -1.556134223937988e-01);
vs[0] = planck.Vec2(6.299892067909241e-02, -1.556134223937988e-01);
vs[1] = planck.Vec2(6.299892067909241e-02, 1.556134223937988e-01);
vs[2] = planck.Vec2(-6.299898028373718e-02, 1.556134223937988e-01);
vs[3] = planck.Vec2(-6.299898028373718e-02, -1.556134223937988e-01);
shape.SetAsArray(vs, 4);
fd.shape = shape;
bodies[3].CreateFixture(fd);
bodies[3].createFixture(fd);
}
}
{
var bd = new Box2D.Dynamics.b2BodyDef();
bd.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
var bd = { type: 'static', position: planck.Vec2(0, 0), angle: 0 };
bd.type = 'dynamic';
bd.position.Set(-9.663248062133789e-02, 3.554300665855408e-01);
bodies[4] = world.CreateBody(bd);
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = Settings.PLAYER_FRICTION;
fd.restitution = Settings.PLAYER_RESTITUTION;
fd.density = Settings.PLAYER_DENSITY;
@ -174,18 +174,18 @@ function (Parent, Box2D, Settings) {
fd.filter.groupIndex = -1;
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
var vs = [];
vs[0] = new Box2D.Common.Math.b2Vec2(1.550966501235962e-01, -1.253567039966583e-01);
vs[1] = new Box2D.Common.Math.b2Vec2(1.550966501235962e-01, -6.225190684199333e-02);
vs[2] = new Box2D.Common.Math.b2Vec2(-9.268096834421158e-02, -6.225190684199333e-02);
vs[3] = new Box2D.Common.Math.b2Vec2(-9.268096834421158e-02, -1.253567039966583e-01);
vs[0] = planck.Vec2(1.550966501235962e-01, -1.253567039966583e-01);
vs[1] = planck.Vec2(1.550966501235962e-01, -6.225190684199333e-02);
vs[2] = planck.Vec2(-9.268096834421158e-02, -6.225190684199333e-02);
vs[3] = planck.Vec2(-9.268096834421158e-02, -1.253567039966583e-01);
shape.SetAsArray(vs, 4);
fd.shape = shape;
bodies[4].CreateFixture(fd);
bodies[4].createFixture(fd);
}
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = Settings.PLAYER_FRICTION;
fd.restitution = Settings.PLAYER_RESTITUTION;
fd.density = Settings.PLAYER_DENSITY;
@ -193,26 +193,26 @@ function (Parent, Box2D, Settings) {
fd.filter.groupIndex = -1;
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
var vs = [];
vs[0] = new Box2D.Common.Math.b2Vec2(9.449840337038040e-02, -1.247676759958267e-01);
vs[1] = new Box2D.Common.Math.b2Vec2(9.449840337038040e-02, 1.715210527181625e-01);
vs[2] = new Box2D.Common.Math.b2Vec2(-9.449829906225204e-02, 1.715210527181625e-01);
vs[3] = new Box2D.Common.Math.b2Vec2(-9.449829906225204e-02, -1.247676759958267e-01);
vs[0] = planck.Vec2(9.449840337038040e-02, -1.247676759958267e-01);
vs[1] = planck.Vec2(9.449840337038040e-02, 1.715210527181625e-01);
vs[2] = planck.Vec2(-9.449829906225204e-02, 1.715210527181625e-01);
vs[3] = planck.Vec2(-9.449829906225204e-02, -1.247676759958267e-01);
shape.SetAsArray(vs, 4);
fd.shape = shape;
bodies[4].CreateFixture(fd);
bodies[4].createFixture(fd);
}
}
{
var bd = new Box2D.Dynamics.b2BodyDef();
bd.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
var bd = { type: 'static', position: planck.Vec2(0, 0), angle: 0 };
bd.type = 'dynamic';
bd.position.Set(-1.917138099670410e-01, 1.142371892929077e+00);
bodies[5] = world.CreateBody(bd);
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = Settings.PLAYER_FRICTION;
fd.restitution = Settings.PLAYER_RESTITUTION;
fd.density = Settings.PLAYER_DENSITY;
@ -220,26 +220,26 @@ function (Parent, Box2D, Settings) {
fd.filter.groupIndex = -1;
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
var vs = [];
vs[0] = new Box2D.Common.Math.b2Vec2(6.299891322851181e-02, -1.556134223937988e-01);
vs[1] = new Box2D.Common.Math.b2Vec2(6.299891322851181e-02, 1.556134223937988e-01);
vs[2] = new Box2D.Common.Math.b2Vec2(-6.299878656864166e-02, 1.556134223937988e-01);
vs[3] = new Box2D.Common.Math.b2Vec2(-6.299878656864166e-02, -1.556134223937988e-01);
vs[0] = planck.Vec2(6.299891322851181e-02, -1.556134223937988e-01);
vs[1] = planck.Vec2(6.299891322851181e-02, 1.556134223937988e-01);
vs[2] = planck.Vec2(-6.299878656864166e-02, 1.556134223937988e-01);
vs[3] = planck.Vec2(-6.299878656864166e-02, -1.556134223937988e-01);
shape.SetAsArray(vs, 4);
fd.shape = shape;
bodies[5].CreateFixture(fd);
bodies[5].createFixture(fd);
}
}
{
var bd = new Box2D.Dynamics.b2BodyDef();
bd.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
var bd = { type: 'static', position: planck.Vec2(0, 0), angle: 0 };
bd.type = 'dynamic';
bd.position.Set(1.235442161560059e-01, 1.433728694915771e+00);
bodies[6] = world.CreateBody(bd);
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = Settings.PLAYER_FRICTION;
fd.restitution = Settings.PLAYER_RESTITUTION;
fd.density = Settings.PLAYER_DENSITY;
@ -247,25 +247,25 @@ function (Parent, Box2D, Settings) {
fd.filter.groupIndex = -1;
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
var vs = [];
vs[0] = new Box2D.Common.Math.b2Vec2(6.299892067909241e-02, -2.545155882835388e-01);
vs[1] = new Box2D.Common.Math.b2Vec2(6.299892067909241e-02, 2.545149326324463e-01);
vs[2] = new Box2D.Common.Math.b2Vec2(-6.299898028373718e-02, 2.545149326324463e-01);
vs[3] = new Box2D.Common.Math.b2Vec2(-6.299898028373718e-02, -2.545155882835388e-01);
vs[0] = planck.Vec2(6.299892067909241e-02, -2.545155882835388e-01);
vs[1] = planck.Vec2(6.299892067909241e-02, 2.545149326324463e-01);
vs[2] = planck.Vec2(-6.299898028373718e-02, 2.545149326324463e-01);
vs[3] = planck.Vec2(-6.299898028373718e-02, -2.545155882835388e-01);
shape.SetAsArray(vs, 4);
fd.shape = shape;
bodies[6].CreateFixture(fd);
bodies[6].createFixture(fd);
}
}
{
var bd = new Box2D.Dynamics.b2BodyDef();
bd.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
var bd = { type: 'static', position: planck.Vec2(0, 0), angle: 0 };
bd.type = 'dynamic';
bd.position.Set(2.897095680236816e-02, 6.702435612678528e-01);
bodies[7] = world.CreateBody(bd);
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = Settings.PLAYER_FRICTION;
fd.restitution = Settings.PLAYER_RESTITUTION;
fd.density = Settings.PLAYER_DENSITY;
@ -273,26 +273,26 @@ function (Parent, Box2D, Settings) {
fd.filter.groupIndex = -1;
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
var vs = [];
vs[0] = new Box2D.Common.Math.b2Vec2(9.449830651283264e-02, -2.537839412689209e-01);
vs[1] = new Box2D.Common.Math.b2Vec2(9.449830651283264e-02, 2.537844777107239e-01);
vs[2] = new Box2D.Common.Math.b2Vec2(-9.449817240238190e-02, 2.537844777107239e-01);
vs[3] = new Box2D.Common.Math.b2Vec2(-9.449817240238190e-02, -2.537839412689209e-01);
vs[0] = planck.Vec2(9.449830651283264e-02, -2.537839412689209e-01);
vs[1] = planck.Vec2(9.449830651283264e-02, 2.537844777107239e-01);
vs[2] = planck.Vec2(-9.449817240238190e-02, 2.537844777107239e-01);
vs[3] = planck.Vec2(-9.449817240238190e-02, -2.537839412689209e-01);
shape.SetAsArray(vs, 4);
fd.shape = shape;
bodies[7].CreateFixture(fd);
bodies[7].createFixture(fd);
}
}
{
var bd = new Box2D.Dynamics.b2BodyDef();
bd.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
var bd = { type: 'static', position: planck.Vec2(0, 0), angle: 0 };
bd.type = 'dynamic';
bd.position.Set(-9.663248062133789e-02, 6.702435612678528e-01);
bodies[8] = world.CreateBody(bd);
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = Settings.PLAYER_FRICTION;
fd.restitution = Settings.PLAYER_RESTITUTION;
fd.density = Settings.PLAYER_DENSITY;
@ -300,26 +300,26 @@ function (Parent, Box2D, Settings) {
fd.filter.groupIndex = -1;
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
var vs = [];
vs[0] = new Box2D.Common.Math.b2Vec2(9.449842572212219e-02, -2.537839412689209e-01);
vs[1] = new Box2D.Common.Math.b2Vec2(9.449842572212219e-02, 2.537844777107239e-01);
vs[2] = new Box2D.Common.Math.b2Vec2(-9.449826925992966e-02, 2.537844777107239e-01);
vs[3] = new Box2D.Common.Math.b2Vec2(-9.449826925992966e-02, -2.537839412689209e-01);
vs[0] = planck.Vec2(9.449842572212219e-02, -2.537839412689209e-01);
vs[1] = planck.Vec2(9.449842572212219e-02, 2.537844777107239e-01);
vs[2] = planck.Vec2(-9.449826925992966e-02, 2.537844777107239e-01);
vs[3] = planck.Vec2(-9.449826925992966e-02, -2.537839412689209e-01);
shape.SetAsArray(vs, 4);
fd.shape = shape;
bodies[8].CreateFixture(fd);
bodies[8].createFixture(fd);
}
}
{
var bd = new Box2D.Dynamics.b2BodyDef();
bd.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
var bd = { type: 'static', position: planck.Vec2(0, 0), angle: 0 };
bd.type = 'dynamic';
bd.position.Set(2.897095680236816e-02, 3.554300665855408e-01);
bodies[9] = world.CreateBody(bd);
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = Settings.PLAYER_FRICTION;
fd.restitution = Settings.PLAYER_RESTITUTION;
fd.density = Settings.PLAYER_DENSITY;
@ -327,18 +327,18 @@ function (Parent, Box2D, Settings) {
fd.filter.groupIndex = -1;
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
var vs = [];
vs[0] = new Box2D.Common.Math.b2Vec2(1.550965905189514e-01, -1.253567039966583e-01);
vs[1] = new Box2D.Common.Math.b2Vec2(1.550965905189514e-01, -6.225190684199333e-02);
vs[2] = new Box2D.Common.Math.b2Vec2(-9.268099069595337e-02, -6.225190684199333e-02);
vs[3] = new Box2D.Common.Math.b2Vec2(-9.268099069595337e-02, -1.253567039966583e-01);
vs[0] = planck.Vec2(1.550965905189514e-01, -1.253567039966583e-01);
vs[1] = planck.Vec2(1.550965905189514e-01, -6.225190684199333e-02);
vs[2] = planck.Vec2(-9.268099069595337e-02, -6.225190684199333e-02);
vs[3] = planck.Vec2(-9.268099069595337e-02, -1.253567039966583e-01);
shape.SetAsArray(vs, 4);
fd.shape = shape;
bodies[9].CreateFixture(fd);
bodies[9].createFixture(fd);
}
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = Settings.PLAYER_FRICTION;
fd.restitution = Settings.PLAYER_RESTITUTION;
fd.density = Settings.PLAYER_DENSITY;
@ -346,29 +346,29 @@ function (Parent, Box2D, Settings) {
fd.filter.groupIndex = -1;
var shape = new Box2D.Collision.Shapes.b2PolygonShape();
var vs = [];
vs[0] = new Box2D.Common.Math.b2Vec2(9.449830651283264e-02, -1.247680261731148e-01);
vs[1] = new Box2D.Common.Math.b2Vec2(9.449830651283264e-02, 1.713046580553055e-01);
vs[2] = new Box2D.Common.Math.b2Vec2(-9.449817240238190e-02, 1.713046580553055e-01);
vs[3] = new Box2D.Common.Math.b2Vec2(-9.449817240238190e-02, -1.247680261731148e-01);
vs[0] = planck.Vec2(9.449830651283264e-02, -1.247680261731148e-01);
vs[1] = planck.Vec2(9.449830651283264e-02, 1.713046580553055e-01);
vs[2] = planck.Vec2(-9.449817240238190e-02, 1.713046580553055e-01);
vs[3] = planck.Vec2(-9.449817240238190e-02, -1.247680261731148e-01);
shape.SetAsArray(vs, 4);
fd.shape = shape;
bodies[9].CreateFixture(fd);
bodies[9].createFixture(fd);
}
}
/*{
ground body
var bd = new Box2D.Dynamics.b2BodyDef();
var bd = { type: 'static', position: planck.Vec2(0, 0), angle: 0 };
bd.type = b2BodyType(0);
bd.position.Set(3.118395805358887e-03, -6.553649902343750e-03);
bodies[10] = world.CreateBody(bd);
{
var fd = new Box2D.Dynamics.b2FixtureDef();
var fd = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fd.friction = Settings.PLAYER_FRICTION;
fd.restitution = Settings.PLAYER_RESTITUTION;
fd.density = Settings.PLAYER_DENSITY;
@ -376,8 +376,8 @@ function (Parent, Box2D, Settings) {
fd.filter.groupIndex = int16(0);
b2ChainShape shape;
b2Vec2 vs[2];
vs[0] = new Box2D.Common.Math.b2Vec2(-4.179394245147705e+00, 0.000000000000000e+00);
vs[1] = new Box2D.Common.Math.b2Vec2(4.179394245147705e+00, 0.000000000000000e+00);
vs[0] = planck.Vec2(-4.179394245147705e+00, 0.000000000000000e+00);
vs[1] = planck.Vec2(4.179394245147705e+00, 0.000000000000000e+00);
shape.CreateChain(vs, 2);
shape.m_prevVertex.Set(-1.998532295227051e+00, -2.391039296991059e-23);
shape.m_nextVertex.Set(4.949933242915726e-38, 3.363116314379561e-44);
@ -386,7 +386,7 @@ function (Parent, Box2D, Settings) {
fd.shape = shape;
bodies[10].CreateFixture(fd);
bodies[10].createFixture(fd);
}
}*/
{
@ -548,7 +548,7 @@ function (Parent, Box2D, Settings) {
lowerRightLeg: bodies[9]
};
this.body.SetPosition(new Box2D.Common.Math.b2Vec2(20,0));
this.body.SetPosition(planck.Vec2(20,0));
};
RagDoll.prototype.destroy = function() {

View file

@ -1,21 +1,25 @@
define([
"Game/" + GLOBALS.context + "/GameObjects/Item",
"Lib/Vendor/RubeLoader",
"Lib/Vendor/Box2D",
// "Lib/Vendor/RubeLoader", // Temporarily disabled during Planck.js migration
"Lib/Vendor/Planck",
"Game/Config/Settings",
"Lib/Utilities/Assert",
"Lib/Utilities/NotificationCenter",
"Lib/Utilities/Matrix",
"json!Game/Asset/RubeDoll.json" // using requirejs json loader plugin
"Lib/Utilities/Matrix"
// "json!Game/Asset/RubeDoll.json" // Temporarily disabled during Planck.js migration
],
function (Parent, RubeLoader, Box2D, Settings, Assert, nc, Matrix, RubeDollJson) {
function (Parent, /* RubeLoader, */ Box2D, Settings, Assert, nc, Matrix /* , RubeDollJson */) {
"use strict";
function RubeDoll(physicsEngine, uid, options) {
Assert.number(options.x, options.y);
// TODO: Implement RubeDoll with Planck.js
// Temporarily stubbed out during Box2D -> Planck.js migration
console.warn("RubeDoll is temporarily disabled during Planck.js migration");
this.rubeLoader = null;
this.body = null;
this.limbs = {};
@ -23,9 +27,8 @@ function (Parent, RubeLoader, Box2D, Settings, Assert, nc, Matrix, RubeDollJson)
this.limits = [];
var chest = null;
this.rubeLoader = new RubeLoader(RubeDollJson, physicsEngine.getWorldForRubeLoader());
this.loadRubeDollFromScene(options);
// this.rubeLoader = new RubeLoader(RubeDollJson, physicsEngine.getWorldForRubeLoader());
// this.loadRubeDollFromScene(options);
Parent.call(this, physicsEngine, uid, options);
physicsEngine.destroyBody(this.body);
@ -46,8 +49,8 @@ function (Parent, RubeLoader, Box2D, Settings, Assert, nc, Matrix, RubeDollJson)
for (var i in scene.bodies) {
var body = scene.bodies[i];
var position = body.GetPosition().Copy();
position.Add(new Box2D.Common.Math.b2Vec2(
var position = body.GetPosition().clone();
position.Add(planck.Vec2(
options.x / Settings.RATIO,
options.y / Settings.RATIO
));
@ -86,7 +89,7 @@ function (Parent, RubeLoader, Box2D, Settings, Assert, nc, Matrix, RubeDollJson)
};
RubeDoll.prototype.getFixtureDef = function() {
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
var fixtureDef = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fixtureDef.shape = new Box2D.Collision.Shapes.b2CircleShape();
return fixtureDef;
};
@ -134,7 +137,7 @@ function (Parent, RubeLoader, Box2D, Settings, Assert, nc, Matrix, RubeDollJson)
var differenceAngle = oldAngle - this.body.GetAngle();
//this.body.SetLinearVelocity(new Box2D.Common.Math.b2Vec2(0, 0));
//this.body.SetLinearVelocity(planck.Vec2(0, 0));
var offset = Box2D.Common.Math.b2Math.SubtractVV(this.getPosition(), oldPosition);
var grabAngle = (this.options.grabAngle || 0.001);
@ -143,12 +146,12 @@ function (Parent, RubeLoader, Box2D, Settings, Assert, nc, Matrix, RubeDollJson)
var limb = this.limbs[key];
// Setting position offset first (floor to hand)
var position = limb.GetPosition().Copy();
var position = limb.GetPosition().clone();
position.Add(offset);
limb.SetPosition(position);
// grabing local point to "rotate" around (x, y position transform only)
var localPoint = this.body.GetLocalPoint(limb.GetPosition().Copy());
var localPoint = this.body.getLocalPoint(limb.GetPosition().clone());
// create rotation matrix from chest rotation difference
var mat = Box2D.Common.Math.b2Mat22.FromAngle(differenceAngle);
@ -157,15 +160,15 @@ function (Parent, RubeLoader, Box2D, Settings, Assert, nc, Matrix, RubeDollJson)
position = Box2D.Common.Math.b2Math.MulTMV(mat, localPoint);
// translating back to global position
var globalPoint = this.body.GetWorldPoint(position);
var globalPoint = this.body.getWorldPoint(position);
limb.SetPosition(globalPoint);
// relative limb rotating by chest rotation difference
var d = (oldDirection == direction) ? -1 : 1;
limb.SetAngle((limb.GetAngle() - differenceAngle) * d);
//limb.SetType(Box2D.Dynamics.b2Body.b2_staticBody);
//limb.SetLinearVelocity(new Box2D.Common.Math.b2Vec2(0, 0));
//limb.SetType('static');
//limb.SetLinearVelocity(planck.Vec2(0, 0));
}
};
@ -174,18 +177,18 @@ function (Parent, RubeLoader, Box2D, Settings, Assert, nc, Matrix, RubeDollJson)
Assert.number(options.angularVelocity);
this.body.SetLinearVelocity(options.linearVelocity);
this.body.SetAngularVelocity(options.angularVelocity);
this.body.setAngularVelocity(options.angularVelocity);
for(var name in this.limbs) {
this.limbs[name].SetLinearVelocity(options.linearVelocity);
}
};
RubeDoll.prototype.getPosition = function() {
return this.body.GetPosition().Copy();
return this.body.GetPosition().clone();
};
RubeDoll.prototype.getHeadPosition = function() {
return this.limbs.head.GetPosition().Copy();
return this.limbs.head.GetPosition().clone();
};
RubeDoll.prototype.setUpdateData = function(update) {
@ -199,11 +202,11 @@ function (Parent, RubeLoader, Box2D, Settings, Assert, nc, Matrix, RubeDollJson)
Assert.number(update.limbs[name].lv.x, update.limbs[name].lv.y);
Assert.number(update.limbs[name].av);
this.limbs[name].SetAwake(true);
this.limbs[name].setAwake(true);
this.limbs[name].SetPosition(update.limbs[name].p);
this.limbs[name].SetAngle(update.limbs[name].a);
this.limbs[name].SetLinearVelocity(update.limbs[name].lv);
this.limbs[name].SetAngularVelocity(update.limbs[name].av);
this.limbs[name].setAngularVelocity(update.limbs[name].av);
}
*/
}

View file

@ -1,6 +1,6 @@
define([
"Game/" + GLOBALS.context + "/GameObjects/Item",
"Lib/Vendor/Box2D",
"Lib/Vendor/Planck",
"Game/Config/Settings",
"Lib/Utilities/Assert"
],
@ -20,12 +20,11 @@ function (Parent, Box2D, Settings, Assert) {
Assert.number(this.options.width, this.options.height);
Assert.number(this.options.weight);
var deckShape = new Box2D.Collision.Shapes.b2PolygonShape();
var w = this.options.width / Settings.RATIO;
var h = 2 / Settings.RATIO;
deckShape.SetAsOrientedBox(w / 2, h / 2, new Box2D.Common.Math.b2Vec2(0, -(4.5 / Settings.RATIO)));
var deckShape = planck.Box(w / 2, h / 2, planck.Vec2(0, -(4.5 / Settings.RATIO)));
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
var fixtureDef = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fixtureDef.shape = deckShape;
var offset = 4,
@ -36,7 +35,7 @@ function (Parent, Box2D, Settings, Assert) {
fixtureDef.restitution = 0.2;
fixtureDef.isSensor = false;
this.body.CreateFixture(fixtureDef);
this.body.createFixture(fixtureDef);
this.addWheel(
@ -54,18 +53,16 @@ function (Parent, Box2D, Settings, Assert) {
Skateboard.prototype.addWheel = function(x, y) {
Assert.number(x, y);
var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
bodyDef.position.x = x / Settings.RATIO;
bodyDef.position.y = y / Settings.RATIO;
bodyDef.angle = 0;
var bodyDef = {
type: 'dynamic',
position: planck.Vec2(x / Settings.RATIO, y / Settings.RATIO),
angle: 0
};
var wheelShape = new Box2D.Collision.Shapes.b2CircleShape();
wheelShape.SetRadius(2.5 / Settings.RATIO);
wheelShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(x / Settings.RATIO, y / Settings.RATIO));
var wheelShape = planck.Circle(2.5 / Settings.RATIO, planck.Vec2(x / Settings.RATIO, y / Settings.RATIO));
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
var fixtureDef = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
var offset = 4,
factor = 80;
var density = ((0.1 + offset) / 3 / 3) * factor;
@ -75,7 +72,7 @@ function (Parent, Box2D, Settings, Assert) {
fixtureDef.isSensor = false;
fixtureDef.friction = 0.0005;
this.body.CreateFixture(fixtureDef);
this.body.createFixture(fixtureDef);
};
Skateboard.prototype.flip = function(direction) {
@ -90,7 +87,7 @@ function (Parent, Box2D, Settings, Assert) {
/*
define([
"Game/" + GLOBALS.context + "/GameObjects/Item",
"Lib/Vendor/Box2D",
"Lib/Vendor/Planck",
"Game/Config/Settings",
"Lib/Utilities/Assert"
],
@ -121,12 +118,11 @@ function (Parent, Box2D, Settings, Assert) {
Assert.number(this.options.width, this.options.height);
Assert.number(this.options.weight);
var deckShape = new Box2D.Collision.Shapes.b2PolygonShape();
var w = this.options.width / Settings.RATIO;
var h = 1.5 / Settings.RATIO;
deckShape.SetAsOrientedBox(w / 2, h / 2, new Box2D.Common.Math.b2Vec2(0, -(4.5 / Settings.RATIO)));
var deckShape = planck.Box(w / 2, h / 2, planck.Vec2(0, -(4.5 / Settings.RATIO)));
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
var fixtureDef = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fixtureDef.shape = deckShape;
var offset = 4,
@ -137,23 +133,21 @@ function (Parent, Box2D, Settings, Assert) {
fixtureDef.restitution = Settings.ITEM_RESTITUTION;
fixtureDef.isSensor = false;
this.body.CreateFixture(fixtureDef);
this.body.createFixture(fixtureDef);
};
Skateboard.prototype.addWheel = function(x, y) {
Assert.number(x, y);
var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
bodyDef.position.x = x / Settings.RATIO;
bodyDef.position.y = y / Settings.RATIO;
bodyDef.angle = 0;
var bodyDef = {
type: 'dynamic',
position: planck.Vec2(x / Settings.RATIO, y / Settings.RATIO),
angle: 0
};
var wheelShape = new Box2D.Collision.Shapes.b2CircleShape();
wheelShape.SetRadius(1.5 / Settings.RATIO);
wheelShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0, 0));
var wheelShape = planck.Circle(1.5 / Settings.RATIO, planck.Vec2(0, 0));
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
var fixtureDef = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
var offset = 4,
factor = 80;
var density = ((0.1 + offset) / 3 / 3) * factor;
@ -163,7 +157,7 @@ function (Parent, Box2D, Settings, Assert) {
fixtureDef.friction = 0;
var wheelBody = this.body.GetWorld().CreateBody(bodyDef);
wheelBody.CreateFixture(fixtureDef);
wheelBody.createFixture(fixtureDef);
//var revoluteJointDef = new Box2D.Dynamics.Joints.b2RevoluteJointDef();
var revoluteJointDef = new Box2D.Dynamics.Joints.b2WeldJointDef();
@ -171,7 +165,7 @@ function (Parent, Box2D, Settings, Assert) {
revoluteJointDef.Initialize(this.body, wheelBody, wheelBody.GetWorldCenter());
revoluteJointDef.Initialize(this.body, wheelBody, wheelBody.getWorldCenter());
var j = this.body.GetWorld().CreateJoint(revoluteJointDef);