Birch-san / box2d-wasm

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

this check should be wrong. #40

Closed jcyuan closed 2 years ago

jcyuan commented 2 years ago

image

it means i have to implement the JSDrawer like this?

  const myDrawer  = {
      DrawSegments(...) {}
  }

im doing this: image and then

this._drawer = new DebugDrawer(this);
world.SetDebugDraw(this._drawer);

the method is just right there: image

btw, the current way to import methods, i feel inconvenient, because i have to pass the b2d instance everywhere.... i prefer just import { what i need } from 'box2d';

i'm doing test on this proj.

thank you.

Birch-san commented 2 years ago

that binding code is generated via Emscripten; if you want it to work differently: you'll need to file an issue with them (or contribute a pull request).

I suspect that if they changed b.hasOwnProperty('DrawSegment') to 'DrawSegment' in b it'd fix it (i.e. it would look for method implementations on the prototype chain).

typically I use this technique to satisfy the validation:

const debugDraw = Object.assign(new JSDraw(), {
    /**
     * @param {number} vert1_p pointer to {@link Box2D.b2Vec2}
     * @param {number} vert2_p pointer to {@link Box2D.b2Vec2}
     * @param {number} color_p pointer to {@link Box2D.b2Color}
     * @returns {void}
     */
    DrawSegment(vert1_p, vert2_p, color_p) {
    },
});

there's probably a better way.

Birch-san commented 2 years ago

btw, the current way to import methods, i feel inconvenient, because i have to pass the b2d instance everywhere....
i prefer just import { what i need } from 'box2d';

imports are evaluated synchronously, but any WebAssembly module requires asynchronous setup (you have to download the bytecode and then parse it).

the way I typically solve this is to make an asynchronous module box2d.ts which exports an initialized Box2D Emscripten module:
https://github.com/Birch-san/liquidfun-play-2/blob/master/src/box2d.ts#L6

and then use ES7 dynamic import() + top-level await to wait for the asynchronous module:
https://github.com/Birch-san/liquidfun-play-2/blob/master/src/demo/gravity.ts#L6
(you'll need to make sure your build system is configured with modern settings if you want to use these language features).

jcyuan commented 2 years ago

btw, the current way to import methods, i feel inconvenient, because i have to pass the b2d instance everywhere.... i prefer just import { what i need } from 'box2d';

imports are evaluated synchronously, but any WebAssembly module requires asynchronous setup (you have to download the bytecode and then parse it).

the way I typically solve this is to make an asynchronous module box2d.ts which exports an initialized Box2D Emscripten module: https://github.com/Birch-san/liquidfun-play-2/blob/master/src/box2d.ts#L6

and then use ES7 dynamic import() + top-level await to wait for the asynchronous module: https://github.com/Birch-san/liquidfun-play-2/blob/master/src/demo/gravity.ts#L6 (you'll need to make sure your build system is configured with modern settings if you want to use these language features).

ok, thank you.