Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to find positions to insert new bodies into my world. For that, I'd like to have a "free" spot where this body wouldn't overlap with anything else. So my plan was to sample "random" positions and check whether they overlap with my "potential" new body. Since my bodies are always circular, I'd need to test within a given circle. So far, the only way to use box2d for this seems to use b2World::QueryAABB around my circle and manually doing an overlap test with all the fixtures it gives me (Box2D doesn't event seem to allow me to tap into its overlapping tests?!). It seems to me like Box2D should already provide such functionality - is there a way that lets me do this without reinventing most of the wheel again?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

I suggest using a sensor.

  1. Create a circular body of the size of you choosing at the location you desire.
  2. Set the Sensor flag to true.
  3. Leave it there for a step. (The delta can be 0 units)
  4. See if the sensors isTouching flag is set to true.

box2d Manual

Sometimes game logic needs to know when two fixtures overlap yet there should be no collision response. This is done by using sensors. A sensor is a fixture that detects collision but does not produce a response.

You can flag any fixture as being a sensor. Sensors may be static or dynamic. Remember that you may have multiple fixtures per body and you can have any mix of sensors and solid fixtures.

Sensors do not generate contact points. There are two ways to get the state of a sensor:

  1. b2Contact::IsTouching
  2. b2ContactListener::BeginContact and EndContact
share|improve this answer
    
If you need to leave the sensor for a step, that's a little tough if you're trying to find a location to place your object: "Is here good? nope... What about here? nope.. etc" –  John McDonald Apr 13 '12 at 16:49
    
Yea, especially since I might have to sample a lot of points to actually find one that is free. Can't just leave it there for x steps. Other than that, it's a nice suggestion.. –  ltjax Apr 13 '12 at 17:19
    
@John McDonald No need to wait a frame if that's what your implying. Just have a time delta of 0 ms when you do the step. It will update collision but not move anything. –  ClassicThunder Apr 13 '12 at 18:11
    
Ahh, that makes more sense. Thanks for updating the answer too btw –  John McDonald Apr 13 '12 at 18:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.