Property 'enablePhysics' does not exist on type 'Scene'

I’ve got this working before in another app, but I’m trying to start again, and this is driving me nuts. I can’t see anything different between the old, working implementation and my attempt at a new one.

I’m using the Ionic framework, so attempting ES6 importing, and am getting:

error TS2339: Property ‘enablePhysics’ does not exist on type ‘Scene’.

These are the packages I’m using:

"@angular/common": "~10.0.0",
"@angular/core": "~10.0.0",
"@angular/forms": "~10.0.0",
"@angular/platform-browser": "~10.0.0",
"@angular/platform-browser-dynamic": "~10.0.0",
"@angular/router": "~10.0.0",
"@babylonjs/core": "^4.2.0",
"@capacitor/core": "2.4.3",
"@ionic-native/core": "^5.0.0",
"@ionic-native/splash-screen": "^5.0.0",
"@ionic-native/status-bar": "^5.0.0",
"@ionic/angular": "^5.0.0",
"ammojs-typed": "^1.0.6",
"rxjs": "~6.5.5",
"stats.js": "^0.17.0",
"tslib": "^2.0.0",
"zone.js": "~0.10.3"

Here’s how I’m importing:

import { Engine } from ‘@babylonjs/core/Engines/engine’;
import { Scene } from ‘@babylonjs/core/scene’;
import { AmmoJSPlugin } from ‘@babylonjs/core/Physics/Plugins/ammoJSPlugin’;
import Ammo from ‘ammojs-typed’
import { Vector3 } from ‘@babylonjs/core/Maths’;

And here’s the code:

this.canvas = document.querySelector('canvas');
this.engine = new Engine(this.canvas, true, {preserveDrawingBuffer: true, stencil: true,
  deterministicLockstep: true, lockstepMaxSteps: 4});
this.engine.setSize(window.innerWidth, window.innerHeight);
window.addEventListener('resize', () => {
    this.engine.resize();
});
this.scene = new Scene(this.engine);
return Ammo().then((Ammo: any) => {
  var ammoPlugin = new AmmoJSPlugin(true, Ammo);
  this.scene.enablePhysics(new Vector3(0, -9.81, 0), ammoPlugin);
  ammoPlugin.setTimeStep(1/30);
}).catch((reason) => {
  console.log(`Failed to initialise Ammo.js. Reason: ${reason}`);
});

I think you need to import a side-effect, as stated in the docs for ES6 imports usage (https://doc.babylonjs.com/divingDeeper/developWithBjs/treeShaking):

For enabling physics on the scene you need import "@babylonjs\core\Physics\physicsEngineComponent" to populate the scene.enablePhysics function.

1 Like

You’re my saviour. I swear I searched that page for “enablePhysics”, but either I didn’t or I simply skipped past that bit. Adding:

import “@babylonjs/core/Physics/physicsEngineComponent”;

fixed it.

I checked and my old app doesn’t use that import statement; I guess one of the other imports happened to side-effect import the necessary stuff.

1 Like