Migrate Box2D to Planck.js in core game logic, items, debug draw, and menu. Remove legacy Box2D references, update level and item loading, and improve debug draw for Planck.

This commit is contained in:
Karl Pannek 2025-07-17 18:50:16 +02:00
parent 799601f24d
commit da6e9a244b
15 changed files with 66 additions and 201 deletions

View file

@ -117,7 +117,7 @@ function (Parent, Exception, planck, Settings, CollisionDetector, Item, nc, Asse
shape: planck.Box(
r / 2 / R,
(h / 2 + r / 4) / R,
planck.Vec2(sign * r / 2 / R, h / 2 / R)
planck.Vec2(sign * r / 2 / R, 0)
),
density: 0,
friction: 0,
@ -133,7 +133,7 @@ function (Parent, Exception, planck, Settings, CollisionDetector, Item, nc, Asse
// Area sensor
addFixture({
shape: planck.Box((w + a) / 2 / R, (h + a) / 2 / R, planck.Vec2(0, h / 2 / R)),
shape: planck.Box((w + a) / 2 / R, (h + a) / 2 / R, planck.Vec2(0, 0)),
density: 0,
friction: 0,
restitution: 0,
@ -319,15 +319,20 @@ function (Parent, Exception, planck, Settings, CollisionDetector, Item, nc, Asse
Assert.number(this.lookDirection);
var handPosition = planck.Vec2(
bodyPosition.x + ((this.width / 2 / Settings.RATIO) * this.lookDirection),
bodyPosition.y - this.height / 4 * 2 / Settings.RATIO // 2/3 of the body height
bodyPosition.y - 0 / Settings.RATIO // 2/3 of the body height
);
this.holdingItem.reposition(handPosition, this.lookDirection);
var jointDef = new Box2D.Dynamics.Joints.b2WeldJointDef();
jointDef.initialize(this.body, this.holdingItem.body, this.holdingItem.getGrabPoint());
this.holdingJoint = this.body.getWorld().createJoint(jointDef);
// Planck.js WeldJoint
var jointDef = {
bodyA: this.body,
bodyB: this.holdingItem.body,
localAnchorA: this.body.getLocalPoint(this.holdingItem.getGrabPoint()),
localAnchorB: this.holdingItem.body.getLocalPoint(this.holdingItem.getGrabPoint()),
referenceAngle: 0
};
this.holdingJoint = this.body.getWorld().createJoint(planck.WeldJoint(jointDef));
}
};

View file

@ -8,7 +8,7 @@ define([
"Lib/Utilities/Assert"
],
function (Parent, Box2D, optionsHelper, Settings, Exception, nc, Assert) {
function (Parent, planck, optionsHelper, Settings, Exception, nc, Assert) {
"use strict";
@ -35,10 +35,10 @@ function (Parent, Box2D, optionsHelper, Settings, Exception, nc, Assert) {
Parent.call(this, physicsEngine, uid);
this.createFixture();
this.body.ResetMassData();
this.body.resetMassData();
this.flipDirection = 1;
if (this.body.getMass() < 1) {
this.body.SetBullet(true);
this.body.setBullet(true);
}
nc.trigger(nc.ns.core.game.worldUpdateObjects.add, this);
@ -124,8 +124,8 @@ function (Parent, Box2D, optionsHelper, Settings, Exception, nc, Assert) {
handPosition.x + ((this.options.width / Settings.RATIO / 2) * direction),
handPosition.y
);
this.body.SetPosition(position);
this.body.SetAngle((this.options.grabAngle || 0.0) * direction);
this.body.setPosition(position);
this.body.setAngle((this.options.grabAngle || 0.0) * direction);
this.flip(direction);
};
@ -148,7 +148,7 @@ function (Parent, Box2D, optionsHelper, Settings, Exception, nc, Assert) {
var x = options.x * Settings.MAX_THROW_FORCE / this.options.weight + carrierVelocity.x;
var y = -options.y * Settings.MAX_THROW_FORCE / this.options.weight + carrierVelocity.y;
var vector = planck.Vec2(x, y);
body.SetLinearVelocity(vector);
body.setLinearVelocity(vector);
var av = -options.av * Settings.MAX_THROW_ANGULAR_VELOCITY;
body.setAngularVelocity(av);

View file

@ -8,7 +8,7 @@ define([
"Game/Config/ItemSettings",
],
function (Parent, Box2D, Settings, nc, Assert, optionsHelper, ItemSettings) {
function (Parent, planck, Settings, nc, Assert, optionsHelper, ItemSettings) {
"use strict";

View file

@ -4,7 +4,7 @@ define([
"Game/Config/Settings"
],
function (Parent, Box2D, Settings) {
function (Parent, planck, Settings) {
"use strict";

View file

@ -9,7 +9,7 @@ define([
// "json!Game/Asset/RubeDoll.json" // Temporarily disabled during Planck.js migration
],
function (Parent, /* RubeLoader, */ Box2D, Settings, Assert, nc, Matrix /* , RubeDollJson */) {
function (Parent, /* RubeLoader, */ planck, Settings, Assert, nc, Matrix /* , RubeDollJson */) {
"use strict";

View file

@ -5,7 +5,7 @@ define([
"Lib/Utilities/Assert"
],
function (Parent, Box2D, Settings, Assert) {
function (Parent, planck, Settings, Assert) {
"use strict";
@ -159,15 +159,7 @@ function (Parent, Box2D, Settings, Assert) {
var wheelBody = this.body.getWorld().createBody(bodyDef);
wheelBody.createFixture(fixtureDef);
//var revoluteJointDef = new Box2D.Dynamics.Joints.b2RevoluteJointDef();
var revoluteJointDef = new Box2D.Dynamics.Joints.b2WeldJointDef();
//revoluteJointDef.enableMotor = false;
revoluteJointDef.initialize(this.body, wheelBody, wheelBody.getWorldCenter());
var j = this.body.getWorld().createJoint(revoluteJointDef);
// See top of file for Planck.js joint creation reference.
// FIXME this means, that we will have bodies in the world, which must not be
// updated (wheels) because they are always connected to a body which will be updated.

View file

@ -3,7 +3,7 @@ define([
"Lib/Vendor/Planck"
],
function (Parent, Box2D) {
function (Parent, planck) {
"use strict";