Author Topic: Info on calcAngle on SimpleVector  (Read 4890 times)

Offline Ulrick

  • byte
  • *
  • Posts: 18
    • View Profile
Info on calcAngle on SimpleVector
« on: October 21, 2010, 06:13:54 pm »
Hi all,

just a quick question.

Probably in known to everyone, but I cannot understand why calcAngle return a 180 degree angle (I mean values from 0 to 180).

It could be possible to have it 360 degrees (means from 0 to 360)? or from -180 to +180?  Otherwise, how can I calculate angles from 0 to 360 between two SimpleVectors?

Probably I missed something.

Thanks in advance,

Ulrick

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Info on calcAngle on SimpleVector
« Reply #1 on: October 21, 2010, 08:46:00 pm »
It's simply the closest angle between two SimpleVectors and it has no direction, i.e. the angle between a and b is the same as between b and a. And it never exceeds 180°, because it's the closest angle...and that can't be larger than 180°. The actual formula is simple (in pseudo code):

Code: [Select]
nv1=norm(v1)
nv2=norm(v2)

d=dot(nv1, nv2)

angle=acos(d)

If you need something else, google might be your friend. I've already tried briefly, but i couldn't come up with something really better...just with different solutions to do the same thing...

What do you want with that angle? Maybe there's another solution!?

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Info on calcAngle on SimpleVector
« Reply #2 on: October 21, 2010, 11:31:35 pm »
360 degrees doesn't make sense when you consider the fact that the two vectors can be any orientation.  There are always going to be two arcs you can measure angles for (and if you know one you know the other since they add to 360 degrees).  How would you specify which arc is the one you want to measure?  That's a question which depends on each particular project.  You have to take the information jPCT gives you and decide which arc you are looking for.  I can probably help with this if you let me know what you are after (I've used this extensively in the auto navigation system I wrote for the game I'm working on).

Offline Ulrick

  • byte
  • *
  • Posts: 18
    • View Profile
Re: Info on calcAngle on SimpleVector
« Reply #3 on: October 22, 2010, 03:19:36 pm »
You are right, but do you think could be possible to add also a direction vector in calcAngle in order to have a 360 degree angle evaluation?

What I actually need is to get a space orientation in the 3D world of an object. This is needed to have a correct rapresentation of a map view when moving in all direction of the surrounding elements.

As an example, image to have a space game where a ship can move in any direction, How do you represent a radar view of enemy ships? imagine that you could be upside down or on one side while moving in space.

-- Update --
Actually, would be interesting, and I believe usefull, to have an extended class of the SimpleVector witch take into account also the direction vector.

-- Update2 --

After the suggestion of EgonOlsen and the description on how does it work, I found out a possible work around, but I will need to work on it to make it in one step.

What I do is the following:

SimpleVector V1=plane.getTransformedCenter();
SimpleVector V2=cam.getDirection();
                     
V1.y=0;
V2.y=0;
                     
float Angi= (float) (Math.atan2(V1.normalize().z,V1.normalize().x)-Math.atan2(V2.normalize().z,V2.normalize().x));

This should work for me. Do you think it is ok? The radar view is just a "top view" of the area and surrounding elements are displayed smaller if at a lower altitude or bigger if higher.         

Anyway both of you gave me already a good hint to start on.

Thanks a lot,

Ulrick
« Last Edit: October 22, 2010, 04:43:07 pm by Ulrick »

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Info on calcAngle on SimpleVector
« Reply #4 on: October 22, 2010, 11:37:08 pm »
That looks like it should work.  I came up with another way to do something similar to this (a single-axis billboarding effect I needed to add glow effects to cylindrical objects).  In case you run into any issues with your formula, I can give you another way to do the same thing.

Offline Ulrick

  • byte
  • *
  • Posts: 18
    • View Profile
Re: Info on calcAngle on SimpleVector
« Reply #5 on: October 27, 2010, 12:57:31 pm »
Thanks paulscode,

the one I made work great!!.

Thanks again,

Ulrick

Offline AugTech

  • int
  • **
  • Posts: 64
    • View Profile
    • Augmented Technologies
Re: Info on calcAngle on SimpleVector
« Reply #6 on: January 09, 2014, 01:32:11 pm »
I know its an old thread, but after working on this for some time I found the following link that provides a solution to create an angle between -180' and +180' (in rads)

http://answers.unity3d.com/questions/24983/how-to-calculate-the-angle-between-two-vectors.html

Hope this helps someone else!