schteppe / p2.js

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

Search for empty space. #288

Open McRain opened 7 years ago

McRain commented 7 years ago

Hello. I use p2.js in node.js. I'm looking for an empty space in the world for a new (Circle) body. How can I verify that the circle covers (in whole or in part) some body in the world without calling world.step? The bodies in the world can be box, circle and fromPolygon

schteppe commented 7 years ago

You could use world.broadphase.aabbQuery to get all bodies overlapping the bounding box of your body. This should be faster than going through all bodies one by one directly. If you want to refine the search, so it checks a circle instead of a bounding box, you could use world.narrowphase.bodiesOverlap.

McRain commented 7 years ago

Thanks for the help. Maybe I'm doing something wrong, but it does not give the right result.

for (var l = 0; l < 20; l++) {
                const shape = new p2.Circle({ radius: 2 });
                let pos = [getRandom(-100 / 2, 100 / 2), getRandom(-60 / 2, 60 / 2)];
                if (shape.aabbNeedsUpdate) {
                    shape.updateAABB();
                }
                const body = new p2.Body({
                    mass: 0,
                    position: pos,
                    angularVelocity: 0,
                    type: p2.Body.STATIC
                });
                body.addShape(shape);
                world.addBody(body);
                let isOver = CheckOverNarrowphase(body);
                    while (isOver) {
                        body.position = [getRandom(-100 / 2, 100 / 2), getRandom(-60 / 2, 60 / 2)];
                        isOver = CheckOverNarrowphase(body);
                    }
            }
        function CheckOverNarrowphase(b) {
            const nph = new p2.Narrowphase();
            for (let d = 0; d < world.bodies.length; d++) {
                if (b === world.bodies[d])
                    continue;
                if (nph.bodiesOverlap(b, world.bodies[d])) 
                    return true;
            }
            return false;
        }

White circles still cover other bodies( Image of Yaktocat

schteppe commented 7 years ago

Can you debug it? Step into the bodiesOverlap method?

If you want a quick fix, world.broadphase.aabbQuery should work, but you'll get some extra space around your circles...

McRain commented 7 years ago

Unfortunately, I did not understand very well what kind of information you need to debug. If you have a little time, please look at the test page : http://reneos.com/test.html I tried both of the ways you say, but the result is always the same - white circles sometimes fall on other objects. Thank you

schteppe commented 5 years ago

Did you find a solution for this yet?

Looking at your code, it seems like you use a new Narrowphase instance when you should use the one already existing in world.narrowphase. Your function would change to:

function CheckOverNarrowphase(b) {
    const nph = world.narrowphase;
    for (let d = 0; d < world.bodies.length; d++) {
        if (b === world.bodies[d])
            continue;
        if (nph.bodiesOverlap(b, world.bodies[d])) 
            return true;
    }
    return false;
}