Skip to content Skip to sidebar Skip to footer

Emscripten Bindings: How To Create An Accessible C/c++ Array From Javascript?

I am using box2d and attempting to create a chain shape. In order to create a chain shape or polygon shape I must pass an array of vectors in order to specify the geometry. I do

Solution 1:

I have solved this problem by using these (as yet undocumented) emscripten features.

Note that I am accessing the functions and values (like ALLOC_STACK and wrapPointer) out of the Box2D object simply because that is the scope where I have found them to be exposed. They are emscripten-specific values, so for a different project and/or build settings it would be different.

// an alternative method that may work (shorter, less obvious code) is // allocate([v1x,0,0,0,v1y,0,0,0,v2x,0,0,0,...], 'float', Box2D.ALLOC_STACK);// 8 byte per vector * 4 vectors = 32 bytes of memory required var buffer = Box2D.allocate(32, 'float', Box2D.ALLOC_STACK);
Box2D.setValue(buffer, left, 'float'); // v1x
Box2D.setValue(buffer+4, bottom, 'float'); // v1y
Box2D.setValue(buffer+8, right, 'float'); // v2x
Box2D.setValue(buffer+12, bottom, 'float'); // v2y
Box2D.setValue(buffer+16, right, 'float'); // v3x
Box2D.setValue(buffer+20, top, 'float'); // v3y
Box2D.setValue(buffer+24, left, 'float'); // v4x
Box2D.setValue(buffer+28, top, 'float'); // v4yvar ptr_wrapped = Box2D.wrapPointer(buffer, Box2D.b2Vec2);
shape.CreateLoop(ptr_wrapped, 4);
body.CreateFixture(shape,0.0);

Post a Comment for "Emscripten Bindings: How To Create An Accessible C/c++ Array From Javascript?"