I cannot migrate a simple example from cannon to cannon-es

Started by
2 comments, last by 8Observer8 2 years, 8 months ago

I have a very simple TypeScript example in cannon that just print a gravity:

import "cannon";

function main()
{
    const physicsWorld = new CANNON.World();
    physicsWorld.gravity.set(0, -9.82, 0);
    console.log("gravity = ", physicsWorld.gravity);
}

main();

You can run it on playground: https://plnkr.co/edit/EoxKFFYZgtXnTTEX?preview

Source code on GitHub: https://github.com/8Observer8/hello-cannon-ts

When I try to change "cannon" to "cannon-es", for example, this line:

import "cannon"

on this line:

import * as CANNON from "cannon-es";

I get this error: Uncaught ReferenceError: exports is not defined and this error: Uncaught TypeError: Cannot read property 'World' of undefined

image
Advertisement

I have no clue about the subject, but

You may want to ask for movement to a programming forum, as “math and physics” is typically about sets of equations or other pure math.

I also find the other error quite interesting. Could it be that ‘World’ was not exported due to that error? (I mean, if it needs an export to make symbols available externally and exports are not defined, you wouldn't be able to access them, would you?)

Member

marcofugaro commented 23 hours ago

It does not work because that's a commonjs module. You need an umd module to make it work with RequireJS.

Cannon-es does not expose an umd module. Please use esmodules directly or alternatively use any bundler.

This answer was here: https://github.com/pmndrs/cannon-es/issues/106#issuecomment-909682690​ I need to try an umd module with RequireJS.​ I think I will continue practice with cannon with TypeScript instead of cannon-es. I tried to use Ammo.js with TypeScript but I have this problem: https://www.gamedev.net/forums/topic/710614-how-to-use-setuserpointergetuserpointer-for-detecting-collisions-with-ammojs/

I created this topic in “Math and Physics” because I thought that some people use cannon-es for Physics with TypeScript and RequireJS. But now I see (from GitHub) that the problem with AMD (Asynchronous Module Definition) and RequireJS. I will try to use UMD.

This topic is closed to new replies.

Advertisement