2010-03-25 8 views
0

Box2dx (C#으로 포팅 됨, XNA 용으로 최적화 됨)를 사용 중입니다. 충돌 해결을 처리하지만 두 객체가 현재 충돌하고 있는지 어떻게 알 수 있습니까?충돌이 발생했을 때 Box2d에 질문하기

public bool IsColliding(GameObjectController collider1, GameObjectController collider2) 
collider1.Model.Body는 Box2D의 Body입니다

collider1.Model.BodyDef는 Box2D의 BodyDef입니다 :

내가 작성하는 노력하고있어 기능입니다. (동일은 물론, collider2 간다.)

UPDATE : 접촉 리스너 것 같은데 또는이 유용 할 수있다 :

 AABB collisionBox; 
     model.Body.GetFixtureList().GetAABB(out collisionBox); 

GetFixtureList() 복귀 한 고정합니까?

답변

0

모양 유형을 알고있는 경우 다음 기능 중 하나를 사용하여 겹침 여부를 직접 확인할 수 있습니다.

public static void Collision.CollideCircles(ref Manifold manifold, 
    CircleShape circle1, XForm xf1, CircleShape circle2, XForm xf2); 
public static void Collision.CollidePolygonAndCircle(ref Manifold manifold, 
    PolygonShape polygon, XForm xf1, CircleShape circle, XForm xf2); 
public static void Collision.CollideEdgeAndCircle(ref Manifold manifold, 
    EdgeShape edge, XForm transformA, CircleShape circle, XForm transformB); 
public static void Collision.CollidePolyAndEdge(ref Manifold manifold, 
    PolygonShape polygon, XForm transformA, EdgeShape edge, XForm transformB); 
public static void Collision.CollidePolygons(ref Manifold manifold, 
    PolygonShape polyA, XForm xfA, PolygonShape polyB, XForm xfB); 

이들 모두는 두 개의 모양과 두 개의 변환을 사용합니다. 결과는 모양 경계가 교차하는 점들의 집합을 포함하는 매니 폴드 객체입니다. 포인트 수가 0보다 큰 경우 충돌이 발생합니다.

클래스를 통해 ContactListener 인터페이스를 구현하면 간접적으로 동일한 정보를 얻을 수 있습니다.

public class MyContactListener : ContactListener { 
    // Called when intersection begins. 
    void BeginContact(Contact contact) { 
     // ... 
     // Make some indication that the two bodies collide. 
     // ... 
    } 

    // Called when the intersection ends. 
    void EndContact(Contact contact) { 
     // ... 
     // Make some indication that the two bodies no longer collide. 
     // ... 
    } 

    // Called before the contact is processed by the dynamics solver. 
    void PreSolve(Contact contact, Manifold oldManifold) {} 

    // Called after the contact is processed by the dynamics solver. 
    void PostSolve(Contact contact, ContactImpulse impulse) {} 
} 

두 신체는 Contact._fixtureA.Body 및 Contact._fixtureB.Body에서 찾을 수 없습니다. 리스너 객체를 World에 등록해야합니다.

GetFixtureList(), GetBodyList(), GetJointList() 등은 연결된 목록의 첫 번째 요소를 반환합니다. 요소의 GetNext()를 호출하면 목록의 다음 요소를 찾을 수 있습니다. 다음 코드를 사용하여 목록을 반복 할 수 있습니다. GetNext()가 null을 반환하면 더 이상 요소가 없습니다.

// Given there is a Body named body. 
for (Fixture fix = body.GetFixtureList(); fix; fix = fix.GetNext()) { 
    // Operate on fix. 
} 
관련 문제