Restore RubeDoll functionality with Planck.js migration. Update RubeLoader to use Planck.js, fix import aliases, and enable RubeDoll spawning on player death.

This commit is contained in:
Karl Pannek 2025-07-18 22:06:58 +02:00
parent da6e9a244b
commit 162a4ab82d
3 changed files with 135 additions and 232 deletions

View file

@ -6,7 +6,7 @@ define([
"Lib/Utilities/Assert" "Lib/Utilities/Assert"
], ],
function (Parent, Item, Box2D, nc, Assert) { function (Parent, Item, planck, nc, Assert) {
"use strict"; "use strict";

View file

@ -1,25 +1,21 @@
define([ define([
"Game/" + GLOBALS.context + "/GameObjects/Item", "Game/" + GLOBALS.context + "/GameObjects/Item",
// "Lib/Vendor/RubeLoader", // Temporarily disabled during Planck.js migration "Lib/Vendor/RubeLoader", // Re-enabled for Planck.js
"Lib/Vendor/Planck", "Lib/Vendor/Planck",
"Game/Config/Settings", "Game/Config/Settings",
"Lib/Utilities/Assert", "Lib/Utilities/Assert",
"Lib/Utilities/NotificationCenter", "Lib/Utilities/NotificationCenter",
"Lib/Utilities/Matrix" "Lib/Utilities/Matrix",
// "json!Game/Asset/RubeDoll.json" // Temporarily disabled during Planck.js migration "json!Game/Asset/RubeDoll.json" // Re-enabled for Planck.js
], ],
function (Parent, /* RubeLoader, */ planck, Settings, Assert, nc, Matrix /* , RubeDollJson */) { function (Parent, RubeLoader, planck, Settings, Assert, nc, Matrix, RubeDollJson) {
"use strict"; "use strict";
function RubeDoll(physicsEngine, uid, options) { function RubeDoll(physicsEngine, uid, options) {
Assert.number(options.x, options.y); 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.rubeLoader = null;
this.body = null; this.body = null;
this.limbs = {}; this.limbs = {};
@ -27,15 +23,15 @@ function (Parent, /* RubeLoader, */ planck, Settings, Assert, nc, Matrix /* , Ru
this.limits = []; this.limits = [];
var chest = null; var chest = null;
// this.rubeLoader = new RubeLoader(RubeDollJson, physicsEngine.getWorldForRubeLoader()); this.rubeLoader = new RubeLoader(RubeDollJson, physicsEngine.getWorldForRubeLoader());
// this.loadRubeDollFromScene(options); this.loadRubeDollFromScene(options);
Parent.call(this, physicsEngine, uid, options); Parent.call(this, physicsEngine, uid, options);
physicsEngine.destroyBody(this.body); physicsEngine.destroyBody(this.body);
this.body = this.limbs.chest; this.body = this.limbs.chest;
delete this.limbs.chest; delete this.limbs.chest;
this.body.SetUserData(this); this.body.setUserData(this);
this.flip(options.direction || 1); this.flip(options.direction || 1);
} }
@ -45,24 +41,22 @@ function (Parent, /* RubeLoader, */ planck, Settings, Assert, nc, Matrix /* , Ru
RubeDoll.prototype.loadRubeDollFromScene = function(options) { RubeDoll.prototype.loadRubeDollFromScene = function(options) {
var scene = this.rubeLoader.getScene(); var scene = this.rubeLoader.getScene();
for (var i in scene.bodies) { for (var i in scene.bodies) {
var body = scene.bodies[i]; var body = scene.bodies[i];
var position = body.GetPosition().clone(); var position = body.getPosition().clone();
position.Add(planck.Vec2( position.add(planck.Vec2(
options.x / Settings.RATIO, options.x / Settings.RATIO,
options.y / Settings.RATIO options.y / Settings.RATIO
)); ));
body.SetPosition(position); body.setPosition(position);
this.limbs[body.name] = body; this.limbs[body.name] = body;
// code snipped possibly needed for filtering between doll and rubedoll while holding // code snipped possibly needed for filtering between doll and rubedoll while holding
//var filterData = new Box2D.Dynamics.b2FilterData(); //var filterData = new planck.Filter();
//filterData.groupIndex = -66; //filterData.groupIndex = -66;
//if(body.name != "head" && body.name != "chest") { //if(body.name != "head" && body.name != "chest") {
// for (var fixture = body.getFixtureList(); fixture; fixture = fixture.getNext()) { // for (var fixture = body.getFixtureList(); fixture; fixture = fixture.getNext()) {
// fixture.SetFilterData(filterData); // fixture.setFilterData(filterData);
// } // }
//} //}
} }
@ -72,13 +66,13 @@ function (Parent, /* RubeLoader, */ planck, Settings, Assert, nc, Matrix /* , Ru
var count = 0; var count = 0;
for (var i in this.joints) { for (var i in this.joints) {
this.limits[i] = { this.limits[i] = {
lower: this.joints[i].GetLowerLimit(), lower: this.joints[i].getLowerLimit(),
upper: this.joints[i].GetUpperLimit(), upper: this.joints[i].getUpperLimit(),
}; };
/* /*
this.joints[i].EnableLimit(false); this.joints[i].enableLimit(false);
if(count < 4 && this.joints[i] instanceof Box2D.Dynamics.Joints.b2RevoluteJoint) { if(count < 4 && this.joints[i] instanceof planck.RevoluteJoint) {
console.log(i); console.log(i);
} else { } else {
body.getWorld().destroyJoint(this.joints[i]); body.getWorld().destroyJoint(this.joints[i]);
@ -90,7 +84,7 @@ function (Parent, /* RubeLoader, */ planck, Settings, Assert, nc, Matrix /* , Ru
RubeDoll.prototype.getFixtureDef = function() { RubeDoll.prototype.getFixtureDef = function() {
var fixtureDef = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false }; var fixtureDef = { shape: null, density: 1.0, friction: 0.3, restitution: 0.0, isSensor: false };
fixtureDef.shape = new Box2D.Collision.Shapes.b2CircleShape(); fixtureDef.shape = planck.Circle(0.1); // Small circle as placeholder
return fixtureDef; return fixtureDef;
}; };
@ -105,10 +99,10 @@ function (Parent, /* RubeLoader, */ planck, Settings, Assert, nc, Matrix /* , Ru
var joint = this.joints[i]; var joint = this.joints[i];
var limits = this.limits[i]; var limits = this.limits[i];
if (joint instanceof Box2D.Dynamics.Joints.b2RevoluteJoint) { if (joint instanceof planck.RevoluteJoint) {
if (direction > 0) { if (direction > 0) {
joint.SetLimits(limits.lower, limits.upper); joint.setLimits(limits.lower, limits.upper);
continue; continue;
} }
@ -116,12 +110,12 @@ function (Parent, /* RubeLoader, */ planck, Settings, Assert, nc, Matrix /* , Ru
var a2 = limits.upper * -1; var a2 = limits.upper * -1;
if (a2 > a1) { if (a2 > a1) {
joint.SetLimits(a1, a2); joint.setLimits(a1, a2);
} else { } else {
joint.SetLimits(a2, a1); joint.setLimits(a2, a1);
} }
// joint.SetAngle(joint.GetAngle() * -1); // joint.setAngle(joint.getAngle() * -1);
} }
} }
} }
@ -129,46 +123,46 @@ function (Parent, /* RubeLoader, */ planck, Settings, Assert, nc, Matrix /* , Ru
RubeDoll.prototype.reposition = function(handPosition, direction) { RubeDoll.prototype.reposition = function(handPosition, direction) {
var oldPosition = this.getPosition(); var oldPosition = this.getPosition();
var oldAngle = this.body.GetAngle(); var oldAngle = this.body.getAngle();
var oldDirection = this.flipDirection; var oldDirection = this.flipDirection;
// calls flip() at the end of Parent reposition() // calls flip() at the end of Parent reposition()
Parent.prototype.reposition.call(this, handPosition, direction); Parent.prototype.reposition.call(this, handPosition, direction);
var differenceAngle = oldAngle - this.body.GetAngle(); var differenceAngle = oldAngle - this.body.getAngle();
//this.body.SetLinearVelocity(planck.Vec2(0, 0)); //this.body.setLinearVelocity(planck.Vec2(0, 0));
var offset = Box2D.Common.Math.b2Math.SubtractVV(this.getPosition(), oldPosition); var offset = planck.Vec2(this.getPosition()).sub(planck.Vec2(oldPosition));
var grabAngle = (this.options.grabAngle || 0.001); var grabAngle = (this.options.grabAngle || 0.001);
for(var key in this.limbs) { for(var key in this.limbs) {
var limb = this.limbs[key]; var limb = this.limbs[key];
// Setting position offset first (floor to hand) // Setting position offset first (floor to hand)
var position = limb.GetPosition().clone(); var position = limb.getPosition().clone();
position.Add(offset); position.add(offset);
limb.SetPosition(position); limb.setPosition(position);
// grabing local point to "rotate" around (x, y position transform only) // grabing local point to "rotate" around (x, y position transform only)
var localPoint = this.body.getLocalPoint(limb.GetPosition().clone()); var localPoint = this.body.getLocalPoint(limb.getPosition().clone());
// create rotation matrix from chest rotation difference // create rotation matrix from chest rotation difference
var mat = Box2D.Common.Math.b2Mat22.FromAngle(differenceAngle); var mat = planck.Mat22.fromAngle(differenceAngle);
// matrix multiplication with local limb position // matrix multiplication with local limb position
position = Box2D.Common.Math.b2Math.MulTMV(mat, localPoint); position = mat.mulTV(localPoint);
// translating back to global position // translating back to global position
var globalPoint = this.body.getWorldPoint(position); var globalPoint = this.body.getWorldPoint(position);
limb.SetPosition(globalPoint); limb.setPosition(globalPoint);
// relative limb rotating by chest rotation difference // relative limb rotating by chest rotation difference
var d = (oldDirection == direction) ? -1 : 1; var d = (oldDirection == direction) ? -1 : 1;
limb.SetAngle((limb.GetAngle() - differenceAngle) * d); limb.setAngle((limb.getAngle() - differenceAngle) * d);
//limb.SetType('static'); //limb.setType('static');
//limb.SetLinearVelocity(planck.Vec2(0, 0)); //limb.setLinearVelocity(planck.Vec2(0, 0));
} }
}; };
@ -176,26 +170,25 @@ function (Parent, /* RubeLoader, */ planck, Settings, Assert, nc, Matrix /* , Ru
Assert.number(options.linearVelocity.x, options.linearVelocity.y); Assert.number(options.linearVelocity.x, options.linearVelocity.y);
Assert.number(options.angularVelocity); Assert.number(options.angularVelocity);
this.body.SetLinearVelocity(options.linearVelocity); this.body.setLinearVelocity(options.linearVelocity);
this.body.setAngularVelocity(options.angularVelocity); this.body.setAngularVelocity(options.angularVelocity);
for(var name in this.limbs) { for(var name in this.limbs) {
this.limbs[name].SetLinearVelocity(options.linearVelocity); this.limbs[name].setLinearVelocity(options.linearVelocity);
} }
}; };
RubeDoll.prototype.getPosition = function() { RubeDoll.prototype.getPosition = function() {
return this.body.GetPosition().clone(); return this.body.getPosition().clone();
}; };
RubeDoll.prototype.getHeadPosition = function() { RubeDoll.prototype.getHeadPosition = function() {
return this.limbs.head.GetPosition().clone(); return this.limbs.head.getPosition().clone();
}; };
RubeDoll.prototype.setUpdateData = function(update) { RubeDoll.prototype.setUpdateData = function(update) {
Parent.prototype.setUpdateData.call(this, update); Parent.prototype.setUpdateData.call(this, update);
/*
for(var name in update.limbs) { for(var name in update.limbs) {
Assert.number(update.limbs[name].p.x, update.limbs[name].p.y); Assert.number(update.limbs[name].p.x, update.limbs[name].p.y);
Assert.number(update.limbs[name].a); Assert.number(update.limbs[name].a);
@ -203,12 +196,11 @@ function (Parent, /* RubeLoader, */ planck, Settings, Assert, nc, Matrix /* , Ru
Assert.number(update.limbs[name].av); 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].setPosition(update.limbs[name].p);
this.limbs[name].SetAngle(update.limbs[name].a); this.limbs[name].setAngle(update.limbs[name].a);
this.limbs[name].SetLinearVelocity(update.limbs[name].lv); this.limbs[name].setLinearVelocity(update.limbs[name].lv);
this.limbs[name].setAngularVelocity(update.limbs[name].av); this.limbs[name].setAngularVelocity(update.limbs[name].av);
} }
*/
} }
RubeDoll.prototype.destroy = function() { RubeDoll.prototype.destroy = function() {

View file

@ -5,114 +5,27 @@ define([
/* /*
List of what has been done here List of what has been done here
- enclose in require.js style class - enclose in require.js style class
- added box2d var names - added planck var names
- inversed y coordinates with body positions, polygon coordinates and in getVectorValue for joints - inversed y coordinates with body positions, polygon coordinates and in getVectorValue for joints
- migrated from Box2D to Planck.js
*/ */
function (Box2D) { function (planck) {
var b2Color = Box2D.Common.b2Color, // Planck.js equivalents for Box2D classes
b2internal = Box2D.Common.b2internal, var Vec2 = planck.Vec2,
b2Settings = Box2D.Common.b2Settings, Body = planck.Body,
b2CircleShape = Box2D.Collision.Shapes.b2CircleShape, Fixture = planck.Fixture,
b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef, Circle = planck.Circle,
b2EdgeShape = Box2D.Collision.Shapes.b2EdgeShape, Polygon = planck.Polygon,
b2MassData = Box2D.Collision.Shapes.b2MassData, Edge = planck.Edge,
b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape, Chain = planck.Chain,
b2Shape = Box2D.Collision.Shapes.b2Shape, RevoluteJoint = planck.RevoluteJoint,
b2Mat22 = Box2D.Common.Math.b2Mat22, DistanceJoint = planck.DistanceJoint,
b2Mat33 = Box2D.Common.Math.b2Mat33, PrismaticJoint = planck.PrismaticJoint,
b2Math = Box2D.Common.Math.b2Math, FrictionJoint = planck.FrictionJoint,
b2Sweep = Box2D.Common.Math.b2Sweep, WeldJoint = planck.WeldJoint;
b2Transform = Box2D.Common.Math.b2Transform,
b2Vec2 = Box2D.Common.Math.b2Vec2,
b2Vec3 = Box2D.Common.Math.b2Vec3,
b2Body = Box2D.Dynamics.b2Body,
b2BodyDef = Box2D.Dynamics.b2BodyDef,
b2ContactFilter = Box2D.Dynamics.b2ContactFilter,
b2ContactImpulse = Box2D.Dynamics.b2ContactImpulse,
b2ContactListener = Box2D.Dynamics.b2ContactListener,
b2ContactManager = Box2D.Dynamics.b2ContactManager,
b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
b2DestructionListener = Box2D.Dynamics.b2DestructionListener,
b2FilterData = Box2D.Dynamics.b2FilterData,
b2Fixture = Box2D.Dynamics.b2Fixture,
b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
b2Island = Box2D.Dynamics.b2Island,
b2TimeStep = Box2D.Dynamics.b2TimeStep,
b2World = Box2D.Dynamics.b2World,
b2AABB = Box2D.Collision.b2AABB,
b2Bound = Box2D.Collision.b2Bound,
b2BoundValues = Box2D.Collision.b2BoundValues,
b2Collision = Box2D.Collision.b2Collision,
b2ContactID = Box2D.Collision.b2ContactID,
b2ContactPoint = Box2D.Collision.b2ContactPoint,
b2Distance = Box2D.Collision.b2Distance,
b2DistanceInput = Box2D.Collision.b2DistanceInput,
b2DistanceOutput = Box2D.Collision.b2DistanceOutput,
b2DistanceProxy = Box2D.Collision.b2DistanceProxy,
b2DynamicTree = Box2D.Collision.b2DynamicTree,
b2DynamicTreeBroadPhase = Box2D.Collision.b2DynamicTreeBroadPhase,
b2DynamicTreeNode = Box2D.Collision.b2DynamicTreeNode,
b2DynamicTreePair = Box2D.Collision.b2DynamicTreePair,
b2Manifold = Box2D.Collision.b2Manifold,
b2ManifoldPoint = Box2D.Collision.b2ManifoldPoint,
b2Point = Box2D.Collision.b2Point,
b2RayCastInput = Box2D.Collision.b2RayCastInput,
b2RayCastOutput = Box2D.Collision.b2RayCastOutput,
b2Segment = Box2D.Collision.b2Segment,
b2SeparationFunction = Box2D.Collision.b2SeparationFunction,
b2Simplex = Box2D.Collision.b2Simplex,
b2SimplexCache = Box2D.Collision.b2SimplexCache,
b2SimplexVertex = Box2D.Collision.b2SimplexVertex,
b2TimeOfImpact = Box2D.Collision.b2TimeOfImpact,
b2TOIInput = Box2D.Collision.b2TOIInput,
b2WorldManifold = Box2D.Collision.b2WorldManifold,
ClipVertex = Box2D.Collision.ClipVertex,
Features = Box2D.Collision.Features,
IBroadPhase = Box2D.Collision.IBroadPhase;
b2_dynamicBody = 'dynamic';
b2ControllerEdge = Box2D.Dynamics.Controllers.b2ControllerEdge,
IBroadPhase = Box2D.Collision.IBroadPhase,
b2CircleContact = Box2D.Dynamics.Contacts.b2CircleContact,
b2Contact = Box2D.Dynamics.Contacts.b2Contact,
b2ContactConstraint = Box2D.Dynamics.Contacts.b2ContactConstraint,
b2ContactConstraintPoint = Box2D.Dynamics.Contacts.b2ContactConstraintPoint,
b2ContactEdge = Box2D.Dynamics.Contacts.b2ContactEdge,
b2ContactFactory = Box2D.Dynamics.Contacts.b2ContactFactory,
b2ContactRegister = Box2D.Dynamics.Contacts.b2ContactRegister,
b2ContactResult = Box2D.Dynamics.Contacts.b2ContactResult,
b2ContactSolver = Box2D.Dynamics.Contacts.b2ContactSolver,
b2EdgeAndCircleContact = Box2D.Dynamics.Contacts.b2EdgeAndCircleContact,
b2NullContact = Box2D.Dynamics.Contacts.b2NullContact,
b2PolyAndCircleContact = Box2D.Dynamics.Contacts.b2PolyAndCircleContact,
b2PolyAndEdgeContact = Box2D.Dynamics.Contacts.b2PolyAndEdgeContact,
b2PolygonContact = Box2D.Dynamics.Contacts.b2PolygonContact,
b2PositionSolverManifold = Box2D.Dynamics.Contacts.b2PositionSolverManifold,
b2Controller = Box2D.Dynamics.Controllers.b2Controller,
b2DistanceJoint = Box2D.Dynamics.Joints.b2DistanceJoint,
b2DistanceJointDef = Box2D.Dynamics.Joints.b2DistanceJointDef,
b2FrictionJoint = Box2D.Dynamics.Joints.b2FrictionJoint,
b2FrictionJointDef = Box2D.Dynamics.Joints.b2FrictionJointDef,
b2GearJoint = Box2D.Dynamics.Joints.b2GearJoint,
b2GearJointDef = Box2D.Dynamics.Joints.b2GearJointDef,
b2Jacobian = Box2D.Dynamics.Joints.b2Jacobian,
b2Joint = Box2D.Dynamics.Joints.b2Joint,
b2JointDef = Box2D.Dynamics.Joints.b2JointDef,
b2JointEdge = Box2D.Dynamics.Joints.b2JointEdge,
b2LineJoint = Box2D.Dynamics.Joints.b2LineJoint,
b2LineJointDef = Box2D.Dynamics.Joints.b2LineJointDef,
b2MouseJoint = Box2D.Dynamics.Joints.b2MouseJoint,
b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef,
b2PrismaticJoint = Box2D.Dynamics.Joints.b2PrismaticJoint,
b2PrismaticJointDef = Box2D.Dynamics.Joints.b2PrismaticJointDef,
b2PulleyJoint = Box2D.Dynamics.Joints.b2PulleyJoint,
b2PulleyJointDef = Box2D.Dynamics.Joints.b2PulleyJointDef,
b2RevoluteJoint = Box2D.Dynamics.Joints.b2RevoluteJoint,
b2RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef,
b2WeldJoint = Box2D.Dynamics.Joints.b2WeldJoint,
b2WeldJointDef = Box2D.Dynamics.Joints.b2WeldJointDef;
function RubeLoader(json, world) { function RubeLoader(json, world) {
@ -136,28 +49,28 @@ function (Box2D) {
return null; return null;
} }
var bd = new b2BodyDef(); var bodyDef = {};
if ( bodyJson.type == 2 ) if ( bodyJson.type == 2 )
bd.type = b2_dynamicBody; bodyDef.type = 'dynamic';
else if ( bodyJson.type == 1 ) else if ( bodyJson.type == 1 )
bd.type = b2_kinematicBody; bodyDef.type = 'kinematic';
if ( bodyJson.hasOwnProperty('angle') ) if ( bodyJson.hasOwnProperty('angle') )
bd.angle = bodyJson.angle; bodyDef.angle = bodyJson.angle;
if ( bodyJson.hasOwnProperty('angularVelocity') ) if ( bodyJson.hasOwnProperty('angularVelocity') )
bd.angularVelocity = bodyJson.angularVelocity; bodyDef.angularVelocity = bodyJson.angularVelocity;
if ( bodyJson.hasOwnProperty('active') ) if ( bodyJson.hasOwnProperty('active') )
bd.awake = bodyJson.active; bodyDef.awake = bodyJson.active;
if ( bodyJson.hasOwnProperty('fixedRotation') ) if ( bodyJson.hasOwnProperty('fixedRotation') )
bd.fixedRotation = bodyJson.fixedRotation; bodyDef.fixedRotation = bodyJson.fixedRotation;
if ( bodyJson.hasOwnProperty('linearVelocity') && bodyJson.linearVelocity instanceof Object ) if ( bodyJson.hasOwnProperty('linearVelocity') && bodyJson.linearVelocity instanceof Object )
bd.linearVelocity.SetV( bodyJson.linearVelocity ); bodyDef.linearVelocity = Vec2(bodyJson.linearVelocity.x, bodyJson.linearVelocity.y);
if ( bodyJson.hasOwnProperty('position') && bodyJson.position instanceof Object ) if ( bodyJson.hasOwnProperty('position') && bodyJson.position instanceof Object )
bd.position.SetV( this.getVectorValue(bodyJson.position) ); bodyDef.position = this.getVectorValue(bodyJson.position);
if ( bodyJson.hasOwnProperty('awake') ) if ( bodyJson.hasOwnProperty('awake') )
bd.awake = bodyJson.awake; bodyDef.awake = bodyJson.awake;
else else
bd.awake = false; bodyDef.awake = false;
var body = world.CreateBody(bd); var body = world.createBody(bodyDef);
if ( bodyJson.hasOwnProperty('fixture') ) { if ( bodyJson.hasOwnProperty('fixture') ) {
for (k = 0; k < bodyJson['fixture'].length; k++) { for (k = 0; k < bodyJson['fixture'].length; k++) {
var fixtureJson = bodyJson['fixture'][k]; var fixtureJson = bodyJson['fixture'][k];
@ -174,56 +87,50 @@ function (Box2D) {
RubeLoader.prototype.loadFixtureFromRUBE = function (body, fixtureJson) { RubeLoader.prototype.loadFixtureFromRUBE = function (body, fixtureJson) {
//console.log(fixtureJson); //console.log(fixtureJson);
var fd = new b2FixtureDef(); var fixtureDef = {};
if (fixtureJson.hasOwnProperty('friction')) if(fixtureJson.hasOwnProperty('friction'))
fd.friction = fixtureJson.friction; fixtureDef.friction = fixtureJson.friction;
if (fixtureJson.hasOwnProperty('density')) if(fixtureJson.hasOwnProperty('density'))
fd.density = fixtureJson.density; fixtureDef.density = fixtureJson.density;
if (fixtureJson.hasOwnProperty('restitution')) if(fixtureJson.hasOwnProperty('restitution'))
fd.restitution = fixtureJson.restitution; fixtureDef.restitution = fixtureJson.restitution;
if (fixtureJson.hasOwnProperty('sensor')) if(fixtureJson.hasOwnProperty('sensor'))
fd.isSensor = fixtureJson.sensor; fixtureDef.isSensor = fixtureJson.sensor;
if ( fixtureJson.hasOwnProperty('filter-categoryBits') ) if ( fixtureJson.hasOwnProperty('filter-categoryBits') )
fd.filter.categoryBits = fixtureJson['filter-categoryBits']; fixtureDef.filterCategoryBits = fixtureJson['filter-categoryBits'];
if ( fixtureJson.hasOwnProperty('filter-maskBits') ) if ( fixtureJson.hasOwnProperty('filter-maskBits') )
fd.filter.maskBits = fixtureJson['filter-maskBits']; fixtureDef.filterMaskBits = fixtureJson['filter-maskBits'];
if ( fixtureJson.hasOwnProperty('filter-groupIndex') ) if ( fixtureJson.hasOwnProperty('filter-groupIndex') )
fd.filter.groupIndex = fixtureJson['filter-groupIndex']; fixtureDef.filterGroupIndex = fixtureJson['filter-groupIndex'];
if (fixtureJson.hasOwnProperty('circle')) { if(fixtureJson.hasOwnProperty('circle')) {
fd.shape = new b2CircleShape(); fixtureDef.shape = Circle(fixtureJson.circle.radius);
fd.shape.m_radius = fixtureJson.circle.radius;
if ( fixtureJson.circle.center ) if ( fixtureJson.circle.center )
fd.shape.m_p.SetV(fixtureJson.circle.center); fixtureDef.shape.m_center = Vec2(fixtureJson.circle.center.x, fixtureJson.circle.center.y);
var fixture = body.createFixture(fd); var fixture = body.createFixture(fixtureDef);
if ( fixtureJson.name ) if ( fixture && fixtureJson.name )
fixture.name = fixtureJson.name; fixture.name = fixtureJson.name;
} }
else if (fixtureJson.hasOwnProperty('polygon')) { else if (fixtureJson.hasOwnProperty('polygon')) {
fd.shape = new b2PolygonShape();
var verts = []; var verts = [];
for (v = fixtureJson.polygon.vertices.x.length - 1; v >= 0 ; v--) for (v = fixtureJson.polygon.vertices.x.length - 1; v >= 0 ; v--)
verts.push( new b2Vec2( fixtureJson.polygon.vertices.x[v], -fixtureJson.polygon.vertices.y[v] ) ); verts.push( Vec2( fixtureJson.polygon.vertices.x[v], -fixtureJson.polygon.vertices.y[v] ) );
fd.shape.SetAsArray(verts, verts.length); fixtureDef.shape = Polygon(verts);
var fixture = body.createFixture(fd); var fixture = body.createFixture(fixtureDef);
if ( fixture && fixtureJson.name ) if ( fixture && fixtureJson.name )
fixture.name = fixtureJson.name; fixture.name = fixtureJson.name;
} }
else if (fixtureJson.hasOwnProperty('chain')) { else if (fixtureJson.hasOwnProperty('chain')) {
fd.shape = new b2PolygonShape(); var verts = [];
var lastVertex = new b2Vec2();
for (v = fixtureJson.chain.vertices.x.length - 1; v >= 0; v--) { for (v = fixtureJson.chain.vertices.x.length - 1; v >= 0; v--) {
var thisVertex = new b2Vec2( fixtureJson.chain.vertices.x[v], -fixtureJson.chain.vertices.y[v] ); var thisVertex = Vec2( fixtureJson.chain.vertices.x[v], -fixtureJson.chain.vertices.y[v] );
if ( v < fixtureJson.chain.vertices.x.length - 1 ) { verts.push(thisVertex);
fd.shape.SetAsEdge( lastVertex, thisVertex ); }
var fixture = body.createFixture(fd); fixtureDef.shape = Chain(verts);
var fixture = body.createFixture(fixtureDef);
if ( fixtureJson.name ) if ( fixtureJson.name )
fixture.name = fixtureJson.name; fixture.name = fixtureJson.name;
} }
lastVertex = thisVertex;
}
}
else { else {
console.log("Could not find shape type for fixture"); console.log("Could not find shape type for fixture");
} }
@ -231,17 +138,17 @@ function (Box2D) {
RubeLoader.prototype.getVectorValue = function (val) { RubeLoader.prototype.getVectorValue = function (val) {
if ( val instanceof Object ) { if ( val instanceof Object ) {
return { x: val.x, y: val.y * -1 }; return Vec2(val.x, val.y * -1);
} else { } else {
return { x:0, y:0 }; return Vec2(0, 0);
} }
} }
RubeLoader.prototype.loadJointCommonProperties = function (jd, jointJson, loadedBodies) { RubeLoader.prototype.loadJointCommonProperties = function (jd, jointJson, loadedBodies) {
jd.bodyA = loadedBodies[jointJson.bodyA]; jd.bodyA = loadedBodies[jointJson.bodyA];
jd.bodyB = loadedBodies[jointJson.bodyB]; jd.bodyB = loadedBodies[jointJson.bodyB];
jd.localAnchorA.SetV( this.getVectorValue(jointJson.anchorA) ); jd.localAnchorA = this.getVectorValue(jointJson.anchorA);
jd.localAnchorB.SetV( this.getVectorValue(jointJson.anchorB) ); jd.localAnchorB = this.getVectorValue(jointJson.anchorB);
if ( jointJson.collideConnected ) if ( jointJson.collideConnected )
jd.collideConnected = jointJson.collideConnected; jd.collideConnected = jointJson.collideConnected;
} }
@ -263,7 +170,7 @@ function (Box2D) {
var joint = null; var joint = null;
if ( jointJson.type == "revolute" ) { if ( jointJson.type == "revolute" ) {
var jd = new b2RevoluteJointDef(); var jd = {};
this.loadJointCommonProperties(jd, jointJson, loadedBodies); this.loadJointCommonProperties(jd, jointJson, loadedBodies);
if ( jointJson.hasOwnProperty('refAngle') ) if ( jointJson.hasOwnProperty('refAngle') )
jd.referenceAngle = jointJson.refAngle; jd.referenceAngle = jointJson.refAngle;
@ -279,12 +186,12 @@ function (Box2D) {
jd.enableLimit = jointJson.enableLimit; jd.enableLimit = jointJson.enableLimit;
if ( jointJson.hasOwnProperty('enableMotor') ) if ( jointJson.hasOwnProperty('enableMotor') )
jd.enableMotor = jointJson.enableMotor; jd.enableMotor = jointJson.enableMotor;
joint = world.CreateJoint(jd); joint = world.createJoint(RevoluteJoint(jd));
} }
else if ( jointJson.type == "distance" || jointJson.type == "rope" ) { else if ( jointJson.type == "distance" || jointJson.type == "rope" ) {
if ( jointJson.type == "rope" ) if ( jointJson.type == "rope" )
console.log("Replacing unsupported rope joint with distance joint!"); console.log("Replacing unsupported rope joint with distance joint!");
var jd = new b2DistanceJointDef(); var jd = {};
this.loadJointCommonProperties(jd, jointJson, loadedBodies); this.loadJointCommonProperties(jd, jointJson, loadedBodies);
if ( jointJson.hasOwnProperty('length') ) if ( jointJson.hasOwnProperty('length') )
jd.length = jointJson.length; jd.length = jointJson.length;
@ -292,13 +199,13 @@ function (Box2D) {
jd.dampingRatio = jointJson.dampingRatio; jd.dampingRatio = jointJson.dampingRatio;
if ( jointJson.hasOwnProperty('frequency') ) if ( jointJson.hasOwnProperty('frequency') )
jd.frequencyHz = jointJson.frequency; jd.frequencyHz = jointJson.frequency;
joint = world.CreateJoint(jd); joint = world.createJoint(DistanceJoint(jd));
} }
else if ( jointJson.type == "prismatic" ) { else if ( jointJson.type == "prismatic" ) {
var jd = new b2PrismaticJointDef(); var jd = {};
this.loadJointCommonProperties(jd, jointJson, loadedBodies); this.loadJointCommonProperties(jd, jointJson, loadedBodies);
if ( jointJson.hasOwnProperty('localAxisA') ) if ( jointJson.hasOwnProperty('localAxisA') )
jd.localAxisA.SetV( this.getVectorValue(jointJson.localAxisA) ); jd.localAxisA = this.getVectorValue(jointJson.localAxisA);
if ( jointJson.hasOwnProperty('refAngle') ) if ( jointJson.hasOwnProperty('refAngle') )
jd.referenceAngle = jointJson.refAngle; jd.referenceAngle = jointJson.refAngle;
if ( jointJson.hasOwnProperty('enableLimit') ) if ( jointJson.hasOwnProperty('enableLimit') )
@ -313,44 +220,44 @@ function (Box2D) {
jd.maxMotorForce = jointJson.maxMotorForce; jd.maxMotorForce = jointJson.maxMotorForce;
if ( jointJson.hasOwnProperty('motorSpeed') ) if ( jointJson.hasOwnProperty('motorSpeed') )
jd.motorSpeed = jointJson.motorSpeed; jd.motorSpeed = jointJson.motorSpeed;
joint = world.CreateJoint(jd); joint = world.createJoint(PrismaticJoint(jd));
} }
else if ( jointJson.type == "wheel" ) { else if ( jointJson.type == "wheel" ) {
//Make a fake wheel joint using a line joint and a distance joint. //Make a fake wheel joint using a distance joint and a prismatic joint.
//Return the line joint because it has the linear motor controls. //Return the prismatic joint because it has the linear motor controls.
//Use ApplyTorque on the bodies to spin the wheel... //Use ApplyTorque on the bodies to spin the wheel...
var jd = new b2DistanceJointDef(); var jd = {};
this.loadJointCommonProperties(jd, jointJson, loadedBodies); this.loadJointCommonProperties(jd, jointJson, loadedBodies);
jd.length = 0.0; jd.length = 0.0;
if ( jointJson.hasOwnProperty('springDampingRatio') ) if ( jointJson.hasOwnProperty('springDampingRatio') )
jd.dampingRatio = jointJson.springDampingRatio; jd.dampingRatio = jointJson.springDampingRatio;
if ( jointJson.hasOwnProperty('springFrequency') ) if ( jointJson.hasOwnProperty('springFrequency') )
jd.frequencyHz = jointJson.springFrequency; jd.frequencyHz = jointJson.springFrequency;
world.CreateJoint(jd); world.createJoint(DistanceJoint(jd));
jd = new b2LineJointDef(); jd = {};
this.loadJointCommonProperties(jd, jointJson, loadedBodies); this.loadJointCommonProperties(jd, jointJson, loadedBodies);
if ( jointJson.hasOwnProperty('localAxisA') ) if ( jointJson.hasOwnProperty('localAxisA') )
jd.localAxisA.SetV( this.getVectorValue(jointJson.localAxisA) ); jd.localAxisA = this.getVectorValue(jointJson.localAxisA);
joint = world.CreateJoint(jd); joint = world.createJoint(PrismaticJoint(jd));
} }
else if ( jointJson.type == "friction" ) { else if ( jointJson.type == "friction" ) {
var jd = new b2FrictionJointDef(); var jd = {};
this.loadJointCommonProperties(jd, jointJson, loadedBodies); this.loadJointCommonProperties(jd, jointJson, loadedBodies);
if ( jointJson.hasOwnProperty('maxForce') ) if ( jointJson.hasOwnProperty('maxForce') )
jd.maxForce = jointJson.maxForce; jd.maxForce = jointJson.maxForce;
if ( jointJson.hasOwnProperty('maxTorque') ) if ( jointJson.hasOwnProperty('maxTorque') )
jd.maxTorque = jointJson.maxTorque; jd.maxTorque = jointJson.maxTorque;
joint = world.CreateJoint(jd); joint = world.createJoint(FrictionJoint(jd));
} }
else if ( jointJson.type == "weld" ) { else if ( jointJson.type == "weld" ) {
var jd = new b2WeldJointDef(); var jd = {};
this.loadJointCommonProperties(jd, jointJson, loadedBodies); this.loadJointCommonProperties(jd, jointJson, loadedBodies);
if ( jointJson.hasOwnProperty('referenceAngle') ) if ( jointJson.hasOwnProperty('referenceAngle') )
jd.referenceAngle = jointJson.referenceAngle; jd.referenceAngle = jointJson.referenceAngle;
joint = world.CreateJoint(jd); joint = world.createJoint(WeldJoint(jd));
} }
else { else {
console.log("Unsupported joint type: " + jointJson.type); console.log("Unsupported joint type: " + jointJson.type);
@ -381,8 +288,7 @@ function (Box2D) {
else else
image.body = null; image.body = null;
image.center = new b2Vec2(); image.center = this.getVectorValue(imageJson.center);
image.center.SetV( this.getVectorValue(imageJson.center) );
return image; return image;
} }
@ -434,10 +340,10 @@ function (Box2D) {
//create a world variable and return it if loading succeeds //create a world variable and return it if loading succeeds
RubeLoader.prototype.loadWorldFromRUBE = function (worldJson) { RubeLoader.prototype.loadWorldFromRUBE = function (worldJson) {
var gravity = new b2Vec2(0,0); var gravity = Vec2(0,0);
if ( worldJson.hasOwnProperty('gravity') && worldJson.gravity instanceof Object ) if ( worldJson.hasOwnProperty('gravity') && worldJson.gravity instanceof Object )
gravity.SetV( worldJson.gravity ); gravity = Vec2(worldJson.gravity.x, worldJson.gravity.y);
var world = new b2World( gravity ); var world = planck.World({ gravity: gravity });
if ( ! this.loadSceneIntoWorld(worldJson, world) ) if ( ! this.loadSceneIntoWorld(worldJson, world) )
return false; return false;
return world; return world;
@ -445,7 +351,8 @@ function (Box2D) {
RubeLoader.prototype.getNamedBodies = function (world, name) { RubeLoader.prototype.getNamedBodies = function (world, name) {
var bodies = []; var bodies = [];
for (b = world.m_bodyList; b; b = b.m_next) { var bodyList = world.getBodyList();
for (var b = bodyList; b; b = b.getNext()) {
if ( b.name == name ) if ( b.name == name )
bodies.push(b); bodies.push(b);
} }
@ -454,8 +361,10 @@ function (Box2D) {
RubeLoader.prototype.getNamedFixtures = function (world, name) { RubeLoader.prototype.getNamedFixtures = function (world, name) {
var fixtures = []; var fixtures = [];
for (b = world.m_bodyList; b; b = b.m_next) { var bodyList = world.getBodyList();
for (f = b.m_fixtureList; f; f = f.m_next) { for (var b = bodyList; b; b = b.getNext()) {
var fixtureList = b.getFixtureList();
for (var f = fixtureList; f; f = f.getNext()) {
if ( f.name == name ) if ( f.name == name )
fixtures.push(f); fixtures.push(f);
} }
@ -465,7 +374,8 @@ function (Box2D) {
RubeLoader.prototype.getNamedJoints = function (world, name) { RubeLoader.prototype.getNamedJoints = function (world, name) {
var joints = []; var joints = [];
for (j = world.m_jointList; j; j = j.m_next) { var jointList = world.getJointList();
for (var j = jointList; j; j = j.getNext()) {
if ( j.name == name ) if ( j.name == name )
joints.push(j); joints.push(j);
} }
@ -484,7 +394,8 @@ function (Box2D) {
//custom properties //custom properties
RubeLoader.prototype.getBodiesByCustomProperty = function (world, propertyType, propertyName, valueToMatch) { RubeLoader.prototype.getBodiesByCustomProperty = function (world, propertyType, propertyName, valueToMatch) {
var bodies = []; var bodies = [];
for (b = world.m_bodyList; b; b = b.m_next) { var bodyList = world.getBodyList();
for (var b = bodyList; b; b = b.getNext()) {
if ( ! b.hasOwnProperty('customProperties') ) if ( ! b.hasOwnProperty('customProperties') )
continue; continue;
for (var i = 0; i < b.customProperties.length; i++) { for (var i = 0; i < b.customProperties.length; i++) {