Box2dx: Usage of World.QueryAABB?
Posted
by Rosarch
on Stack Overflow
See other posts from Stack Overflow
or by Rosarch
Published on 2010-03-26T18:21:37Z
Indexed on
2010/03/26
18:23 UTC
Read the original article
Hit count: 563
I'm using Box2dx with C#/XNA. I'm trying to write a function that determines if a body could exist in a given point without colliding with anything:
/// <summary>
/// Can gameObject exist with start Point without colliding with anything?
/// </summary>
internal bool IsAvailableArea(GameObjectModel model, Vector2 point)
{
Vector2 originalPosition = model.Body.Position;
model.Body.Position = point; // less risky would be to use a deep clone
AABB collisionBox;
model.Body.GetFixtureList().GetAABB(out collisionBox);
// how is this supposed to work?
physicsWorld.QueryAABB(x => true, ref collisionBox);
model.Body.Position = originalPosition;
return true;
}
Is there a better way to go about doing this? How is World.QueryAABB
supposed to work?
Here is an earlier attempt. It is broken; it always returns false.
/// <summary>
/// Can gameObject exist with start Point without colliding with anything?
/// </summary>
internal bool IsAvailableArea(GameObjectModel model, Vector2 point)
{
Vector2 originalPosition = model.Body.Position;
model.Body.Position = point; // less risky would be to use a deep clone
AABB collisionBox;
model.Body.GetFixtureList().GetAABB(out collisionBox);
ICollection<GameObjectController> gameObjects = worldQueryEngine.GameObjectsForPredicate(x => ! x.Model.Passable);
foreach (GameObjectController controller in gameObjects)
{
AABB potentialCollidingBox;
controller.Model.Body.GetFixtureList().GetAABB(out potentialCollidingBox);
if (AABB.TestOverlap(ref collisionBox, ref potentialCollidingBox))
{
model.Body.Position = originalPosition;
return false; // there is something that will collide at this point
}
}
model.Body.Position = originalPosition;
return true;
}
© Stack Overflow or respective owner