Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411526 Posts in 69381 Topics- by 58436 Members - Latest Member: GlitchyPSI

May 02, 2024, 12:02:59 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Simple trig
Pages: [1]
Print
Author Topic: Simple trig  (Read 1127 times)
Tenderfoot
Level 0
**



View Profile
« on: December 09, 2009, 02:13:44 PM »

Hey,

Sorry, I know theres probably another thread that discusses this.

I have a function that uses arctan to find an angle (between 2 points in 2D space), but I don't get the right value unless I multiply the result by negative 2:

float CalcTheta( CObject Point1, CObject Point2 )
{
   float Theta;   
   Theta = std::atan( (Point2.y - Point1.y) / (Point2.x - Point1.x) );
   return -2*Theta*(180/PI);
}

why??
Logged
increpare
Guest
« Reply #1 on: December 09, 2009, 02:17:02 PM »

1: Don't use atan use atan2f (lest they be on the same x coord)

2: I don't know why.  Give me a definite input and output values and then I might know why.  I sort of don't believe that you're not getting the correct values without the -2.
Logged
Tenderfoot
Level 0
**



View Profile
« Reply #2 on: December 09, 2009, 02:26:05 PM »

http://www.grantmcneil.com/Test.zip

Its a simple SDL/OpenGL app in the newest NeHe basecode;

The calctheta function and its use are in "Lesson01.cpp"

The program uses GLUnProject to get x and z coordinates of the mouse in 3d space, and uses that to calculate an angle between that point and the box in the middle (projecting vector (0,1)). It then rotates the box in the middle to face the mouse. You'll see what i mean.

its a vc++ project.

And what is the difference between atan and atan2?
« Last Edit: December 09, 2009, 02:33:37 PM by Tenderfoot » Logged
Zaphos
Guest
« Reply #3 on: December 09, 2009, 02:46:11 PM »

Wiki explains atan2 in detail: http://en.wikipedia.org/wiki/Atan2
short answer: atan(dy/dx) has singularities (for dx = 0) and ambiguities (since dx=-1,dy=-1 is indistinguishable from dx=1,dy=1) which atan2(dy,dx) removes by taking dy and dx as separate arguments.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic