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

Author Topic: [SOLVED] Rotating child transform  (Read 1618 times)

0 Members and 1 Guest are viewing this topic.

stavsen

  • Newbie
  • *
  • Posts: 5
    • View Profile
[SOLVED] Rotating child transform
« on: February 13, 2018, 10:34:34 pm »
Hi
I'm trying to make two sprites have a parent / child transform "relationship" like in unity

I have the following code:

Code: [Select]
int main(int argc, char** argv)
{
   RenderWindow wind(VideoMode(800,400),"test");
   Event event;

   Sprite sprite;
   Texture text;

   text.loadFromFile("C:\\test.jpg");
   sprite.setTexture(text);

   Transform trans;
   trans.translate(100,100);

   Transform parentTrans;
   parentTrans.translate(100,100);


   while(wind.isOpen())
   {

      while(wind.pollEvent(event) )
      {
         if(event.type == Event::Closed)
            wind.close();
      }

      wind.clear();

      parentTrans.translate(0.01f,0.0f);
      //trans.rotate(1.0f);

      wind.draw(sprite,parentTrans);
      wind.draw(sprite,trans*parentTrans);


      wind.display();

   }
   

   return 0;
}

When i run it i see 2 sprites.
One being drawn at 100, 100
and another one being drawn 100, 100 offset from its parent transform, just as expected.

And as i translate the parentTrans in the x direction
they are both affected, just as i want.

The problem is when i try to rotate the "child" sprites transform (trans).

When i uncomment the line where it rotates the sprite,
the child sprite does not rotate around itself (as it moves along in the x direction), as i was expecting.

Instead it rotates around the corner of the screen (0,0) in a circle that gets bigger and bigger as i keep translating the parent transform.

I'm guessing this is because i have to set the sprites origin to its position, right ?

but how can i do that? when the sprites own position actually never changes, and i cant do something like: 
Code: [Select]
trans.getPosition()
Is there some way i can get the a sprites position on the screen?
or is there a better solution to this problem?


Thanks in advance :)
« Last Edit: February 14, 2018, 08:39:42 pm by stavsen »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Rotating child transform
« Reply #1 on: February 14, 2018, 01:15:45 pm »
I suggest to take a look at the SFML Game Development book's node system.

To rotate an object around itself, the origin needs to be set to the center of if you're really just using the transform, you can provide the rotation point as additional parameters.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

stavsen

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Rotating child transform
« Reply #2 on: February 14, 2018, 08:39:14 pm »
Thanks, it was just what i was looking for :)