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

Author Topic: Rotation rectangle around object  (Read 1446 times)

0 Members and 1 Guest are viewing this topic.

addepadde

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Rotation rectangle around object
« on: April 26, 2022, 02:25:15 pm »
Hey guys, im trying to make a game where you can controll a player around a object.

Key press A: Rotates counter clockwise.
Key press D: Rotates clockwise.
Key press W: Moves player away from centerobject, (opposite direction).
Key press S: Moves player towards centerobject.

Im having trouble getting the moment for W and S to work as i want it to...

For rotation i did :
    globe.setOrigin(125.00f, 125.00f);
    globe.setPosition(window.getSize().x /2.00f, window.getSize().y / 2.00f);
    globe.setTexture(&globe_texture);
   
    player.setOrigin(globe.getOrigin().x, globe.getOrigin().y);      // Player get the same central as the globe.
    player.setPosition(globe.getPosition().x, globe.getPosition().y);

For A and D:
        if (Keyboard::isKeyPressed(Keyboard::Key::A))
            {
                counter = 0;
                player.rotate(-0.5f);    // Counter Clockwise.
            }
        if (Keyboard::isKeyPressed(Keyboard::Key:: D))
            {
                counter = 0;
                player.rotate(0.5f);    // Clockwise.
            }

Thx! :)




« Last Edit: April 26, 2022, 03:01:23 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Rotation rectangle around object
« Reply #1 on: April 26, 2022, 05:32:58 pm »
While you can achieve some things by setting and resetting the origin point, it's closer to what you're actually doing and more flexible to use linear algebra and trigonometry.
For the circular movement (good keyword to web search), you'd use cos and sin.

Here's some short article nicely explaining this (with JavaScript): https://franzeus.medium.com/math-for-circular-object-movement-in-a-game-cae101474a65

Once you have that, the rotation of the player (if so desired), would require you to calculate the direction vector from which you can then determine necessary rotation angle.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

addepadde

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Rotation rectangle around object
« Reply #2 on: April 26, 2022, 06:26:51 pm »
Thx for the link :), Ye i tried to calculate it and just solve it with trig math without success.

 I also tried to use transform and rotate the transform around the center and then apply the transform to the player in order to not have to change and rechange the origin of the player all the time.


EDIT:
Solved the rotation part thanks to your link :)
« Last Edit: April 26, 2022, 07:05:32 pm by addepadde »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Rotation rectangle around object
« Reply #3 on: April 28, 2022, 08:35:16 pm »
Adapting some code from my Rotating Triangle Example, you should be able to achieve the rotation with something like this:

auto centerPoint= sf::Vector2f{ 20.f, 20.f };
auto deltaPosition = sf::Vector2f{ triangle.getPosition().x - centerPoint.x, triangle.getPosition().y - centerPoint.y };
auto rotation = to_degree(atan2(deltaPosition.y, deltaPosition.x)) + 90.f;
 
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything