Ammo with BabylonJS & Angular

You have 2 choices:

  1. you use ammojs-typed and change your file like this:
....

import Ammo from 'ammojs-typed'

...

	protected addPhysicsToScene(scene: Scene, sphere: any, ground: any): void {
		Ammo().then((ammo: any) => {
			scene.enablePhysics(new Vector3(0, -9.81, 0), new AmmoJSPlugin(true, ammo));

			sphere.physicsImpostor = new PhysicsImpostor(
				sphere,
				PhysicsImpostor.BoxImpostor,
				{
					mass: 1,
					friction: 0.1,
					restitution: 0.1,
				},
				this.scene
			);
	
			// Move the sphere upward 1/2 its height
			sphere.position.y = 5;

			ground.physicsImpostor = new PhysicsImpostor(
				ground,
				PhysicsImpostor.BoxImpostor,
				{
					mass: 0,
					friction: 0.1,
					restitution: 0.1,
				},
				this.scene
			);
		});
	}

	public createScene(): void {
...
		
		this.addPhysicsToScene(this.scene, sphere, ground);

		// this.scene.registerBeforeRender(() => {});
	}

...
}

This is still pretty bad as you should await the promise and not only fire and forget but at least it will work.

The second solution is to rely on ammo directly and in this case from solution 1. you only change:

import Ammo from 'ammojs-typed' to import * as Ammo from 'ammo.js'; but you need to add a typings file for it in your project like in app you create a ammo.d.ts file containing:

declare module 'ammo.js';

You would basically lose ammo typings except if you manually create them in the d.ts file but will be on latest.

2 Likes