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

Author Topic: Help with moving object according to rotation  (Read 7061 times)

0 Members and 1 Guest are viewing this topic.

Rdevs

  • Newbie
  • *
  • Posts: 4
    • View Profile
Help with moving object according to rotation
« on: January 12, 2016, 07:39:57 pm »
I would like some help with moving an entity in proportion to it's rotation, For example if it is rotated 30 degrees clockwise, I would like it to move in that line from 30 degrees. I know this seems a fairly basic task/problem, but I don't quite know how to do it exactly. I have read a bit on radians and similar, but my code below doesn't seem to work correctly. This is the closest I have got to it working, but I want it to move in the same linear direction, and when you rotate it, it works correctly.


//convert the angle of the object to radians
radians = 3.1415926536 / 360*object.getRotation();

//x and y
x = 0.5f*cos(radians);
y = 0.5f*-sin(radians);
//rotate accordingly
object.move(x, y);

 

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Help with moving object according to rotation
« Reply #1 on: January 12, 2016, 08:02:57 pm »
//convert the angle of the object to radians
radians = 3.1415926536 / 360*object.getRotation();
 

This isn't the right formula for converting degrees to radians (assuming object.getRotation() is returning degrees). It either needs to be ((2 * pi / 360) * degrees) or ((pi/180) * degrees)


Rdevs

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Help with moving object according to rotation
« Reply #2 on: January 12, 2016, 08:14:43 pm »
I see, ok so I changed it to 180, but unfortunately it is not working as it should. I think the rotation of it isn't working properly. When the object is moved linearly, the rotation goes incorrectly. I want the angle to be the rotation around the same point (the center of the object) if you see what I mean.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Help with moving object according to rotation
« Reply #3 on: January 12, 2016, 08:35:59 pm »
I can't understand exactly what you are trying to do and what the problem is. Can you first explain what you want your program to do and then explain what exactly is going wrong? The code snippet you provided doesn't show much and I don't really understand when you say "When the object is moved linearly, the rotation goes incorrectly". Do you just mean that your object isn't moving at the angle you expect? Can you provide an example of the angle you are setting and the resulting incorrect behavior? It would also help if you can show a complete (but stripped-down and minimal) version of the code you currently have so we can help you debug it.

Rdevs

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Help with moving object according to rotation
« Reply #4 on: January 12, 2016, 08:37:06 pm »
To explain a bit more, what I would like is to replicate the controls of the object you control in the 2d game asteroids, please can someone tell me what I need to do in order to achieve this?  ;)

Update: @Arcade, As above basically I would like the controls to be like the game Asteroids. it doesn't go in the direction I expected when the object is rotated.
« Last Edit: January 12, 2016, 08:38:59 pm by Rdevs »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Help with moving object according to rotation
« Reply #5 on: January 13, 2016, 02:29:55 am »
This is quite a common problem as such you'll find a lot of answers on the internet (e.g. here) or even this forum (e.g. here or here ).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with moving object according to rotation
« Reply #6 on: January 13, 2016, 02:57:43 am »
I normally see:
x = sin
y = -cos
not
x = cos
y = -sin

Could you describe a little more about what you mean by "not working", "not as it should" and "the direction I expected"?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Rdevs

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Help with moving object according to rotation
« Reply #7 on: January 13, 2016, 04:53:12 pm »
It's hard to describe. In best words, it's point that it rotates around doesn't seem to be consistent. For example, when rotated 30 degrees, it moves in that direction as it should, but then when you change the rotation of the object to, say 60 degrees, it goes in a random direction that is not proportional to it's original direction.
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
                object.rotate(0.5);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
                object.rotate(-0.5);
        }

        X = speed*sin(object.getRotation());
        Y = speed*cos(object.getRotation());
       

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
                object.move(X, Y);
        }
       
 
« Last Edit: January 13, 2016, 04:55:04 pm by Rdevs »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Help with moving object according to rotation
« Reply #8 on: January 13, 2016, 05:29:11 pm »
SFML works with degrees. C++ trigonometry functions work with Radian. So you need to convert between degrees and Radians and ther other way around.

Also keep in mind that rotate() rotates relative to the previous rotation, while set/getRotation() work with the absolute rotation.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: Help with moving object according to rotation
« Reply #9 on: January 13, 2016, 09:03:45 pm »
Why not use vector math for that?
Just construct a unit vector pointing in the initial direction, apply the rotation with a Transform object multiply that with your speed and you are good.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with moving object according to rotation
« Reply #10 on: January 13, 2016, 09:27:29 pm »
   X = speed*sin(object.getRotation());
   Y = speed*cos(object.getRotation());
I normally see:
x = sin
y = -cos
:P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*