schteppe / p2.js

JavaScript 2D physics library
Other
2.64k stars 330 forks source link

Raycasting returns internal hits for Body.fromPolygon #178

Closed noelb closed 8 years ago

noelb commented 9 years ago

Raycasting returns hitPoints on internal seams of polygons created with Body.fromPolygon

If you watch the weird shape here, you'll see a green hitPoint travelling inside the shape: screen shot 2015-07-27 at 9 24 30 pm

http://codepen.io/anon/pen/EjdMEa?editors=001

schteppe commented 9 years ago

Yes, currently that is what you get. Body.fromPolygon will add a number of Convex shapes to the body, depending on how the polygon looks. Body.fromPolygon uses poly-decomp.js to split the polygon into convex pieces. You can experiment with the splitting here to get a feeling for how it works: http://schteppe.github.io/poly-decomp.js/

Therefore, I'd say this is not a bug. Two convex shapes should get inner hit points.. When p2 gets a real Polygon shape, this would be an issue that has to be solved though.

To solve your problem as p2 is now, you can check the faceIndex property of your RaycastResult. Identify outer edge hits and skip the rest.

var faceIndices = [1,2,3,4,5]; // Fill with edge indices
var ray = new Ray({
    mode: Ray.ALL,
    from: [0, 0],
    to: [10, 0],
    callback: function(result){
        if(result.body === polygonBody && faceIndices.indexOf(result.faceIndex) !== -1){
            // The hit was on an edge
        }
    }
});

Note that this won't be an issue if you use Ray.CLOSEST.