From 875abd60d94d3d5ee3b80e2186390578360d73d0 Mon Sep 17 00:00:00 2001 From: Karl Pannek Date: Wed, 16 Jul 2025 13:28:21 +0200 Subject: [PATCH] Major progress on Planck.js migration - server starts without crashing --- app/Game/Client/GameObjects/Tile.js | 4 +-- app/Game/Core/GameObjects/Doll.js | 30 ++++++++++---------- app/Game/Core/GameObjects/GameObject.js | 12 ++++---- app/Game/Core/GameObjects/Tile.js | 37 +++++++++++++------------ app/Game/Core/Loader/DebugLevel.js | 4 +-- app/Game/Core/Physics/Engine.js | 22 +++++++-------- app/Lib/Vendor/Planck.js | 9 ++++++ 7 files changed, 64 insertions(+), 54 deletions(-) create mode 100644 app/Lib/Vendor/Planck.js diff --git a/app/Game/Client/GameObjects/Tile.js b/app/Game/Client/GameObjects/Tile.js index 1ca9984..4d8c753 100755 --- a/app/Game/Client/GameObjects/Tile.js +++ b/app/Game/Client/GameObjects/Tile.js @@ -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 } ); } diff --git a/app/Game/Core/GameObjects/Doll.js b/app/Game/Core/GameObjects/Doll.js index 32c3e05..2605ba2 100755 --- a/app/Game/Core/GameObjects/Doll.js +++ b/app/Game/Core/GameObjects/Doll.js @@ -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) { diff --git a/app/Game/Core/GameObjects/GameObject.js b/app/Game/Core/GameObjects/GameObject.js index a1eb12d..2aee316 100755 --- a/app/Game/Core/GameObjects/GameObject.js +++ b/app/Game/Core/GameObjects/GameObject.js @@ -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) { diff --git a/app/Game/Core/GameObjects/Tile.js b/app/Game/Core/GameObjects/Tile.js index dee3a1a..9c373e9 100755 --- a/app/Game/Core/GameObjects/Tile.js +++ b/app/Game/Core/GameObjects/Tile.js @@ -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; diff --git a/app/Game/Core/Loader/DebugLevel.js b/app/Game/Core/Loader/DebugLevel.js index 8611553..2d06188 100644 --- a/app/Game/Core/Loader/DebugLevel.js +++ b/app/Game/Core/Loader/DebugLevel.js @@ -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"; diff --git a/app/Game/Core/Physics/Engine.js b/app/Game/Core/Physics/Engine.js index 4ec27b2..06d7e3e 100755 --- a/app/Game/Core/Physics/Engine.js +++ b/app/Game/Core/Physics/Engine.js @@ -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(); } diff --git a/app/Lib/Vendor/Planck.js b/app/Lib/Vendor/Planck.js new file mode 100644 index 0000000..1f42807 --- /dev/null +++ b/app/Lib/Vendor/Planck.js @@ -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; +}); \ No newline at end of file