2013-12-17 6 views
2

저는 매우 새로운 프로그래머이고 배열에서 충돌을 감지하는 방법을 파악하려고합니다. 나는 그것을 생각해 보려고 노력했는데, 내가 생각했던 몇 가지 예를 발견했지만 지금까지는 그렇게하지 못했다.처리중인 배열의 사각형 충돌 감지

여기 내 코드가 있습니다.별로 중요하지 않습니다.

상자가 화면 상단에있을 때가끔 메시지가 표시되지만 이유는 확실하지 않습니다.

Box [] b = new Box[1]; 

float x,y,w,h; 

void setup(){ 
    size(800,800); 
    x=random(width); 
    y=random(height); 
    w=random(200); 
    h=random(200); 


    b[0] = new Box(x,y,w,h); 

} 

void draw(){ 
    background(0); 
    x=random(width); 
    y=random(height); 
    w=25; 
    h=25; 

    for(int j = 0; j<b.length;j++){ 
    for(int k = 0; k<b.length;k++){ 
     if(j!=k){ 
     b[j].contact(b[k]); 

     } 


    } 

    } 

    for(int i=0;i<b.length;i++){ 
    b[i].run(); 


    } 



} 


void keyPressed(){ 

    if(key =='n'){ 

    Box boxnew = new Box(x,y,w,h); 
    b = (Box[]) append(b,boxnew); 



    } 


} 


class Box{ 

    float x,y,w,h,c1,c2,c3,ii,xmo,ymo; 

Box(float mx,float my,float mw,float mh){ 

    x=mx; 
    y=my; 
    w=mw; 
    h=mh; 
    c1=150; 
    c2=50; 
    c3=200; 
    xmo=1; 
    ymo=1; 


} 
    void run(){ 
    maker(); 
    mover(); 
    wcolli(); 


    } 

    void maker(){ 
    ii=random(-1,1); 
    c1+=ii; 
    c2+=ii; 
    c3+=ii; 
    fill(c1,c2,c3); 
    rect(x,y,w,h); 

    } 

    void mover(){ 
    x+=xmo; 
    y+=ymo; 


    } 

    void wcolli(){ 

    if(x>800-w||x<1){ 
     xmo*=-1; 
     } 
     if(y>800-h||y<1){ 
     ymo*=-1; 
     } 


    } 
    void contact(Box b){ 

    if((b.x>=this.x&&b.x<=this.w||b.w>=this.x&&b.w<=this.x) && (b.h>=this.y||b.y<=this.h)){ 
     println("hit"); 



    } 
    if((b.y<=this.h&&b.y>=this.y||b.h<=this.h&&b.h>=this.y) && (b.x<=this.w||b.w>=this.x)){ 
     println("hit"); 


    } 


    } 
} 
+0

질문 관련 .http : //stackoverflow.com/questions/23774684/android-rect-intersect-is-always-false/23775232#23775232 – Nepster

답변

3

충돌 감지에 몇 가지 문제가 있습니다. 가장 중요한 점은 폭과 높이 (wh)를 절대 위치로 사용하려는 것입니다. 실제로는 각 상자의 왼쪽 상단 모서리와 관련이 있기 때문에 상황이 작동하지 않는 것 같습니다. 비교를 수행하기 전에 실제 오른쪽 하단 모서리 위치를 계산해야합니다.

또한 if 조건에주의해야합니다. 그것은 논리 연산의 우선 순위는 결합 AND (&&||)이 같은 간단한 축 정렬 직사각형의 충돌을 등

와 OR 때 명확히하기 위해 괄호를 사용하는 것이 가장 좋습니다, 여기에 내가 유용한 접근 방법이다 :

void contact(Box b) { 

    // Calculate the bottom-right corners of the boxes. 
    float myX2 = x + w; 
    float myY2 = y + h; 
    float otherX2 = b.x + b.w; 
    float otherY2 = b.y + b.h; 

    // If this box is entirely to the left of box b, then there is no collision. 
    if (x < b.x && myX2 < b.x) return; 

    // If this box is entirely to the right of box b, then there is no collision. 
    if (x > otherX2 && myX2 > otherX2) return; 

    // If this box is entirely above box b, then there is no collision. 
    if (y < b.y && myY2 < b.y) return; 

    // If this box is entirely below box b, then there is no collision. 
    if (y > otherY2 && myY2 > otherY2) return; 

    // If we reach this point, the boxes haven't missed each other. 
    // Therefore, there must be a collision. 
    println("hit"); 

} 

그건 한 상자가 다른 상자를 놓칠 수있는 모든 가능한 상황을 확인하여 충돌을 결정합니다. 그들이 서로를 놓치지 않았다면, 논리적으로 충돌이 있어야합니다.