Can't initialize havok physics

import { Scene, Engine, MeshBuilder, PhysicsImpostor, Vector3 } from '@babylonjs/core';
import { PhysicsEngine, HavokPlugin } from '@babylonjs/core/Physics';
import HavokPhysics from '@babylonjs/havok';

async function createScene() {
  // Create the engine and scene
  const canvas = document.getElementById('renderCanvas');
  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 BABYLON.FreeCamera("camera", new BABYLON.Vector3(0, 5, -10), scene);
  camera.setTarget(BABYLON.Vector3.Zero());
  scene.activeCamera = camera;

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

createScene();

cc @eoin / @Cedric

The “found 3c 21 64 6f” translates to “<!do” which looks like the start of a doctype html5 - make sure the URL you’re loading the WASM from is correct, and is serving up the WASM file and not a 404 or someting.

1 Like

i don’t load any wasm file myself. it’s all done by havok. and i like to state that i’m using vite

This probably relates on how vite is loading Havok cc @RaananW would might have worked it out already ?

Here is the Vite template with Havok - GitHub - minibao/babylon-vite

1 Like

Can you check if HavokPhysics.wasm is generated under ./dist/assets/ after vite build?

Add the following configuration in vite.config.ts, this might solve the issue.

// vite.config.ts
{
  // ...
  optimizeDeps: {
      exclude: ['@babylonjs/havok']
    }
}
1 Like

no wasm file in vite folder. even asset folder isn’t exist. the only wasm file i could find is in @babylonjs/havok/lib/esm/HavokPhysics.wasm

oh. that’s the point. thanks. i just configured vite incorrectly.

1 Like