Birch-san / box2d-wasm

Box2D physics engine compiled to WebAssembly. Supports TypeScript and ES modules.
263 stars 21 forks source link

Cannot construct a b2Draw, no constructor in IDL #55

Closed 8Observer8 closed 1 year ago

8Observer8 commented 1 year ago

I try to create an instance of b2Draw class:

import { box2d } from "./init-box2d.js";

export default class DebugDrawer {
    constructor() {
        this.debugDrawer = box2d.b2Draw();
    }
}

But I have this error: Uncaught (in promise) cannot construct a b2Draw, no constructor in IDL

My example on Playground: https://plnkr.co/edit/Y8seZUXNtgv6uTUS

Full code of the example:

index.html

<!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>
    <canvas id="renderCanvas" width="400" height="400"></canvas>

    <!-- Since import maps are not yet supported by all browsers, it is
        necessary to add the polyfill es-module-shims.js
        Source: https://threejs.org/docs/index.html#manual/en/introduction/Installation
    -->
    <script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>

    <script type="importmap">
        {
            "imports": {
                "gl-matrix": "https://cdn.skypack.dev/gl-matrix@3.4.3",
                "box2d": "https://8observer8.github.io/lib/box2d-wasm-2.4.1/box2d-wasm-2.4.1.min.js"
            }
        }
    </script>

    <script type="module" src="./js/main.js"></script>
</body>

</html>

js/debug-drawer.js

import { box2d } from "./init-box2d.js";

export default class DebugDrawer {
    constructor() {
        this.debugDrawer = box2d.b2Draw();
    }
}

js/init-box2d.js

import Box2DLib from "box2d";

export let box2d = null;

export function initBox2D() {
    return new Promise(resolve => {
        Box2DLib().then((re) => {
            box2d = re;
            resolve();
        });
    });
}

js/main.js

import { box2d, initBox2D } from "./init-box2d.js";
import DebugDrawer from "./debug-drawer.js";

async function init() {
    await initBox2D();

    const vec = new box2d.b2Vec2(1, 2);
    console.log(`vec = ${vec.x}, ${vec.y}`);

    const debugDrawer = new DebugDrawer();
}

init();
8Observer8 commented 1 year ago

I created a topic here: https://stackoverflow.com/questions/75179301/drawing-collider-boundaries-box2d-wasm-error-cannot-construct-a-b2draw-no-c

8Observer8 commented 1 year ago

I found the solution in the official example: https://github.com/Birch-san/box2d-wasm/tree/master/demo/modern/public