### <CMath>Getting angles between 2 3d points.

I need to get a cannon to schoot on my target.
It is a 3d game.

I need to get 2 angles between the x,y and z of the cannon, and the target.

I can calculate 1 of the angles:
 123456 float getAngle2 (float x1,float y1,float z1,float x2,float y2,float z2) { float dist=sqrt(pow(x1 - x2,2) + pow(y1 - y2,2) + pow(z1 - z2,2)); float dist2=sqrt(pow(x1 - x2,2) + pow(z1 - z2,2)); return acos(dist2/dist)*180/3.1415926; }

That's the angle on the y-plane.

But now I need to get the angles of the x-z plane.

Does anyone know how to do that?

Last edited on
It looks like your are getting the angle on xz-plane, try using asin
v_j w_j = |v| |w| cos \theta

> I need to get 2 angles between the x,y and z of the cannon, and the target.
¿2 angles?
@ne555
Yes I need 2 angles, because it's 3d.
See this image, to understand it.
http://updo.nl/file/ba07bcbe.png

@naraku9333
I tried that, but because the target moves, I cant get the opposite.
Three not aligned points define a plane.
The angle between the vectors is in that plane.

As I see in your drawing, you want to compute the angle of the projections.
w_j = v_j - v_k n_k n_j where n_j' is the normal of the plane.
 1234567891011 float getAngle (float x1,float y1,float z1,float x2,float y2,float z2) { float theta = atan2(z1-z2,x1-x2); return -theta*180/3.1415926; } float getAngle2 (float x1,float y1,float z1,float x2,float y2,float z2) { float dist=sqrt(pow(x1 - x2,2) + pow(y1 - y2,2) + pow(z1 - z2,2)); float dist2=sqrt(pow(x1 - x2,2) + pow(z1 - z2,2)); return acos(dist2/dist)*180/3.1415926; }`