-
I have been trying to rotate an array of four points that represent a rectangle to match a rotated rectangle sprite. This is the function I've been using, but it doesn't seem to be working.
collide_box[0] = sf::Vector2f(collide_rect.left, collide_rect.top);
collide_box[1] = sf::Vector2f(collide_rect.left + collide_rect.width, collide_rect.top);
collide_box[2] = sf::Vector2f(collide_rect.left, collide_rect.top + collide_rect.height);
collide_box[3] = sf::Vector2f(collide_rect.left + collide_rect.width, collide_rect.top + collide_rect.height);
float offset_x = collide_rect.width / 2 + collide_rect.left;
float offset_y = collide_rect.height / 2 + collide_rect.top;
float rad = sprite.getRotation() / 180 * PI;
for(int i = 0; i < 4; i++)
{
collide_box[i].x -= offset_x;
collide_box[i].y -= offset_y;
collide_box[i].x = collide_box[i].x * std::cos(rad) - collide_box[i].y * std::sin(rad);
collide_box[i].y = collide_box[i].x * std::sin(rad) + collide_box[i].y * std::cos(rad);
collide_box[i].x += offset_x;
collide_box[i].y += offset_y;
}
Thank you.
-
Thank you.
You're welcome! ...or was there some question...? :P
Maybe you could go a bit more into the specifics what doesn't work and what the idea behind your code is, rather than putting it up here and expect people to solve the problem for you. ;)
It's essentially easier to use SFML's built in sf::Transform to apply various transformations such as rotation. Besides, do you use more that just one rotated sprite? Because if not, why not simply use sf::Sprite?
-
I am using this rotated array in order to check for collisions between sprites. I don't know how else I would be able to get the rotated rectangle's coordinates.
As for why it doesn't work, when I draw lines between the points, they stretch to parallelograms.
-
Well you still could use a sprite, but I guess it's better to have your own implementation, that gives easier access to the different edge points.
However I still suggest to use sf::Transform (nice interface to a 3D matrix) or even create a class and derive from sf::Transformable, so you can apply the rotation in an easy way and reuse the transformation for all the different points. Just look it up in the documentation/tutorial, it's really simple to use. :)
-
sf::Sprite::getGlobalBounds takes into account transformations and returns a sf::FloatRect. Conveniently, the latter has a function to check intersection with other FloatRects.
-
float rad = sprite.getRotation() / 180 * PI;
this looks wrong.
Unless your trying to turn from rad to deg, but sprite.getRoation() doesn't return radians.
-
sf::Sprite::getGlobalBounds takes into account transformations and returns a sf::FloatRect...
... representing the bounding box of the rotated sprite, thus quite useless for rotated rectangle collision detection (http://www.gamedev.net/page/resources/_/technical/game-programming/2d-rotated-rectangle-collision-r2604), which is what I believe collechess is trying to implement. :)
-
float rad = sprite.getRotation() / 180 * PI;
this looks wrong.
Unless your trying to turn from rad to deg, but sprite.getRoation() doesn't return radians.
Isn't the equation for transforming degrees to radians * PI/180?
However I still suggest to use sf::Transform (nice interface to a 3D matrix) or even create a class and derive from sf::Transformable, so you can apply the rotation in an easy way and reuse the transformation for all the different points. Just look it up in the documentation/tutorial, it's really simple to use. :)
I'm trying to use sf::Transform, but I don't think I quite understand how it works.
collide_box[0] = sf::Vector2f(collide_rect.left, collide_rect.top);
collide_box[1] = sf::Vector2f(collide_rect.left + collide_rect.width, collide_rect.top);
collide_box[3] = sf::Vector2f(collide_rect.left, collide_rect.top + collide_rect.height);
collide_box[2] = sf::Vector2f(collide_rect.left + collide_rect.width, collide_rect.top + collide_rect.height);
float offset_x = collide_rect.width / 2 + collide_rect.left;
float offset_y = collide_rect.height / 2 + collide_rect.top;
float rad = sprite.getRotation() * PI / 180;
for(int i = 0; i < 4; i++)
{
sf::Transform t;
t.transformPoint(collide_box[i]);
t.rotate(rad, offset_x, offset_y);
}
This is just giving me a point outside of the rectangle.
-
deg = rad * (180/PI)
rad = deg * (PI/180)
Have your read the SFML tutorial (http://www.sfml-dev.org/tutorials/2.1/graphics-transform.php#custom-transforms) and looked at the documentation (http://www.sfml-dev.org/documentation/2.1/classsf_1_1Transform.php)?
Define the transform once, apply the rotation (in degrees), then transform each point. :)
-
Ah, I was accidentally applying the Transform before rotating it. However, I changed it and it still has the same problem. Even if I change the rotation to some other value, such as 45, it still does not rotate.
float offset_x = collide_rect.width / 2 + collide_rect.left;
float offset_y = collide_rect.height / 2 + collide_rect.top;
sf::Transform t;
t.rotate(sprite.getRotation(), offset_x, offset_y);
for(int i = 0; i < 4; i++)
{
t.transformPoint(collide_box[i]);
}