2017-10-15 1 views
2

안녕하세요 저는 C++ SFML을 처음 사용합니다. 회전하는 동안 사각형을 그리고 AABB를 렌더링해야하는데 회전하는 다른 AABB 사각형과 교차하는 차원을 감지하려고합니다. 여기 내가 그들을 탐지하기 위해 사용하는 것들이있다. 회전하는 경우 확인하는 것으로 충분합니까? 분리 축 정리와 같은 것들을 사용해야합니까? 또는 사용할 필요가 없습니다 수있는 방법이있는 OBB회전하는 동안 AABB를 렌더링하는 방법

#define RECT 5 

sf::RectangleShape Rect[RECT]; 
Rect[0].setSize(sf::Vector2f(50.0f, 50.0f)); 
Rect[1].setSize(sf::Vector2f(50.0f, 100.0f)); 
Rect[2].setSize(sf::Vector2f(60.0f, 80.0f)); 
Rect[3].setSize(sf::Vector2f(100.0f, 60.0f)); 
Rect[4].setSize(sf::Vector2f(30.0f, 250.0f)); 

sf::Vector2f MinPoint[RECT]; 
sf::Vector2f MaxPoint[RECT]; 

for (int x = 0; x < RECT; x++) 
{ 
    //Starting Position 
    Rect[x].setOrigin(Rect[x].getSize().x/2, Rect[x].getSize().y/2); 
    xpos += 150; 
    Rect[x].setPosition(xpos, ypos); 
    colcount++; 
    if (colcount == 3) 
    { 
     xpos = 0; 
     ypos += 200; 
     colcount = 0; 
    } 

    Rect[x].setFillColor(sf::Color::Red); 

} 


while (window.isOpen()) 
{ 
    window.clear(sf::Color::Black); 
    //Drawing Shapes 
    for (int x = 0; x < RECT; x++) 
    { 
     window.draw(Rect[x]); 
    } 

    Rect[0].rotate(90*3.14/180); 
    Rect[1].rotate(12 * 3.14/180); 
    Rect[2].rotate(10 * 3.14/180); 
    Rect[3].rotate(180 * 3.14/180); 
    Rect[4].rotate(360 * 3.14/180); 


    for (int i = 0; i < RECT; i++) 
    { 
     MinPoint[i].x = Rect[i].getPosition().x - (Rect[i].getSize().x/2); 
     MaxPoint[i].x = Rect[i].getPosition().x + (Rect[i].getSize().x/2); 
     MinPoint[i].y = Rect[i].getPosition().y - (Rect[i].getSize().y/2); 
     MaxPoint[i].y = Rect[i].getPosition().y + (Rect[i].getSize().y/2); 
    } 


    //Collision Detection 
    for (int i = 0; i < RECT; i++) 
    { 
     for (int j = i + 1; j < RECT; j++) 
     { 
      if (i != j) 
      { 
       if (MaxPoint[i].x >= MinPoint[j].x && MaxPoint[j].x >= MinPoint[i].x && MaxPoint[i].y >= MinPoint[j].y && MaxPoint[j].y >= MinPoint[i].y) 
       { 
        Rect[i].setFillColor(sf::Color::Green); 
        Rect[j].setFillColor(sf::Color::Green); 
       } 
      } 
     } 
    } 

답변

0

보다는 그것의 단지 AABB는 분명히 내가 할 필요가 모두 같은 위치에 설정 한 윤곽선을 투명 사각형의 또 다른 세트를 만들 있다면 그 내 회전 사각형 상자 다음 크기를 내 회전 사각형의 getGlobalBounds로 설정합니다. 충돌 검사는 대신 회전 사각형 자체 대신 이러한 투명 테두리 상자 아래에 배치됩니다.

#define RECT 5 

sf::RectangleShape Rect[RECT]; 
sf::RectangleShape AABB[RECT]; 
Rect[0].setSize(sf::Vector2f(50.0f, 50.0f)); 
Rect[1].setSize(sf::Vector2f(50.0f, 100.0f)); 
Rect[2].setSize(sf::Vector2f(60.0f, 80.0f)); 
Rect[3].setSize(sf::Vector2f(100.0f, 60.0f)); 
Rect[4].setSize(sf::Vector2f(30.0f, 250.0f)); 

sf::Vector2f MinPoint[RECT]; 
sf::Vector2f MaxPoint[RECT]; 

for (int x = 0; x < RECT; x++) 
{ 
    //Starting Position 
    Rect[x].setOrigin(Rect[x].getSize().x/2, Rect[x].getSize().y/2); 
    AABB[x].setOrigin(AABB[x].getSize().x/2, AABB[x].getSize().y/2); 
    xpos += 150; 
    Rect[x].setPosition(xpos, ypos); 
    AABB[x].setSize(sf::Vector2f(Rect[x].getGlobalBounds().width, Rect[x].getGlobalBounds().height)); 
    AABB[x].setPosition(Rect[x].getPosition().x, Rect[x].getPosition().y); 
    colcount++; 
    if (colcount == 3) 
    { 
     xpos = 0; 
     ypos += 200; 
     colcount = 0; 
    } 

    Rect[x].setFillColor(sf::Color::Red); 
    AABB[x].setFillColor(sf::Color::Transparent); 
    AABB[x].setOutlineThickness(1); 
    AABB[x].setOutlineColor(sf::Color::White); 

} 


while (window.isOpen()) 
{ 
    window.clear(sf::Color::Black); 
    //Drawing Shapes 
    for (int x = 0; x < RECT; x++) 
    { 
     window.draw(Rect[x]); 
     window.draw(AABB[x]); 
    } 

    //Rotation 
    Rect[0].rotate(1); 
    Rect[1].rotate(45); 
    Rect[2].rotate(11.25); 
    Rect[3].rotate(5.625); 
    Rect[4].rotate(22.5); 

    for (int i = 0; i < RECT; i++) 
    { 
     MinPoint[i].x = AABB[i].getPosition().x - (AABB[i].getSize().x/2); 
     MaxPoint[i].x = AABB[i].getPosition().x + (AABB[i].getSize().x/2); 
     MinPoint[i].y = AABB[i].getPosition().y - (AABB[i].getSize().y/2); 
     MaxPoint[i].y = AABB[i].getPosition().y + (AABB[i].getSize().y/2); 

     AABB[i].setOrigin(AABB[i].getSize().x/2, AABB[i].getSize().y/2); 
     AABB[i].setSize(sf::Vector2f(Rect[i].getGlobalBounds().width, Rect[i].getGlobalBounds().height)); 
     AABB[i].setPosition(Rect[i].getPosition().x, Rect[i].getPosition().y); 
    } 


    //Collision Detection 
    for (int i = 0; i < RECT; i++) 
    { 
     for (int j = i + 1; j < RECT; j++) 
     { 
      if (i != j) 
      { 
       if (MaxPoint[i].x >= MinPoint[j].x && MaxPoint[j].x >= MinPoint[i].x && MaxPoint[i].y >= MinPoint[j].y && MaxPoint[j].y >= MinPoint[i].y) 
       { 
        Rect[i].setFillColor(sf::Color::Green); 
        Rect[j].setFillColor(sf::Color::Green); 
        AABB[i].setOutlineColor(sf::Color::Blue); 
        AABB[j].setOutlineColor(sf::Color::Blue); 
       } 
      } 
     } 
    } 
관련 문제