我将 Box2dx 与 C#/XNA 一起使用。我正在尝试编写一个函数来确定一个物体是否可以存在于给定点而不与任何东西发生碰撞:
/// <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
工作?
这是较早的尝试。它被打破; 它总是返回假。
/// <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;
}