Enabling physics in xr Scene

Hi everyone,
I want to add physics in my XR scene using the Havok Plugin, my issue is that I recieve the following error message : Physics not enabled. Please use scene.enablePhysics(…) before creating impostors.

I have my scene, my engine and all my meshes in a separated file
I have my WebXRDefaultExperience object in my main

in my separated file I create my engine, my scene, I initialize HK, the plugIn and the gravity vector as shown in the documentation then I call scene.enablePhysics(gravity, PlugIn) then I export the engine, scene and some meshes.

When I start my app everything goes well (no error message), then I enter in XR mode and I get the error message about enabling Physics. Is there like a new scene that is created when I enter in xr mode ?

const scene = new Scene(engine);
async() => {
	let HavokInstance = await HK();
	let PlugIn = new HavokPlugin(true, HavokInstance);
	let gravity = new Vector3(0, 0, 0);
}

I also tried this way :

const scene = new Scene(engine);
let hk = new HavokPlugin();
scene.enablePhysics(new Vector3(0, 0, 0), hk);

but I get a different error which is :

havokPlugin.js:227 Uncaught ReferenceError: HK is not defined
    at new HavokPlugin (havokPlugin.js:227:61)
    at eval (sNamespace.ts:24:10)
    at ./src/sNamespace.ts (bundleName.js:408:1)
    at __webpack_require__ (bundleName.js:19493:32)
    at fn (bundleName.js:19688:21)
    at eval (app.ts:16:69)
    at ./src/app.ts (bundleName.js:386:1)
    at __webpack_require__ (bundleName.js:19493:32)
    at bundleName.js:20568:37
    at bundleName.js:20570:12

I am not sure about what is going on. Do you guys have an Idea about what could be going on ?
I also tried to initialize the Havok objects in the main but it does not work. My understaning is since I use an async function then javascript ends the execution of the function later so Havok does not get instanciate on time so it is not enabled.

Do someone tried to implement a vr scene that has physics enabled ?

Hi @Karl_JOYEUX
Impostors are part of physics V1. You need to add Aggregates or PhysicsShape/PhysicsBody instead with Havok (Physics V2)
Check this doc pages: https://doc.babylonjs.com/features/featuresDeepDive/physics/migrate

Thanks you just saved me

Sorry to bother again, but I tried using aggregates and now it tells me that No Physics Engine available

Error: No Physics Engine available.
    at new PhysicsBody (physicsBody.js:64:19)
    at new PhysicsAggregate (physicsAggregate.js:63:21)
    at eval (sNamespace.ts:39:17)
    at ./src/sNamespace.ts (bundleName.js:408:1)
    at __webpack_require__ (bundleName.js:19515:32)
    at fn (bundleName.js:19710:21)
    at eval (app.ts:16:69)
    at ./src/app.ts (bundleName.js:386:1)
    at __webpack_require__ (bundleName.js:19515:32)
    at bundleName.js:20590:37

I added an aggregate like this :

let aggregate = new PhysicsAggregate(
	mesh,
	PhysicsShapeType.BOX,
	{ mass: 0 },
	scene
);

Did you add these lines?

// initialize plugin
var hk = new BABYLON.HavokPlugin();
// enable physics in the scene with a gravity
scene.enablePhysics(new BABYLON.Vector3(0, -9.8, 0), hk);

yes I did, I think I got the solution

I think I have to call WebXRDefault Experience after ‘scene’ instead of calling it before.

1 Like