2

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 not see any documentation to help me accomplish this, and the notes about bindings here don't go into any detail about arrays.

How can I construct an array?

1 Answer 1

2

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'); // v4y
var ptr_wrapped = Box2D.wrapPointer(buffer, Box2D.b2Vec2);
shape.CreateLoop(ptr_wrapped, 4);
body.CreateFixture(shape,0.0);
Sign up to request clarification or add additional context in comments.

3 Comments

Small update - it works great for polygons but not for chain shapes. I think this is because in the original C++ the vertices of a b2PolygonShape are stored in a fixed size array since there will only ever be 8 of them, whereas chain shapes malloc the required space upon creation. I'm guessing the allocate used here tramples on something. I don't have a fix, but I have found that compiling with --closure 0 to disable the closure compiler at least avoids the problem, at the expense of much larger js file (performance seems the same...?)
Interesting. The chain shape I made in my test seemed to work fine but it certainly could have trampled the stack without me noticing anything wrong. What if you try passing a larger size value to allocate? Are you using b2ChainShape.CreateLoop()? I think the only reasonable thing Box2D would do is allocate its own b2Vec2 buffer to copy the provided geometry into, and that ought to function just as well as the rest of the allocations for everything else Box2D does.
The chain shape is created and works fine, at least initially. I should have mentioned that this only occurred after I made some constructors public in the jointdef classes, a seemingly totally unrelated area! I posted more info to issue 629 on github, and you can see the problem in my demo (moved to iforce2d.net/embox2d/testbed.html) by changing the very last part of the fallingShapes.js file to create a chain instead of the polygon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.