0
\$\begingroup\$

Assume, that we have a player that has a vector direction. We have an enemy, that is shooting a player. And the task is to find out from which side the enemy is shooting. I know the algorithm, how to find this.We need to find the left vector and then do a dot product with enemy direction vector. The question is to find the left or right side of player's current vision and it means to find out the vector perpendicular to the player's vector. What's the easiest and the most least computation way to find it.

\$\endgroup\$
9
  • \$\begingroup\$ I don't think you can really have a normal to a vector, since normals are for planes. A single vector doesn't define a plane (unless of course that vector IS the normal) \$\endgroup\$
    – jhocking
    Sep 25 '14 at 20:22
  • 1
    \$\begingroup\$ For this sort of 3d stuff adding a picture of what you are talking about can be very helpful. \$\endgroup\$ Sep 25 '14 at 20:45
  • 1
    \$\begingroup\$ This question appears to be off-topic because it is about mathematics in a way that is unrelated to game development. \$\endgroup\$
    – jmegaffin
    Sep 25 '14 at 20:47
  • 1
    \$\begingroup\$ Hint: rotate the vector 90° around the vertical axis. \$\endgroup\$ Sep 25 '14 at 21:23
  • 1
    \$\begingroup\$ bool is_left=dot(cross(forward, up), enemy_dir)>0; \$\endgroup\$
    – JarkkoL
    Sep 26 '14 at 0:29
2
\$\begingroup\$

In order to compute left and right, you need the concept of "up". For instance, if you are flying a spaceship and performing a "barrel roll" then the concept of "up" changes and with it, left and right change as well although you are facing in the same direction. Up can be based on the plain the character is standing on or simply be an absolute direction.

ilustration

Once you figure out "up" you simply do the cross product like @JarkkoL already explained. If you multiply with the reverse order, you"ll get the opposite direction. So in other words, if left == cross(forward, up) than right == cross(up, forward). You can also simply negate one to compute the other.

\$\endgroup\$
2
  • \$\begingroup\$ also the handedness of your coordinate system will dictate which left/right vector you get \$\endgroup\$ Sep 26 '14 at 9:35
  • \$\begingroup\$ Thx for the answer, I found out that my concept of up is irrelevant. That's because I only care about left/right it means that only plate XY considered. And then it comes to easy problem solving with just finding 2 dot products. \$\endgroup\$ Sep 26 '14 at 9:58

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.