Skip to main content
gamedev.net
🔒 Locked

simple trig problem?

Started by dense Jan 18, 2002 at 3:29 PM 2 replies 550+ views
Original Post
dense
dense
Hi, I have what seems to be a simple trig problem, but for some reason it doesn''t quite work how it seems it should. Here is a diagram of what my problem looks like (hope this works) A------B \ | \ | \ | \ | \ | \| C In the case of my program, A is the camera, C is the point in space I''m wanting to look at, and the angle at A represents the pitch of the camera, which is what I''m trying to find. I know distances AB, AC, and BC. So, to find angle A I would think I just need to do the inverse cosine function of AB/AC, right? When done on a calculator, this works fine and get the right answer, but in my program it''s different. I know it''s calculating AB and AC correctly, and the only other thing I do is acos(AB/AC). For some reason though, it comes out as 0 every time. As far as I know, acos is the inverse cosine function, or is it something else? I''m either doing something stupidly wrong, or there is different math involved when dealing with 3d stuff, but it seems it shouldn''t matter. Any help is appreciated.
krez
krez
are you using degrees instead of radians for your angles? not to insult your intelligence, but you said it works on a calculator so there must be something you are assuming wrong in your code...

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Jesper T
Jesper T
Hmm the way you mentioned should work, but there is also a general way to get an angle between two vectors (in any dimension)

Say the vector from point A to point B is:
AB = {ABx, ABy, ABz} found like this: {Bx-Ax, By-Ay, Bz-Az}

and AC = {ACx, ACy, ACz} (use the same method as above to get it)

then the angle between them can be found like this:

cosine = ABx*ACx + ABy*ACy + ABz*ACz / ( sqrt(ABx*ABx+ABy*ABy+ABz*ABz) * sqrt(ACx*ACx+ACy*ACy+ACz*ACz) )

angle = acos(cosine)

note that is gives you the angle in radians, to convert it to degrees just:

degAngle = 180.0 * angle / PI
(PI = 3.1415...)


edit: Just read krez's reply, and I think he might be right that you are treating radians as integers degrees and thus flooring it to zero

Edited by - Jesper T on January 18, 2002 4:46:16 PM
dense
dense
Well that was the problem, thanks guys.

*slaps himself*

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.