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

Author Topic: Moving sprite forward (moving it in the direction of it's rotation)  (Read 1571 times)

0 Members and 1 Guest are viewing this topic.

dd

  • Newbie
  • *
  • Posts: 10
    • View Profile
Hi, I'm trying to develop a rougelike game using SFML, and in said game I have a blaster.
I've read the documentation, but I can't figure out for the life of me how to move a sprite in it's pointed direction.
I would really appreciate it if you could tell me how to do this.

Thank you for your time.
« Last Edit: November 09, 2023, 11:06:12 pm by dd »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Moving sprite forward (moving it in the direction of it's rotation)
« Reply #1 on: November 09, 2023, 10:05:35 pm »
To move a sprite, we can use:
sprite.move(v);
where v is a 2D vector representing movement in 2D space.
Usually, this is thought of as x and y so the vector would be an x offset and a y offset.

SFML provides a 2D vector class called sf::Vector2f that is the type passed to sprite.move()

To calculate the x and y offsets, we need to use sine and cosine as usual based on the angle you want to move.

However, if you are using the current SFML 3, an sf::Vector2f can work with angles directly (so calculates the sine/cosine stuff automatically).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

dd

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Moving sprite forward (moving it in the direction of it's rotation)
« Reply #2 on: November 09, 2023, 11:05:24 pm »
It worked perfectly. Thank you a lot.