2
您好,我是C++ SFML的新手。我應該繪製一些矩形並在旋轉時渲染它們的AABB,並且我想檢測爲它們設置的尺寸是否與另一個旋轉的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);
}
}
}
}