Detecting the empty region in my physics scene

Detecting the empty region in my physics scene

I am implementing a physics space using chipmunk in cocos2dJS v3.0
I’ve around 10-12 physics sprites moving here and there in a box.
These sprites are stored in array whose position can be known.]
They all have physics body and shapes attached

I want to know the region in the scene of having same contentSize as one of those physics sprite so that I can spawn a new physics sprite…

How to do so?



Hey, anybody? Any idea… :smile:

Hope my doubt is clear… otherwise in sweet and simple way, here it is…

I’ve a region say around half of the screen’s size. It contains many physics sprites which are moving here and there in that box under certain forces.
This all thing is (obviously) inside space implemented by chipmunk…

NOW, I want to find a region inside that box which is not having that physics sprite so that I could spawn one more physics sprite in that?

I can do this using normal programming method but is there any API in cocos2dJS or chipmunk for finding region in space which is not containing any physicsBody?



I believe there is no integrated API in Cocos2d-JS neither in Chipmunk


I’m pretty certain there’s nothing ‘out of the box’ that allows you to find an unpopulated area.

It’s an interesting problem, though!

I’ve previously handled a similar problem by spawning new entities off-screen and moving them in - so they are guaranteed to spawn in an empty space, and then the physics engine handles them as normal as they move into position.

Create sprite 100 units above top of the window - set visibility to zero
Allow sprite to fall until it collides with something.
On the collision, make it visible

Not exactly what you’re asking for, though!

If you do solve your issue, it’d be great if you posted the solution here to help others (me!!) in the future!


Hey, thanks for this wonderful visible true/false thing.

  1. Also, can I make my physics body visible/invisible too?
    If so, then I should have know this…

  2. Also, by default, my those sprites are colliding and handled automatically by chipmunk without any collision callback.
    So, if I need to use your way, which is to spawn those physics sprite(with physics bodies attached buy inivisible),
    hope this won’t be handled automatically by chipmunk. I mean, hope it does not remain away out of my physics box because they’re colliding with its boundaries. !!!

And, ya. Actually my way is not so good. I mean, since I was having very very few physics sprite just 2 when I was to spawn those physics body sprites. And also there were random forces on existing physics sprites in my physics box.
So, it was very less probability that they will have same position at the time of spawning just 5 more new.
I played my game 25-40 times. And it never happened strange motion. So, this was fine with my case and moreoever it was my first game.

Also, the best solution for my case is only to find the empty region in the scene.
Which I guess could be done using iterating a dummy box of size half of the physics sprite and then shifting it by half the size of sprite in x axis and y axis (So, 2 for loops), and then checking whether this rectangle of my existing sprites in the scene contains the points in my dummy rectangle…

This should have worked, but I couldn’t use before submitting my game because the I got late reply of using that API to check boolean of rectangle within rectangle.

Anyways, I would be happy if you reply to my 2 questions/doubts in this post…



Hi. I am also trying to find empty region in my physics to spawn new item. I use Cocos Creator 1.9.3 Box2D. It has seceral query functions like testAABB testPoint and rayCast. I already made a function which finds empty area but it’s not working correctly for static bodies. If you guys have any ideas that would be cool!
Here is my topic for details: