我在C#/ XNA中使用Box2dx。我想寫確定是否機構可以在一個給定的點不存在與任何碰撞的功能:Box2dx:World.QueryAABB的用法?
/// <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;
}
有沒有一種更好的方式去這樣做呢? World.QueryAABB
應該如何工作?
這是一個較早的嘗試。它被打破;它總是返回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;
}