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

Author Topic: Angle between two vectors  (Read 6284 times)

0 Members and 1 Guest are viewing this topic.

mathme

  • Newbie
  • *
  • Posts: 8
    • View Profile
Angle between two vectors
« on: July 16, 2013, 09:37:22 pm »
I have been trying out different methods all day to calculate the angle between 2 vectors. I've browsed countless forums and read way too many stackoverflow questions. No matter what method I implement I keep getting the same result.

The angle I'm getting is only good in the bottom right quadrant of the player, and is only exactly correct at 45 degrees.

I've tried the dot product way with acos, I've tried the atan2 method with delta X and delta Y. I just can't seem to get a working function.

The goal is to be able to aim a bullet from the player to the mouse.

Does anyone have a function that works to do this?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Angle between two vectors
« Reply #1 on: July 16, 2013, 09:48:24 pm »
The goal is to be able to aim a bullet from the player to the mouse.
Why would you need an angle for that? Just take the difference vector from both positions.


Does anyone have a function that works to do this?
Polar angle of single vector: thor::polarAngle()
Signed angle between two vectors: thor::signedAngle()

Check the API documentation of Thor.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

cpolymeris

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
    • Email
Re: Angle between two vectors
« Reply #2 on: July 17, 2013, 01:20:44 am »
Does anyone have a function that works to do this?

The Law of Cosines works. If it doesn't, you must be doing something wrong (or not be in Euclidian space, but you would have said if that was the case  :)) ). Since you don't go into detail, here are some things to check:
  • You are using the right vectors. If player position is P and mouse is M, the angle from P to M, taking the Y axis as reference is acos([0,1] dot (M - P))
  • You are not mixing radians with degrees. Remember SFML uses the later, while most other libraries, including <math> use radians.
  • Mouse position is in the same coordinate system as player. See MouseToCoord and similar functions in renderTarget

I know it's kinda obvious stuff, but since you don't supply code, I am not sure what else you could be doing wrong.

Edit: If you need to know the direction (which point is behind the other), remember to use acos2, or that atan2 function you mention.
« Last Edit: July 17, 2013, 01:25:12 am by cpolymeris »

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Angle between two vectors
« Reply #3 on: July 19, 2013, 04:37:37 pm »
The goal is to be able to aim a bullet from the player to the mouse.

You need a direction vector (mouse position - player position)
Then normalize that vector (make it a unit vector)
Now, add this normalized vector to your bullet velocity and multiply by the bullet movement speed (a float) and the delta time (as float)

- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Angle between two vectors
« Reply #4 on: July 19, 2013, 06:46:04 pm »
Quote from: mateandmetal
You need a direction vector (mouse position - player position)
Then normalize that vector (make it a unit vector)
Now, add this normalized vector to your bullet velocity and multiply by the bullet movement speed (a float) and the delta time (as float)

depending on what hes using for a bullet image, he might also need to rotate the image to match that of the player. However this is fairly simple, just use the players current angle at the time of the shot and set your bullet to that.
« Last Edit: July 19, 2013, 06:57:21 pm by Gobbles »

 

anything