# Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

### Newcomers AppGameKit Corner / How do i convert two 3d points to x/y/z angles

Message
Posted: 29th Aug 2018 01:12
Like the title says i have x1,y1,z1 and x2, y2, z2
How do i convert those values to angles xa, ya, za
thanks in advance for any help
Posted: 29th Aug 2018 13:37
Well, you can't exactly. Whether you form a line segment from the two points or separate segments from each point to the origin [0,0,0], you'll only get two relevant angles. Because on one of the axes the rotation will have no effect on it's 3D position.

I'm not sure if I explained that clearly enough. But basically, a 3D point is really only going to give 2 angles from how I understand it.

You can *try* this, but I'm not 100% certain on the math

xa = atan2(sqrt(y1^2 + z1^2), x1)
ya = atan2(sqrt(x1^2 + z1^2), y1)

Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Posted: 29th Aug 2018 13:41
Can you clarify what angles you actually want to get??

Do you want the angles needed to rotate an object (at x1,y1,z1) to face another object (at X2,Y2,Z2)?
Do you want the angle between the two vectors?
Do you want the angles between the axis planes and the vector between the 2 points given?

It all a case of either using maths = acos(dot(nv1,nv2))

Or you can use the SetObjectLookAt function and GetObjectAngleX() Y() Z()
Posted: 29th Aug 2018 13:42
Can be very tricky, I'm sure you've found the ATANFULL commands already. ATANFULL will let you work out an angle from a height and length - so the Y angle should be straightforward. If you calculate the X and Z distance,, you have a method to calculate the X angle... ATANFULL(xb-xa,dist). The Z angle is roll, which you probably wouldn't want to use, or you'd probably want to handle roll differently.

So... calculate the difference between all values...

DX#=XA#-XB#
DY#=YA#-YB#
DZ#=ZA#-ZB#

Calculate XZ distance...

DXZ#=sqrt((DX#*DX#)+(DY#*DY#))

Calculate Y angle from XZ
AY#=atanfull(dx#,dz#)

Calculate X angle from DY
AX#=atanfull(dy#,DXZ#)

This is all untested, I could make an example for you, but I think you'll figure it out regardless - it's usually just that X angle that is tricky to calculate.
The code is dark and full of errors
Posted: 29th Aug 2018 21:28
thanks you very much guys!
@bengismo: I think i would like the o2 object to face the o1 object
Posted: 29th Aug 2018 21:52 Edited at: 29th Aug 2018 21:54
That's really very simple....You just use SetObjectLookAt( objID, x, y, z, roll )

The coordinates you pass in are the coordinates of the object you want to look at. So you can just use one line of code to do it

SetObjectLookAt( obj2, GetObjectX(obj1), GetObjectY(obj1), GetObjectZ(obj1), 0 )

This makes obj2 rotate to face obj1. If obj2 is a turret of guns - its guns will face object 1 no matter where obj1 is.

I can post the maths on how the angles are worked out for this function if wanted but you dont really need them.
Posted: 29th Aug 2018 22:49
Yes!!!!! Thanks so much!
Posted: 30th Aug 2018 15:06
One nice trick to add to the SetObjectLookat is storing the location, moving forward 1 unit, then checking the difference as a vector - that turns your angles into a normalised vector very easily - great for firing bullets, throwing items etc etc. It can help a lot with physics ray casting as well, being able to just used the camera to calculate its own forward vector is great.
The code is dark and full of errors
Posted: 30th Aug 2018 21:10
Nice idea! Thanks