Major progress on Planck.js migration - server starts without crashing

This commit is contained in:
Karl Pannek 2025-07-16 13:28:21 +02:00
parent 74957807e2
commit 875abd60d9
7 changed files with 64 additions and 54 deletions

View file

@ -60,8 +60,8 @@ function (Parent, Settings, nc, Layer) {
this.layerId, this.layerId,
this.mesh, this.mesh,
{ {
x: this.body.GetPosition().x * Settings.RATIO, x: this.body.getPosition().x * Settings.RATIO,
y: this.body.GetPosition().y * Settings.RATIO y: this.body.getPosition().y * Settings.RATIO
} }
); );
} }

View file

@ -1,7 +1,7 @@
define([ define([
"Game/" + GLOBALS.context + "/GameObjects/GameObject", "Game/" + GLOBALS.context + "/GameObjects/GameObject",
"Lib/Utilities/Exception", "Lib/Utilities/Exception",
"Lib/Vendor/Box2D", "Lib/Vendor/Planck",
"Game/Config/Settings", "Game/Config/Settings",
"Game/" + GLOBALS.context + "/Collision/Detector", "Game/" + GLOBALS.context + "/Collision/Detector",
"Game/" + GLOBALS.context + "/GameObjects/Item", "Game/" + GLOBALS.context + "/GameObjects/Item",
@ -9,7 +9,7 @@ define([
"Lib/Utilities/Assert" "Lib/Utilities/Assert"
], ],
function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, nc, Assert) { function (Parent, Exception, planck, Settings, CollisionDetector, Item, nc, Assert) {
"use strict"; "use strict";
@ -51,12 +51,12 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, nc, Asser
Doll.prototype = Object.create(Parent.prototype); Doll.prototype = Object.create(Parent.prototype);
Doll.prototype.getBodyDef = function() { Doll.prototype.getBodyDef = function() {
var bodyDef = new Box2D.Dynamics.b2BodyDef(); var bodyDef = {
bodyDef.position.x = 0; position: planck.Vec2(0, 0),
bodyDef.position.y = 0; fixedRotation: true,
bodyDef.fixedRotation = true; linearDamping: Settings.PLAYER_LINEAR_DAMPING,
bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING; type: 'dynamic'
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; };
return bodyDef; return bodyDef;
}; };
@ -83,7 +83,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, nc, Asser
onCollisionChange: this.onImpact.bind(this) onCollisionChange: this.onImpact.bind(this)
}; };
this.body.CreateFixture(fixtureDef); this.body.createFixture(fixtureDef);
var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape(); var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape();
bodyShape.SetAsOrientedBox( bodyShape.SetAsOrientedBox(
@ -93,7 +93,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, nc, Asser
); );
fixtureDef.shape = bodyShape; fixtureDef.shape = bodyShape;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
this.body.CreateFixture(fixtureDef); this.body.createFixture(fixtureDef);
var legsShape = new Box2D.Collision.Shapes.b2CircleShape(); var legsShape = new Box2D.Collision.Shapes.b2CircleShape();
legsShape.SetRadius(this.width / 2 / Settings.RATIO); legsShape.SetRadius(this.width / 2 / Settings.RATIO);
@ -102,7 +102,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, nc, Asser
fixtureDef.friction = Settings.PLAYER_FRICTION; fixtureDef.friction = Settings.PLAYER_FRICTION;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
this.legs = this.body.CreateFixture(fixtureDef); this.legs = this.body.createFixture(fixtureDef);
fixtureDef.density = 0; fixtureDef.density = 0;
@ -116,7 +116,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, nc, Asser
onCollisionChange: this.onFootSensorDetection.bind(this) onCollisionChange: this.onFootSensorDetection.bind(this)
}; };
this.footSensor = this.body.CreateFixture(fixtureDef); this.footSensor = this.body.createFixture(fixtureDef);
var grabSensorLeftShape = new Box2D.Collision.Shapes.b2PolygonShape(); var grabSensorLeftShape = new Box2D.Collision.Shapes.b2PolygonShape();
grabSensorLeftShape.SetAsOrientedBox( grabSensorLeftShape.SetAsOrientedBox(
@ -134,7 +134,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, nc, Asser
self.onFixtureWithinReach(isColliding, "left", fixture); self.onFixtureWithinReach(isColliding, "left", fixture);
} }
}; };
this.body.CreateFixture(fixtureDef); this.body.createFixture(fixtureDef);
var grabSensorRightShape = new Box2D.Collision.Shapes.b2PolygonShape(); var grabSensorRightShape = new Box2D.Collision.Shapes.b2PolygonShape();
grabSensorRightShape.SetAsOrientedBox( grabSensorRightShape.SetAsOrientedBox(
@ -154,7 +154,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, nc, Asser
} }
}; };
this.body.CreateFixture(fixtureDef); this.body.createFixture(fixtureDef);
// Area Sensor // Area Sensor
var areaSensorShape = new Box2D.Collision.Shapes.b2PolygonShape(); var areaSensorShape = new Box2D.Collision.Shapes.b2PolygonShape();
@ -188,7 +188,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, nc, Asser
} }
}; };
this.body.CreateFixture(fixtureDef); this.body.createFixture(fixtureDef);
}; };
Doll.prototype.setActionState = function(state) { Doll.prototype.setActionState = function(state) {

View file

@ -1,11 +1,11 @@
define([ define([
"Lib/Vendor/Box2D", "Lib/Vendor/Planck",
"Lib/Utilities/Exception", "Lib/Utilities/Exception",
"Lib/Utilities/Assert", "Lib/Utilities/Assert",
"Lib/Utilities/NotificationCenter" "Lib/Utilities/NotificationCenter"
], ],
function (Box2D, Exception, Assert, nc) { function (planck, Exception, Assert, nc) {
"use strict"; "use strict";
@ -27,8 +27,8 @@ function (Box2D, Exception, Assert, nc) {
GameObject.prototype.destroy = function() { GameObject.prototype.destroy = function() {
if(this.body instanceof Box2D.Dynamics.b2Body) { if(this.body) {
this.body.GetWorld().DestroyBody(this.body); this.body.getWorld().destroyBody(this.body);
} else { } else {
throw new Exception("can not destroy body"); throw new Exception("can not destroy body");
} }
@ -40,8 +40,8 @@ function (Box2D, Exception, Assert, nc) {
return this.body; return this.body;
}; };
GameObject.prototype.getPosition = function() { GameObject.prototype.getPosition = function() {
return this.body.GetPosition().Copy(); return this.body.getPosition().clone();
}; };
GameObject.prototype.setUpdateData = function(update) { GameObject.prototype.setUpdateData = function(update) {

View file

@ -1,13 +1,13 @@
define([ define([
"Game/" + GLOBALS.context + "/GameObjects/GameObject", "Game/" + GLOBALS.context + "/GameObjects/GameObject",
"Lib/Vendor/Box2D", "Lib/Vendor/Planck",
"Game/Config/Settings", "Game/Config/Settings",
"Lib/Utilities/Exception", "Lib/Utilities/Exception",
"Lib/Utilities/NotificationCenter", "Lib/Utilities/NotificationCenter",
"Lib/Utilities/Assert" "Lib/Utilities/Assert"
], ],
function (Parent, Box2D, Settings, Exception, nc, Assert) { function (Parent, planck, Settings, Exception, nc, Assert) {
"use strict"; "use strict";
@ -23,27 +23,30 @@ function (Parent, Box2D, Settings, Exception, nc, Assert) {
Assert.number(this.options.x, this.options.y); Assert.number(this.options.x, this.options.y);
Assert.number(this.options.r); Assert.number(this.options.r);
var bodyDef = new Box2D.Dynamics.b2BodyDef(); var bodyDef = {
bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody; type: 'static',
bodyDef.position.x = (this.options.x * Settings.TILE_SIZE + Settings.TILE_SIZE / 2) / Settings.RATIO; position: planck.Vec2(
bodyDef.position.y = (this.options.y * Settings.TILE_SIZE + Settings.TILE_SIZE / 2) / Settings.RATIO; (this.options.x * Settings.TILE_SIZE + Settings.TILE_SIZE / 2) / Settings.RATIO,
bodyDef.angle = (this.options.r || 0) * 90 * Math.PI / 180; (this.options.y * Settings.TILE_SIZE + Settings.TILE_SIZE / 2) / Settings.RATIO
),
angle: (this.options.r || 0) * 90 * Math.PI / 180
};
return bodyDef; return bodyDef;
}; };
Tile.prototype.createPhysicTile = function (tile) { Tile.prototype.createPhysicTile = function (tile) {
var vertices = this.createVertices(tile); var vertices = this.createVertices(tile);
var tileShape = new Box2D.Collision.Shapes.b2PolygonShape(); var tileShape = planck.Polygon(vertices);
tileShape.SetAsArray(vertices, vertices.length);
var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); var fixtureDef = {
fixtureDef.shape = tileShape; shape: tileShape,
fixtureDef.density = 0; density: 0,
fixtureDef.friction = Settings.TILE_FRICTION; friction: Settings.TILE_FRICTION,
fixtureDef.restitution = Settings.TILE_RESTITUTION; restitution: Settings.TILE_RESTITUTION,
fixtureDef.isSensor = false; isSensor: false
this.body.CreateFixture(fixtureDef); };
this.body.createFixture(fixtureDef);
}; };
Tile.prototype.createVertices = function (tile) { Tile.prototype.createVertices = function (tile) {
@ -115,7 +118,7 @@ function (Parent, Box2D, Settings, Exception, nc, Assert) {
}; };
Tile.prototype.addVec = function (vs, m1, m2) { Tile.prototype.addVec = function (vs, m1, m2) {
return vs.push(new Box2D.Common.Math.b2Vec2(this.mkArg(m1), this.mkArg(m2))); return vs.push(planck.Vec2(this.mkArg(m1), this.mkArg(m2)));
}; };
return Tile; return Tile;

View file

@ -1,10 +1,10 @@
define([ define([
"Game/Config/Settings", "Game/Config/Settings",
"Lib/Vendor/Box2D", "Lib/Vendor/Planck",
"Lib/Utilities/NotificationCenter", "Lib/Utilities/NotificationCenter",
"Lib/Utilities/Abstract", "Lib/Utilities/Abstract",
"Game/" + GLOBALS.context + "/GameObjects/Tile" "Game/" + GLOBALS.context + "/GameObjects/Tile"
], function (Settings, Box2D, nc, Abstract, Tile) { ], function (Settings, planck, nc, Abstract, Tile) {
"use strict"; "use strict";

View file

@ -1,20 +1,18 @@
define([ define([
"Game/Config/Settings", "Game/Config/Settings",
"Lib/Vendor/Box2D", "Lib/Vendor/Planck",
"Game/" + GLOBALS.context + "/Collision/Detector", "Game/" + GLOBALS.context + "/Collision/Detector",
"Lib/Utilities/NotificationCenter" "Lib/Utilities/NotificationCenter"
], ],
function (Settings, Box2D, CollisionDetector, nc) { function (Settings, planck, CollisionDetector, nc) {
"use strict"; "use strict";
function Engine () { function Engine () {
this.world = new Box2D.Dynamics.b2World( this.world = planck.World({
new Box2D.Common.Math.b2Vec2(0, Settings.BOX2D_GRAVITY), gravity: planck.Vec2(0, Settings.BOX2D_GRAVITY)
Settings.BOX2D_ALLOW_SLEEP });
);
this.world.SetWarmStarting(true);
this.lastStep = Date.now(); this.lastStep = Date.now();
this.worldQueue = []; this.worldQueue = [];
@ -26,7 +24,7 @@ function (Settings, Box2D, CollisionDetector, nc) {
Engine.prototype.setCollisionDetector = function () { Engine.prototype.setCollisionDetector = function () {
var detector = new CollisionDetector(); var detector = new CollisionDetector();
this.world.SetContactListener(detector.getListener()); this.world.on('begin-contact', detector.getListener());
} }
Engine.prototype.getWorldForRubeLoader = function() { Engine.prototype.getWorldForRubeLoader = function() {
@ -34,11 +32,11 @@ function (Settings, Box2D, CollisionDetector, nc) {
}; };
Engine.prototype.createBody = function (bodyDef) { Engine.prototype.createBody = function (bodyDef) {
return this.world.CreateBody(bodyDef); return this.world.createBody(bodyDef);
} }
Engine.prototype.destroyBody = function (body) { Engine.prototype.destroyBody = function (body) {
return this.world.DestroyBody(body); return this.world.destroyBody(body);
} }
Engine.prototype.addToWorldQueue = function(callback) { Engine.prototype.addToWorldQueue = function(callback) {
@ -55,9 +53,9 @@ function (Settings, Box2D, CollisionDetector, nc) {
Engine.prototype.update = function () { Engine.prototype.update = function () {
var stepLength = (Date.now() - this.lastStep) / 1000; var stepLength = (Date.now() - this.lastStep) / 1000;
this.world.Step(stepLength, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS); this.world.step(stepLength, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS);
this.lastStep = Date.now(); this.lastStep = Date.now();
this.world.ClearForces(); this.world.clearForces();
this.processWorldQueue(); this.processWorldQueue();
} }

9
app/Lib/Vendor/Planck.js vendored Normal file
View file

@ -0,0 +1,9 @@
define([
"Lib/Vendor/planck.min"
], function(planck) {
"use strict";
// planck.min.js exports the planck object through AMD
// Return it for RequireJS module system
return planck;
});