Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search for empty space. #288

Open
McRain opened this issue Mar 15, 2017 · 5 comments
Open

Search for empty space. #288

McRain opened this issue Mar 15, 2017 · 5 comments

Comments

@McRain
Copy link

McRain commented Mar 15, 2017

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
Copy link
Owner

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
Copy link
Author

McRain commented Mar 16, 2017

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
Copy link
Owner

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
Copy link
Author

McRain commented Mar 16, 2017

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
Copy link
Owner

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants