Cannot set property 'LoaderInjectedPhysicsEngine' of undefined

Hey there!

Let me start with a disclosure: I only started learning JavaScript few weeks ago, the error I encountered might be due to shortcomings in my understanding of JS and / Babylon. Be gentle.

I’m trying to get Cannonjs to play with Babylonjs. As far as I understand (see this thread) this should work:

import { BabylonFileLoaderConfiguration } from “babylonjs”;
import * as foo from “cannon”;

BabylonFileLoaderConfiguration.LoaderInjectedPhysicsEngine = foo;

Unfortunately, I’m only getting Cannot set property 'LoaderInjectedPhysicsEngine' of undefined.

The full source code is split across couple of files, and I’m not clever enough (yet!) to make it work with the playground. I uploaded class producing the above error to pastebin here.

Hi @cypherpunk Welcome to the forum!

I’m still unfamiliar with modules/imports but @sebavan is the best in the world for helping you with such issue!

@sebavan can correct me if I’m wrong, but I think this should work. What version of ‘babylonjs’ are you using? The BabylonFileLoaderConfiguration wasn’t added until about 4 months ago.

The code should defiinitely work :slight_smile:

It looks like babylonjs has not been installed in NPM or is using an older version.

You should try npm install babylonjs@preview to see if that fixes it ?

Hey everyone and thanks for the warm welcome!

Both cannon and babylonjs are freshly installed via npm yesterday:

"babylonjs": "^4.0.3",
"cannon": "^0.6.2"

I’m very much new to the world of JS, and Babylonjs I only discovered few days ago - more than likely it is error on my side. I’m following Isaias Pomales tutorial which can be found here. My repo - which results in the said error - can be found here.

As I couldn’t figure out where I did mistake, I tried using @babylonjs/core and loading cannon via webpack - works exactly as advertised!

What makes me think that the mistake here is mine is the weird physics on the working version. I isolated the issue to the physics being applied in the ‘ground’ class. It might be related to the Cannot set property 'LoaderInjectedPhysicsEngine' of undefined error but honestly, I don’t know enough (yet!) to be sure of that. The working version using @babylonjs/core lives in this branch, and I think that the offending file is Ground.js.

If someone could take a look at that file and tell me where did I messed up I’ll be grateful. This thing is driving me up the wall :slight_smile:

Sure thing! Give me few moments…

(few moments later)

Without any changes to the master branch and using "babylonjs": "^4.1.0-beta.23":

Uncaught ReferenceError: CANNON is not defined

…it bloody has to be a mistake on my side then. I wager that there’s something wrong with the Ground.js.

What can I do to further narrow it down? I’m out of ideas (and lack on skills…) :frowning:

Could you share your project on Github so that we can all take a look ?

Sure, links were in my previous replay:

master - this gives me Cannot set property 'LoaderInjectedPhysicsEngine' of undefined

branch - works with @babylonjs/core and cannon loaded via webpack… but the physics is all weird.

Ok so let s fix master, is that normal you are still on the older version ?

Older version? @babylonjs/core is at 4.0.3 - same as what I can see via npm web interface? And same goes for cannon, 0.6.2.

If you mean why I’m not using beta releases, well, I prefer to stay stable as my JS skills are severely lacking.

But LoaderInjectedPhysicsEngine is not available in 4.0.3, you need to use preview or wait a couple weeks for the next stable :slight_smile:

Ha! That would explain why I can’t get it to work :man_facepalming:

Thanks for taking you time to check out the repo!

Can I be cheeky? Could you have a look at the sort-of-working branch of the same repo? I got cannon to work there with @babylonjs/core via webpack. But… the physics itself is like something out of a Lovecraft novel :slight_smile:

I think that the issue there is with how the PhysicsImpostor is being created in Ground.js - but I can’t figure out how to fix it.