0
\$\begingroup\$

Couldn't find this answered already, surprisingly.

For example, for a game (in my case a 2D top-down one), there could be advantages to doing so. For example, whenever you want to change an entity's direction, you just assign entity.direction = unitVector and bam, the direction is changed.

Similarly, if you want to apply a powerup that increases their speed, you just do entity.speed += 5, since it's a scalar value.

However, I also see advantages to keeping them combined. For one, a vector is already the combination of magnitude and direction, so it feels natural to combine into a single value. Secondly, it prevents desync. For example, if you have some sort of function that modifies the an entity's velocity, you have to constantly check whether you're dealing with direction or speed, and also be sure to update both values independently, which can be confusing.

How is this generally handled in the majority of cases? Are they combined, or not? And if they are combined, what are the recommended ways of changing just the speed, or changing just the direction of this combined vector value?

\$\endgroup\$
3
\$\begingroup\$

The speed vector, as all vectors, is already made of a magnitude and a direction. More specifically, you can compute the magnitude and direction from the vector.
Take for example the vector \$ \vec{v} = \begin{pmatrix} 1 \\ 1 \end{pmatrix} \$ . You can extract the magnitude (\$ \sqrt{2} \$ ) and the direction (\$\begin{pmatrix} 1 \\ 1 \end{pmatrix} \$, 45° assuming 0° is horizontal pointing to the right).
Were you to split that info into a vector and a scalar, you'd be using a 2D vector and a float instead of just a 2d vector.

Storing the vector allows you to make the computation you need. Powerup? Multiply the vector by a scalar, it'll be the same as multiplying the magnitude. Need to change only the direction? Either use quaternions, rotation matrices or plain trigonometry depending on your engine.

\$\endgroup\$
6
  • \$\begingroup\$ Yeah, getting the magnitude and direction seems easy enough. So does multiplying the speed by a scalar. But I'm not exactly sure how to change the direction. It's just a 2D vector in a 2D top-down game, so how would I do that then? That is, if my current vector is x: 3, y: 3 (so the speed would be 4.2 with that, I think?), how would I then change the speed to be the same in the southern direction? Just multiply 4.2 by the southern unit vector, I suppose? So just extract the magnitude and then multiply it by the new direction's unit vector? \$\endgroup\$ – Ryan Peschel Mar 29 at 22:22
  • \$\begingroup\$ @RyanPeschel If you know the new direction and need to keep the same speed (4.2 is right for your example), that's indeed the way to go. You said it but I repeat it because I think everyone made the mistake one day: the new direction vector must be a unit vector. \$\endgroup\$ – Sacha Mar 30 at 7:17
  • \$\begingroup\$ Wait, what is indeed the right way to go? I can't tell if you're telling me to keep them a single vector or to split them up \$\endgroup\$ – Ryan Peschel Mar 30 at 16:24
  • \$\begingroup\$ @RyanPeschel Keep them together, store them in a single 2D vector variable. When you need the magnitude or the direction, compute it from the vector variable. \$\endgroup\$ – Sacha Mar 30 at 16:26
  • \$\begingroup\$ Gotcha, thanks. \$\endgroup\$ – Ryan Peschel Mar 30 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.