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

Author Topic: Movement in direction of a heading  (Read 7555 times)

0 Members and 1 Guest are viewing this topic.

gpnk90

  • Newbie
  • *
  • Posts: 3
    • View Profile
Movement in direction of a heading
« on: March 08, 2016, 04:47:48 pm »
Hi,

I'm looking to move an object in a similar way to that of a ship. The user is to enter the direction the ship should sail (i.e 300 degrees) and the ship should then move in the direction of WNW on a heading of 300. Is there anywhere that explains the rotation system used by sfml? As currently a heading of 0 moves my object South.

I hope that makes sense, any help or pointers would be appreciated!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Movement in direction of a heading
« Reply #1 on: March 08, 2016, 04:56:59 pm »
Rotation in SFML is the rotation (in degrees) of the actual visual representation. SFML also has an actual position.
For movement of the usual transformables, you can either set a new position or use the move() method to specify just the offset.
If you wish to move an object a at a specific angle, you will need to calculate the horizontal and vertical offsets yourself.
There are lots of resources about doing so (google it!) and there are libraries that do it for you too. Thor, for example, has Polar Vectors, which could come in handy for you here.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Movement in direction of a heading
« Reply #2 on: March 08, 2016, 05:02:14 pm »
there is another way using sfml sf::transform, if you multiply movement on rotation(in this order) the object will move in direction of its rotation.

Have a look at matrix multiplication.

gpnk90

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Movement in direction of a heading
« Reply #3 on: March 08, 2016, 05:06:07 pm »
The objects I'm moving have their own position and angle, the movement is dealt with and then the resulting position is then used to update the sfml position. Does this change any of the advice?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Movement in direction of a heading
« Reply #4 on: March 08, 2016, 05:10:14 pm »
The objects I'm moving have their own position and angle, the movement is dealt with and then the resulting position is then used to update the sfml position. Does this change any of the advice?
This sounds like you've done what you are asking about.
What is it you are having trouble with?

there is another way using sfml sf::transform
...
Have a look at matrix multiplication.
This is much more complicated that calculating x and y for an angle.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

gpnk90

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Movement in direction of a heading
« Reply #5 on: March 08, 2016, 05:19:36 pm »
Sorry, I should have explained better in my first post!

I have a ship, with a position (2D vector), angle (int), velocity (2D vector).

The objects position is updated:

Quote
vel.x = sin((PI/180)*angle) * speed * time;
vel.y = cos((PI/180)*angle)* speed * time;

pos = pos + vel;

pos.x and pos.y is then used to set the objects sfml pos. (draw.setPosition(pos.x, pos.y)



Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Movement in direction of a heading
« Reply #6 on: March 08, 2016, 09:29:57 pm »
This sounds like you've done what you are asking about.
What is it you are having trouble with?
You know how to move the object by any amount at any angle so I don't understand the problem.
Is it that you don't know what the current rotation is or want to offset that rotation?

sprite.setRotation(x) - sets the current rotation to x
sprite.getRotation() - gets the current rotation
sprite.rotate(x) - rotates by x - effectively the same as sprite.setRotation(sprite.getRotation() + x))
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*