2012-10-03 11 views
1

현재 C++과 SFML 1.6을 사용하여 여가 시간에 게임을 코딩하고 있습니다. 충돌 감지를 넣을 시간이 주변에있을 때 나는 프로그램이 충돌을 감지하는 성가신 버그에 부딪혔다. 마치 객체가 왼쪽 상단으로 이동하는 고정 된 스프라이트의 크기에 따라 다른 양만큼 이동 한 것처럼 보인다. 이것을 보통 크기의 약 절반으로 테스트하십시오. 내 코드에 버그가 무엇인지는 모르지만 아마도 그렇게 할 수 있습니다. 내 코드는 아래에 나열되어 있습니다. 미리 감사드립니다.SFML 1.6 충돌 문제

#include <SFML/Graphics.hpp> 
#include <math.h> 

bool collide_rec(sf::Sprite object_1, sf::Sprite object_2); 
bool collide_point(sf::Vector2f point, sf::Sprite object); 
void collide(sf::Sprite& movable, object& movable_data, sf::Sprite& sationary); 

void collide(sf::Sprite& movable, object& movable_data, sf::Sprite& stationary) 
{ 
//Handle Collision NOTE: Results in infinate loop if started on colliding object 
for(;collide_rec(movable, stationary);) 
{ 
if((movable_data.move.spd.x<0)||(movable_data.move.left==true||movable_data.direction==LEFT)) movable.Move(1, 0); 
if((movable_data.move.spd.x>0)||(movable_data.move.right==true||movable_data.direction==RIGHT)) movable.Move(-1, 0); 
if((movable_data.move.spd.y<0)||(movable_data.move.up ==true||movable_data.direction==UP )) movable.Move(0, 1); 
if((movable_data.move.spd.y>0)||(movable_data.move.down ==true||movable_data.direction==DOWN)) movable.Move(0, -1); 
    } 
} 

bool collide_point(sf::Vector2f point, sf::Sprite object) 
{ 

} 

bool collide_rec(sf::Sprite object_1, sf::Sprite object_2) 
{ 
    bool hit=true; 
    sf::Vector2f tl_1, br_1, tl_2, br_2;//Top-Left Coner, Botom Right Corner 

    //Assign the corners proporly 
    tl_1= object_1.GetPosition(); 
    tl_2= object_2.GetPosition(); 

    br_1= (object_1.GetPosition()+object_1.GetSize()); 
    br_2= (object_2.GetPosition()+object_2.GetSize()); 

    if((tl_1.x<tl_2.x)&&(br_1.x<tl_2.x) || (tl_1.x<br_2.x)&&(br_1.x<br_2.x)) //if both points are to the left or right on the x demtion 
    hit=false; 

    if((tl_1.y<tl_2.y)&&(br_1.y<tl_2.y) || (tl_1.y<br_2.y)&&(br_1.y<br_2.y)) //if both points are to the left or right on the y demtion 
    hit=false; 

    return hit; 
} 

답변

0

아직 답을 얻지는 못했지만 도움이 될지 모르겠다.

확인할 사항 : - object_1.getPosition() 및 object_2의 위치가 개체의 왼쪽 위 모서리인지 여부. - 개체가 이 아니고 인 경우 오른쪽 하단 모서리에는 계산식이 약간 달라야합니다. (x, y) 좌표에 대해 (Pos.x + Width, Pos.y + Height)가됩니다.

당신이의 확실하면

는 몇 가지 나는 당신의 히트 감지 알고리즘에 발견 :

1)

if((tl_1.x<tl_2.x)&&(br_1.x<tl_2.x) || (tl_1.x<br_2.x)&&(br_1.x<br_2.x))

이 조건의 두 번째 부분 :

(tl_1.x < br_2.x)&&(br_1.x < br_2.x)) 

이 (이)되어야합니다.

(tl_1.x > br_2.x)&&(br_1.x > br_2.x)) 

2) 마찬가지로

: 그것이 있어야처럼

(tl_1.y < tl_2.y)&&(br_1.y < tl_2.y) 

보인다

(tl_1.y > tl_2.y)&&(br_1.y > tl_2.y) 

3)

이 마지막으로, 조건이 그들에게 중복의 일정 금액을 가지고, 그것은 불필요합니다.

당신이 당신의 topLeft1, BottomRight1 및 topLeft2, BottomRight2 당신은 단순히이 같은 충돌을 확인할 수 있습니다 일단 : 왼쪽 아래에있는 (0, 0) 원점,

직교 좌표계를 가정합니다.

  • X 단계 충돌 :

    경우 (topLeft1.x> BottomRight2.x || BottomRight1.x < TopLeft2.x)

// 만약 BOX1의 왼쪽> 상자 2의 오른쪽 // // 상자 1의 오른쪽에 < 상자 2의 왼쪽에 의 상자가있는 경우 은 X 축에이 충돌합니다.

  • Y 스테이지 충돌 :

    (topLeft1.y < BottomRight2.y || BottomRight1.y> TopLeft2.y 있으면)

// 경우 BOX1 < 윗면 상자 2의 하단면 OR // 상자 1의 하단면> 상자 2의 상단면 - 상자 이 Y 축에 충돌하지 않습니다.

8 조건에서 4 조건으로 줄일 수 있습니다. 도움이 되길 바랍니다.