2013-12-19 2 views
0

이 질문은 다른 것들과 비슷하지만 사각형 경계의 게임 개체가있는 것 같습니다. 어느 위치로 움직입니다. 중간에있는 항목과 교차하는 경우 줄을 따라 어떻게 확인할 수 있습니까?거리가 충돌했을 때 사각형이 한 위치에서 다른 위치로 이동할 때 게임 충돌 검사

극단적 인 시나리오. [x = 2, x = 1, width = 1, height = 1] A는 [x = 4, y = 1, width = 1, height = 1]로 이동합니다. 사각형 B가 [3,1,0.5,0.5]에있는 곳에 놓치면 놓치게됩니다.

스칼라 및 교차 제품에 대해 읽었지만 올바르게 읽으면 단일 행입니다. 이것은 낮은 프레임 속도로 느린 장치에서 안드로이드 게임 개발로 인한 것입니다. 나는 그것을 대상으로 떨어 뜨리고있다. 아래의 코드를 사용하여 intersects를 확인합니다.

public boolean testIntersection(GameVector lowerLeftMain, float mainWidth, float   mainHeight, GameVector lowerLeftCollider, 
float colliderWidth, float colliderHeight){ 

    boolean intersect = false; 

    if(lowerLeftMain.x < lowerLeftCollider.x + colliderWidth+0.08f && //checks left collision 
      lowerLeftMain.x + mainWidth > lowerLeftCollider.x-0.08f && //checks right collision 
      lowerLeftMain.y < lowerLeftCollider.y + colliderHeight+0.08f &&//checks top collision 
      lowerLeftMain.y + mainHeight > lowerLeftCollider.y-0.08f)//checks bottom collision 
     intersect = true; 
    return intersect; 
} 

제가 직사각형을 포기하고 레이 캐스트 라인 충돌 스타일에 집중한다면 누군가 올바른 방향으로 나를 가리킬 수 있습니까?

미리 감사드립니다.

+0

http://stackoverflow.com/questions/9438690/whats-the-best-way-to-do-collision-detection, http://gamedev.stackexchange.com/questions/24434/polygon- 충돌 감지 안드로이드, – user2864740

답변

0

링크를 제공해 주셔서 감사합니다. 훌륭한 링크는 앞으로 다른 사람들을 돕기 위해 내 코드를 게시 할 것입니다.

내 분리 축 정량 in java. 겹치는 부분 만 테스트하십시오. 나는 최소와 최대 오버랩 벡터를 볼 수있는 효율성과 잠재 성 때문에이 알고리즘을 사용했다.

public GameVector[] getVertices(GameObject obj){ 
    final GameVector topLeft = new GameVector(obj.mPosition.x-0.06f - (obj.mWidth/2), obj.mPosition.y+0.06f +(obj.mHeight/2)); 
    final GameVector topRight = new GameVector( obj.mPosition.x+0.06f + (obj.mWidth/2),obj.mPosition.y+0.06f +(obj.mHeight/2)); 
    final GameVector bottomLeft = new GameVector( obj.mPosition.x-0.06f - (obj.mWidth/2), obj.mPosition.y-0.06f -(obj.mHeight/2)); 
    final GameVector bottomRight = new GameVector( obj.mPosition.x+0.06f + (obj.mWidth/2), obj.mPosition.y-0.06f -(obj.mHeight/2)); 

    //order here matters 
    GameVector[] vertices = { topLeft, topRight, bottomRight, bottomLeft }; 
    return vertices; 
} 

public GameVector[] getAxis(GameObject shape){ 

    GameVector[] vertices = getVertices(shape); 

    GameVector[] axes = new GameVector[vertices.length]; 
    // loop over the vertices 
    for (int i = 0; i < vertices.length; i++) { 
     // get the current vertex 
     GameVector p1 = vertices[i]; 
     // get the next vertex if i+1 == vertices length set back to vertices [0] 
     GameVector p2 = vertices[i + 1 == vertices.length ? 0 : i + 1]; 
     // subtract the two to get the edge vector 
     GameVector edge = p1.subtract(p2.x, p2.y); 
     // get either perpendicular vector 
     GameVector normal; 
     //get the left side normal of the vector due to clock wise positions 
     normal = new GameVector(edge.y, -edge.x);//edge.perp(); 
     axes[i] = normal; 
    } 
    return axes; 
} 

public float dotProduct(GameVector a, GameVector b){ 
    float dp = a.x*b.x + a.y*b.y; 
    return dp; 
} 

public class Projection { 

    private final float min; 
    private final float max; 

    public Projection(float min, float max) { 
     this.min = min; 
     this.max = max; 
    } 

    public boolean doesOverlap(final Projection other) { 
     return !(this.min > other.max || other.min > this.max); 

    } 
} 
관련 문제