Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Rotating Help  (Read 3196 times)

0 Members and 1 Guest are viewing this topic.

collechess

  • Newbie
  • *
  • Posts: 10
    • View Profile
Rotating Help
« on: August 31, 2013, 10:04:28 pm »
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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Rotating Help
« Reply #1 on: August 31, 2013, 10:22:56 pm »
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?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

collechess

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Rotating Help
« Reply #2 on: August 31, 2013, 10:34:43 pm »
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.   

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Rotating Help
« Reply #3 on: August 31, 2013, 11:36:58 pm »
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. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Kojay

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: Rotating Help
« Reply #4 on: August 31, 2013, 11:44:32 pm »
sf::Sprite::getGlobalBounds takes into account transformations and returns a sf::FloatRect. Conveniently, the latter has a function to check intersection with other FloatRects.

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Rotating Help
« Reply #5 on: September 01, 2013, 12:11:49 am »
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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Rotating Help
« Reply #6 on: September 01, 2013, 12:22:13 am »
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, which is what I believe collechess is trying to implement. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

collechess

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Rotating Help
« Reply #7 on: September 01, 2013, 01:12:56 am »
Quote
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?
Quote
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.
« Last Edit: September 01, 2013, 01:35:25 am by collechess »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Rotating Help
« Reply #8 on: September 01, 2013, 01:41:04 am »
Quote from: Google is awesome
deg = rad * (180/PI)
rad = deg * (PI/180)

Have your read the SFML tutorial and looked at the documentation?

Define the transform once, apply the rotation (in degrees), then transform each point. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

collechess

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Rotating Help
« Reply #9 on: September 01, 2013, 01:58:13 am »
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]);
                       
}