kripken / box2d.js

Port of Box2D to JavaScript using Emscripten
1.32k stars 196 forks source link

helper tools #87

Open bdanis opened 7 years ago

bdanis commented 7 years ago

I've been trying to work with the helper tools, but found that they don't work as they come

I had to refactor the .allocate function to _malloc and change Box2D.SetValue to _memAlloc

But at the moment, I still can't create a polygon because I get an assertion error on PolygonShape.Set

I have tried placing my vertices clockwise and counterclockwise, both to no avail

Any ideas?

alperencaliskan commented 7 years ago

With new emscripten builds helper code doesn't work because they removed allocate and setvalue functions. https://github.com/kripken/ammo.js/pull/121 I am using this modified code for creating polygons

function createPolygonShape(vertices) {
    var shape = new Box2D.b2PolygonShape();
    // var buffer = Box2D.allocate(vertices.length * 8, 'float', Box2D.ALLOC_STACK);
    var buffer = Box2D._malloc(vertices.length * 8);
    var offset = 0;
    for (var i = 0; i < vertices.length; i++) {
        // Box2D.setValue(buffer + (offset), vertices[i].get_x(), 'float'); // x
        Box2D.HEAPF32[buffer + offset >> 2] = vertices[i].get_x();
        // Box2D.setValue(buffer + (offset + 4), vertices[i].get_y(), 'float'); // y
        Box2D.HEAPF32[buffer + (offset + 4) >> 2] = vertices[i].get_y();
        offset += 8;
    }
    var ptr_wrapped = Box2D.wrapPointer(buffer, Box2D.b2Vec2);
    shape.Set(ptr_wrapped, vertices.length);
    return shape;
}