Jump to content

View more

Image of the Day

雑魚は多めにして、爽快感重視にしつつ・・・(´・ω・`)
早いとこ、ベースを作って、完成にもっていかないとね。
タイトルもまだ迷ってるだよなぁ。 
#indiedev  #indiegame #screenshotsaturday https://t.co/IwVbswGrhe
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Relative angles with dot product (2D)

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
4 replies to this topic

#1 ViniGodoy   Members   

100
Like
0Likes
Like

Posted 05 August 2011 - 01:09 PM

Hi.

I recently found a dot product relative angle calculation for unit vectors as follows:

float Vector2D::angleBetween(const Vector2D& other) const
{
   float dp = dot(other);

   if(dp >= 1.0) dp = 1.0f;
   else if(dp <=-1.0) dp = -1.0f;

   float angPi = (float)acos(dp);
 
   //side test
   return y*other.x > x*other.y ? -angPi : angPi;
}


But i could not figure out where this "side test" came from.
Does it work? And how?

I usually use the traditional atan2 - atan2 calculation, but if this solution is really correct I could use in machines where this atan2 is not implemented.

#2 ViniGodoy   Members   

100
Like
0Likes
Like

Posted 05 August 2011 - 01:31 PM

Sorry, just now I realised that this is a disguised test of the perp dot product signal.

#3 Dirk Gregorius   Members   

2709
Like
-1Likes
Like

Posted 05 August 2011 - 02:00 PM

You could simply do this:

angle = atan2( abs( cross( v2, v1 ), dot( v2, v1 ) );

#4 Álvaro   Members   

20913
Like
0Likes
Like

Posted 05 August 2011 - 02:51 PM

You could simply do this:

angle = atan2( abs( cross( v2, v1 ), dot( v2, v1 ) );


Man, you should read the question before giving out some canned answer.

#5 Emergent   Members   

982
Like
1Likes
Like

Posted 07 August 2011 - 03:20 PM

Also, just FYI, that" canned answer" is a bit wrong. Although the norm of the cross product gives you the magnitude of the sine of the angle, it doesn't give the sign, which is really what matters. Without that, you have no more information than the cosine gives you. You need the full determinant, sign included. I liked Vini's description as "perp dot product."




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.