SFML community forums
Help => General => Topic started by: Dark Byte on December 05, 2010, 02:36:30 am
-
Where can I learn how to implement the Separating Axis Theorem into one of my projects?
-
List of websites (http://lmgtfy.com/?q=C%2B%2B+Separating+Axis+Theorem)
-
Sorry couldn't help myself :D
Anyway I don't know what kind of algorithm it is but at first glance those websites looked promising
-
I have already searched google I found this http://www.gamedev.net/reference/programming/features/2dRotatedRectCollision/page2.asp
Step two reads: "[...] project the vectors representing the four corners of each rectangle onto each of the axes. [...]"
I know how to project them onto the axes. But somehow I am stuck I have this:
sf::Vector2f Project (sf::Vector2f& v1, sf::Vector2f& v2)
{
sf::Vector2f vec;
float x_ = v1.x * v2.x;
float y_ = v1.y * v2.y;
float d = pow(v2.x, 2) + pow(v2.y, 2);
x_ = (x_ + y_)/d;
vec.x = x_ * v2.x;
vec.y = x_ * v2.y;
return vec;
}
float Dot (sf::Vector2f& v1, sf::Vector2f& v2)
{
return v1.x * v2.x + v1.y * v2.y;
}
sf::Rect<sf::Vector2f> GetRectanglePoints (sf::Shape& Obj)
{
if ( Obj.GetNbPoints() == 4 )
{
sf::Rect<sf::Vector2f> ret;
ret.Top = Obj.GetPointPosition(0); // top-left corner
ret.Right = Obj.GetPointPosition(1); // top-right corner
ret.Bottom = Obj.GetPointPosition(2); // bottom-right corner
ret.Left = Obj.GetPointPosition(3); // bottom-left corner
return ret;
}
}
bool RectsCollide (sf::Rect<sf::Vector2f>& rect1, sf::Rect<sf::Vector2f>& rect2)
{
// Find the axis
sf::Vector2f Axis[4];
Axis[0].x = rect1.Right.x - rect1.Top.x;
Axis[0].y = rect1.Right.y - rect1.Top.y;
Axis[1].x = rect1.Right.x - rect1.Bottom.x;
Axis[1].y = rect1.Right.y - rect1.Bottom.y;
Axis[2].x = rect2.Top.x - rect2.Left.x;
Axis[2].x = rect2.Top.y - rect2.Left.y;
Axis[3].x = rect2.Top.x - rect2.Right.x;
Axis[3].y = rect2.Top.y - rect2.Right.y;
// Project the vectors onto the axis
Now what do I do? I mean do I for each vertex in both rects project them to each axis? or only to a specific one?
-
Looks like it is for each vector.
The next step is to project the vectors representing the four corners of each rectangle onto each of the axes.
Also the page contains the formula for the actual vector equation and even simplified to a scalar math equation.
Though this is new land for me, so I'm not sure. Best advice from here I can give you is try it out and see if it works. After you've run some tests and seen if you get the expected results then write here again and tell me. Who knows you might get someone more educated who can give you a proper answer in that time :)
-
http://www.flashxpress.net/ressources-flash/la-detection-de-collision/
it's a french tutorial, but there is some demonstrations, and you can use any kind of translator.
-
Alright thanks.