|
-
Sep 4th, 2008, 08:11 PM
#1
Thread Starter
Lively Member
Orientation of a character in 3d world
what is a formula to figure out what your orientation needs to be to face an object. for example if im standing at 100,200 on a map and my orientation is 30 and i want to turn and face an object thats located at 400,50 how can i figure out what orientation i need to walk there in a straight line?
-
Sep 4th, 2008, 11:19 PM
#2
Re: Orientation of a character in 3d world
I think Direct3D uses normal vectors instead of angles to find out which direction an object is facing. They're much more natural for a 3D environment.
But for the 2D situation you described, you'll want to use some inverse trig functions. The easiest thing I can think of doing is using a dot product relation to find the angle between north (0 degrees) and the line to your destination.
C = <cx, cy> = character position vector [= <100, 200> from your example]
D = <dx, dy> = destination position vector [= <400, 50>]
E = D - C = <dx-cx, dy-cy> = vector pointing from character to destination [= <300, 150>]
N = <0, 1> = vector pointing north
Get the angle a between E and N. Use the relation E dot N / (|E||N|) = cos a. |E| = Sqrt(300^2+150^2) = 335.41, |N| = 1 similarly. E dot N is always ex*nx + ey*ny = ex*0+ey*1 = ey = dy - cy. Take the inverse cosine and simplify to get
a = arccos(ey / Sqrt(ex^2+ey^2)).
This has to be adjusted, unfortunately, since angles > 180 degrees clockwise will instead be reported using their counterclockwise measurement. This is easy to correct for, though, in the following way:
If ex >= 0 Then
angle = arccos(ey / Sqrt(ex^2+ey^2))
Else
angle = 360 - arccos(ey / Sqrt(ex^2 + ey^2))
End If
This could probably be done more elegantly, but I didn't want to draw pictures....
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|