CANNON is not defined with havok plugin

I get CANNON is not defined when using havok plugin v2.

I follow GitHub - RaananW/babylonjs-webpack-es6: Babylon.js basic scene with typescript, webpack, es6 modules, editorconfig, eslint, hot loading and more. Will even make coffee if you ask nicely. with adaptation to use it with vitejs.
I add @babylonjs/havok package, I don’t use cdn

I miss something ?

I believe there may be an error somewhere in your code. But you may try to use this Vite template

Or create a repro of your error so it would be possible to debug it.

1 Like

As @labris said, there is something in your code. My assumption is that you are trying to load the Cannon plugin instead of the havok plugin (or on top of). The teamplte has examples for havok and ammo, so I am really not sure where things went wrong TBH.

If you want to share your code - it will help us diagnose your issue. Otherwise, @labris’s template is awesome as well :slight_smile:

1 Like

I do

import { HavokPlugin } from "@babylonjs/core/Physics/v2/Plugins/havokPlugin";
import HavokPhysics from "@babylonjs/havok";

let physicsPlugin;
    HavokPhysics().then((havok) => {
        physicsPlugin = new HavokPlugin(true, havok);
    });

const gravity = new Vector3(0, -9.81, 0);
    scene.enablePhysics(gravity, physicsPlugin);

I get issue only on enablePhysics() fn.

My vite config

import { defineConfig } from "vite";

export default defineConfig(({ command, mode }) => {
    return {
        server: {
            fs: {
                // Allow serving files outside of the root
                allow: ["../.."],
            },
        },
        optimizeDeps: { exclude: ["@babylonjs/havok"] },
        resolve: {
            alias: {
                babylonjs:
                    mode === "development"
                        ? "babylonjs/babylon.max"
                        : "babylonjs",
            },
        },
    };
});

I don’t know vite tat well TBH, but correct me if I am wrong - babylonjs as a package should not appear there. you are using @babylonjs/core, and not the UMD package.

But your real issue is asynchronity. You are creating the physics plugin async - as you should. However, you are calling enablePhysics not in the then body, but after. Meaning, physicsPlugin has not yet been initialized, so it is using the default plugin (which is cannon)

Enabling physic into then fix my issue.
Thanks
And will rework vite config to not use UMD package

2 Likes