Why can't Havok Physics work with vite and typescript

I keep getting this error in browser and i’m so drained and lost:

TypeError: this._physicsEngine.removeImpostor is not a function
    at _PhysicsImpostor._init (chunk-DQNGVYKG.js?v=dde2dffd:27786:25)
    at new _PhysicsImpostor (chunk-DQNGVYKG.js?v=dde2dffd:27769:14)
    at createScene (index.ts:33:28)

And this is what i have in my index.ts

import {
  Engine,
  Scene,
  Vector3,
  PhysicsImpostor,
  MeshBuilder,
  UniversalCamera,
} from "@babylonjs/core";
import { HavokPlugin } from "@babylonjs/core/Physics/v2/Plugins/havokPlugin";
import "@babylonjs/loaders/glTF/2.0";
import "@babylonjs/core/Helpers/sceneHelpers";
import HavokPhysics from "@babylonjs/havok";

async function createScene() {
  // Create the engine and scene
  const canvas = document.getElementById(
    "renderCanvas"
  ) as HTMLCanvasElement | null;
  const engine = new Engine(canvas, true);
  const scene = new Scene(engine);

  // Initialize Havok Physics
  const havok = await HavokPhysics();
  const plugin = new HavokPlugin(undefined, havok);
  scene.enablePhysics(new Vector3(0, -9.81, 0), plugin);

  // Create a ground plane
  const ground = MeshBuilder.CreatePlane(
    "ground",
    { width: 10, height: 10 },
    scene
  );
  ground.physicsImpostor = new PhysicsImpostor(
    ground,
    PhysicsImpostor.BoxImpostor,
    { mass: 0 },
    scene
  );

  // Create a box
  const box = MeshBuilder.CreateBox(
    "box",
    { width: 1, height: 1, depth: 1 },
    scene
  );
  box.position.y = 2;
  box.physicsImpostor = new PhysicsImpostor(
    box,
    PhysicsImpostor.BoxImpostor,
    { mass: 1 },
    scene
  );

  // Create a camera
  const camera = new UniversalCamera(
    "camera",
    new Vector3(0, 5, -10),
    scene
  );
  camera.setTarget(Vector3.Zero());
  scene.activeCamera = camera;

  // Start the render loop
  engine.runRenderLoop(() => {
    scene.render();
  });
}

createScene();

Please help a drowning soul :expressionless:

Hello and welcome!

Please have a look how it is done at GitHub - minibao/babylon-vite

Alright, thank you!

1 Like

It uses legacy import to be compatible with the Playground code (with BABYLON. in the beginning), but there is no problem to change it to ES6 for tree-shaking import like in your code example.

Gone through the code in the repo and since it’s not compulsory i use the legacy version, i still can’t point out the error in my code.

i tried modifying my code to some extent to mimick the code in the repo, but still not working.
Is it that the Havok physics can’t work with Babylon using ES6??

No, it works fine :slight_smile:
I believe the error may come a) from some missing import(s); b) from some missing code.
It is hard to tell what is missing just from the part of your code, but check whether you have:

  1. gravity set as Vector3
  2. scene.enablePhysics(gravity, plugin)
1 Like

Here is my fork with Havok / ES6 (from @babylon/core, no tree-shaking) - GitHub - eldinor/babylon-vite

1 Like

Hey @dev-charles15531, I’m using Havok, ES6, and vite. My code also works with Ammo, but my understanding was that PhysicsImpostor was for V1 physics (Ammo, etc), and PhysicsBody and related are for V2 physics (Havok). It’s been a while since I thought about it, so I may be wrong.

For V2 there are PhysicsAggregate instead of PhysicsImpostor.
You may use no Aggregates and PhysicsBody as well - Babylon.js docs

1 Like