Pryme8
March 19, 2025, 1:51am
1
There is a port of this by Phaser:
A high-performance, industry-standard 2D physics engine built on Box2D v3, delivering incredibly realistic and engaging web game experiences.
What would be the process for implementing my own physics wrapper for the phaser js port?
I thought there used to be docs on using your own physics engine or creating a custom implementation but I cant find that anywhere now.
1 Like
Cedric
March 19, 2025, 8:56am
2
Implementing your own physics engine was possible for Physics v1 but it’s deprecated now.
The easiest way to integrate a new engine is to make a copy of Babylon.js/packages/dev/core/src/Physics/v2/Plugins/havokPlugin.ts at master · BabylonJS/Babylon.js · GitHub in the same folder, do the proper imports in index.ts
and then replace functions 1 by 1 excluding private ones.
Concepts are similar: rigid bodies, shape, constraint, etc
For example, this method that creates a shape depending in a shape type would be pretty similar with a switch/case but functions starting with HP_Shape_...
would be the Box2D equivalent. Babylon.js/packages/dev/core/src/Physics/v2/Plugins/havokPlugin.ts at 317f0a787323a430784cf0ea0984245a819fcd19 · BabylonJS/Babylon.js · GitHub
2 Likes
Pryme8
March 19, 2025, 2:16pm
3
Oh man, so this is an actual project… sheeeesh ok thanks for the insight.
Pryme8
March 19, 2025, 4:29pm
4
I started making just a wrapper to see how hard this would be.
And its going to be fairly simple actually.
export class PhaserBox2dWrapper{
public get scene(){
return this._scene
}
public world: Ib2World
private _engineRef: AbstractEngine
constructor(private _scene: Scene){
this.world = CreateWorld({worldDef: b2DefaultWorldDef()})
// this.world.locked = false
this._engineRef = this._scene.getEngine()
this._scene.onBeforeRenderObservable.add(()=>{
this.update()
})
}
public update(){
const deltaTime = this._engineRef.getDeltaTime()
WorldStep({worldId: this.world.worldId, deltaTime})
}
public addBodyToNode(node: TransformNode, options:{
type: 'static' | 'dynamic',
position: Vector2,
size: number | Vector2,
restitution: number,
density: number,
friction: number,
}){
const body = CreateBoxPolygon({
position: new b2Vec2(options.position.x, options.position.y),
type: options.type === 'static' ? b2BodyType.b2_staticBody : b2BodyType.b2_dynamicBody,
size: options.size instanceof Vector2 ? new b2Vec2(options.size.x * 0.5, options.size.y * 0.5) : options.size * 0.5,
density: options.density,
friction: options.friction,
worldId: this.world.worldId,
})
this._scene.onBeforeRenderObservable.add(()=>{
const pos = b2Body_GetPosition(body.bodyId)
const rot = b2Body_GetRotation(body.bodyId)
node.position.copyFromFloats(pos.x, pos.y, 0)
node.rotation.z = b2Rot_GetAngle(rot)
})
}
}
Is all it took to support boxes.
2 Likes
Pryme8
March 20, 2025, 12:37am
5
Took about a hour today after work and was able to get most of the core shapes supported along with debug methods.
This is actually way easier than I thought.
1 Like
cx20
March 20, 2025, 12:50am
6
I knew about Box2D Web, but I didn’t know about Phaser Box2D. Thank you for letting me know.
I don’t know if it will be helpful, but here is a sample of Babylon.js + Box2D Web. (Actually, Claude.AI told me about it.)
3 Likes