SFML community forums

Help => General => Topic started by: Dark Byte on December 05, 2010, 02:36:30 am

Title: Separating Axis Theorem
Post 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?
Title: Separating Axis Theorem
Post by: Groogy on December 05, 2010, 03:01:40 am
List of websites (http://lmgtfy.com/?q=C%2B%2B+Separating+Axis+Theorem)
Title: Separating Axis Theorem
Post by: Groogy on December 05, 2010, 03:11:09 am
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
Title: Separating Axis Theorem
Post by: Dark Byte on December 05, 2010, 04:11:30 am
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:

Code: [Select]

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?
Title: Separating Axis Theorem
Post by: Groogy on December 05, 2010, 04:31:08 am
Looks like it is for each vector.

Quote
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 :)
Title: Separating Axis Theorem
Post by: danman on December 05, 2010, 10:41:46 am
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.
Title: Separating Axis Theorem
Post by: Dark Byte on December 05, 2010, 07:53:47 pm
Alright thanks.