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

Author Topic: Velocity visualizer  (Read 1202 times)

0 Members and 1 Guest are viewing this topic.

Electrovicane

  • Newbie
  • *
  • Posts: 1
    • View Profile
Velocity visualizer
« on: January 17, 2024, 07:36:27 pm »
I'm in the process of making a 2D platformer with SFML and want to make an arrow sprite attached at the center of the player sprite to show the vector of the player's current velocity. I'm able to get the origin at the center. Can anyone recommend the best way to rotate about a central axis while varying the size based on magnitude?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Velocity visualizer
« Reply #1 on: January 17, 2024, 08:35:50 pm »
If the arrow is a sprite/quad or some other transformable, you can simply set its origin to match the point of rotation (start of the arrow or centre depending on your preference), set its position to the centre of the player sprite and then scale it based on the velocity. Assuming your unrotated arrow points to upwards, you would need to scale the y size only to stretch its length (this is not affected by the rotation).

If you just need a vector, using a polar vector is the simplest way.
If you're using SFML 3, you'll already be able to do some of that. SFML 3's sf::Vector2 can allow you to set its rotation angle so you'd just need to set it to a "flat" vector at the correct length and then set its angle.
If you're not using SFML 3 - or you need to set its length directly - you could use an actual polar vector. Plinth and Thor both provide these. You could also do the calculations yourself. It's actually not too hard: calculate its length, divide the vector by that length to get its unit vector and then multiply by the preferred length. Remember to make sure you're not dividing by zero though!

If a stretched arrow isn't even that important, you could just scale the arrow fully so that its width also increases.

Of course, you'd also need to know the player's velocity and have mapped that to the scale you want. e.g. 0-100 speed -> 0-1 ratio -> 0-200 arrow length.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*