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.mesh,
{
x: this.body.GetPosition().x * Settings.RATIO,
y: this.body.GetPosition().y * Settings.RATIO
x: this.body.getPosition().x * Settings.RATIO,
y: this.body.getPosition().y * Settings.RATIO
}
);
}

View file

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

View file

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

View file

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

View file

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

View file

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