Reputation: 453
I'm working on implementing Separating Axis Theorem collision with SFML but am running into a problem with my vertex arrays. They are being transformed (the class containing the vertex array inherits sf::Transformable) but the vertex coordinates do not receive the transformation (namely rotation) thus the collision does not work if the object is not in its original orientation. I tried creating a new vertex array before calculating the SAT collision for the vertex array by using something like this
length = sqrt( shape1.getShape()[i].position.x*shape1.getShape()[i].position.x + shape1.getShape()[i].position.y*shape1.getShape()[i].position.y );
newshape1[i].position.x = length*cos( shape1.getRotation()*(PI/180) );
newshape1[i].position.y = length*sin( shape1.getRotation()*(PI/180) );
for the two shapes being tested. However, this doesn't seem to be working. My function returns that a collision has occurred even though one hasn't. I'm not sure if there's an error with the math, but without the SFML transformations applied to the object it works perfectly. So, is there something else I should be doing or is there another way to gather VertexArray information with transformations (pretty much just rotation) applied?
Edit: I found some information here:
http://sfml-dev.org/documentation/2.0/classsf_1_1Transform.php#details
But I'm having some trouble understanding how this should be applied to my VertexArrays. It looks like I should be doing something with getTransform()
inside my class and pass some combination of that with the class' VertexArray.
Edit: Using the information above I tried using this method instead over each vertex of the VertexArray prior to SAT calculations:
newshape1[i].position = shape1.getTransform().transformPoint( shape1.getShape()[i].position );
After negating the transformation position when calculating dot products, it produces the exact same results as the first attempt.
Upvotes: 3
Views: 1223
Reputation: 606
I realize this is a nearly 3-year old question, but this problem has been driving me nuts for about a week, and I've finally managed to crack it. Here's what worked for me:
sf::Vector2f vertexPosition = getTransform().transformPoint( m_vertices[n].position );
...though looking at what worked for me and what you attempted, I'm honestly not sure why your approach wasn't successful. The only differences I see are that (1) I'm calling this function from within my custom shape, which shouldn't make any difference, and (2) you're using GetShape(), which I'm guessing is a function you wrote to return local vertex positions?
Whatever the case, I hope this helps someone in the future who might be stuck on this problem. The snippet I wrote above returns the absolute coordinates of the vertices from any custom shape with any transformations applied to it (translations, scales, and rotations).
Upvotes: 2