# Thread: How do I figure out what Z-Angle (pitch) I need to be at in order to face a target?

1. ## How do I figure out what Z-Angle (pitch) I need to be at in order to face a target?

This isn't a memory reading/editing question although because it is about my Bot, and apparently half of you are mathematical geniuses, I thought I would post it here.

I am creating a bot which will fly around and follow waypoints, looking for nodes to mine. I already have most of the things I need in regards to in-game values (I am doing this entirely out of process).

My knowledge of trigonometry is lacking at best. Using Shynd's wordpress blog as a basis I was able to develop a function for rotating on the XY to face another XY point. This is my function to do so (C#):

Code:
```public void TurnTo(PlayerObject LocalPlayer, double LocationX, double LocationY, int Accuracy, int Speed)
{
string turnDirection = "";
double l = 0, r = 0;
// the angle to which we need to turn in order to face our location
double f = Math.Atan2(LocationY - LocalPlayer.YPos, LocationX - LocalPlayer.XPos);
if (f < 0)
f = (Math.PI * 2) + f;
// if our angle in degrees is greater than the direction we need to face
if (currentRotation > f)
{
// we dont have to turn past north if we are turning right
r = DifferenceBetween(currentRotation, f);
// we do have to turn past north if we are turning left
l = 360 - currentRotation + f;
}
// if our angle in degrees is less than the direction we need to face
if (currentRotation < f)
{
// we do have to turn past north if we are turning right
r = currentRotation + (360 - f);
// we dont have to turn past north if we are turning left
l = DifferenceBetween(currentRotation, f);
}
if (l < r)
turnDirection = "left";
if (r < l)
turnDirection = "right";
return;
// move our mouse to center of screen
MC.MoveMouseToCenter();
// press right mouse button down
MC.RightMouseDown();
double DegreesToGo;
int oldX = CenterScreenX;
if (turnDirection == "left")
{
// 'l' is how many degrees we have to turn
{
MC.MoveMouse(oldX - Math.Sqrt(DegreesToGo / Speed), CenterScreenY);
oldX = oldX - (int)Math.Round(Math.Sqrt(DegreesToGo / Speed));
}
}
if (turnDirection == "right")
{
// 'r' is how many degrees we have to turn
{
MC.MoveMouse(oldX + Math.Sqrt(DegreesToGo / Speed), CenterScreenY);
oldX = oldX + (int)Math.Round(Math.Sqrt(DegreesToGo / Speed));
}
}
MC.RightMouseUp();
}```
Due to the nature of my bot (operating on an XYZ plane as opposed to a XY plane) I need to adjust my pitch so I am at the right pitch to face my target (in this case a waypoint), as well as adjusting my rotation (which I will do using the function above). I am able to read my players current pitch, and the values of this Z pitch range from -1.553343 (the lowest pitch, pointing towards the ground the most) to 1.553343 (the highest pitch, point towards the sky the most).

What I am asking is, using my current XYZ and my targets XYZ (in this case my target is my next waypoint), how can I determine what my pitch needs to be in order to be facing this target on the Z-plane? Edit: Forgot to say, even if I could work out an angle between 180 and 0 (180 for looking straight down, and 0 for looking straight up, or vice versa) that would help greatly.

2. It's 5am here and I haven't slept yet, but..

float pitch = (float)(Math.PI / 2 - Math.Atan((z2 - z1) / Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2))));
pitch = (float) (Math.PI / 2 - pitch);

I don't have an active WoW acc atm so I can't really test it. :/

Tests:

// you = 1, target = 2
x1 = 0;
y1 = 0;
z1 = 0;

x2 = 0;
y2 = 0;
z2 = -1;

// Means that the target is right beneath you -> pitch = -pi/2

x2 = 1;
y2 = 0;
z2 = 0;

// Means that the target is right in front of you -> pitch = 0

x2 = 0;
y2 = 0;
z2 = 1;

// Target is above you -> pitch = +pi/2

Spherical coordinate system - Wikipedia, the free encyclopedia

there are the transformations from P(x,y,z) -> P(_r_, θ, φ)

3. There is no such thing as "xyz plane". A plane has just TWO dimensions.
Even if your bot operates in a 3d environment (flies around, as opposed to one moving on the ground) it still operates in blizz cardinal system and NOT your own. In plain english, as long as you use x,y,z, pitch and facing retrieved from wow you ARE in the same 3d blizz provides you. Finding out the pitch towards a given point is in no way different than finding the facing. Same thing, projected on another plane. "facing" means projection on XY plane (and thus using the Z as reference for angles) "pitch" is using the XZ plane using Y as reference (assuming X is facing north and Y west).

4. I'm pretty sure that the forumla you posted is correct. I tried it and it seemed to work perfectly, I'm going to input it into my bot and test the results. +Rep for sure.

Would you (or someone else) just mind running through what you did there? I don't like to copy-paste. I did read that article although it is way over my head.

5. Originally Posted by shomoiog
There is no such thing as "xyz plane". A plane has just TWO dimensions.
Even if your bot operates in a 3d environment (flies around, as opposed to one moving on the ground) it still operates in blizz cardinal system and NOT your own. In plain english, as long as you use x,y,z, pitch and facing retrieved from wow you ARE in the same 3d blizz provides you. Finding out the pitch towards a given point is in no way different than finding the facing. Same thing, projected on another plane. "facing" means projection on XY plane (and thus using the Z as reference for angles) "pitch" is using the XZ plane using Y as reference (assuming X is facing north and Y west).

Sorry about the XYZ plane thing, I had no idea that a 'plane' has only XY values. You shouldn't really be aggressive towards me - I did not ask for source code, although it was provided to me (thankyou!). I just had no idea how to go about calculating this.

6. OK let me put it this way:

See the resemblance?

7. Originally Posted by shomoiog
OK let me put it this way:

See the resemblance?
I like you. Can we be friends?

8. Cabri Java Applet

It's from a german site but the important things about it don't require any spoken language.

As you already have the formula for the facing, this 3D problem becomes an easier 2D problem.

(that y should actually be a z meh..)

Now that it's 2 dimensional we can rotate our imaginary camera until we get this 'standard' 2d cartesian view. (jesus my math-english sucks even more than my nonmath-english)

there are several ways on how to get the specific angle we're looking for but a general attempt is to first get the length of all the sides. We know the coordinates of the point P (the player): P(xp,yp,zp), and we know the coordinates of our target T: T(xt,yt,zt).

Imagine you're standing behind a window in a tall building. You are P, and T might be a car parking outside. Then the base B would be the entrence of the building you're in (right beneath you).

As we don't care about the facing anymore, you, the car and the entrence form a triangle.

This leads to the coordinates of the base B: B(xp,yp,zt). It has the same x/y coordinates as you and shares the z coordinate with your target.

|BT| is the length of the 'line/side' from the point B to the point T.

|BT| = |(xt,yt,zt) - (xp,yp,zt)| = Sqrt([xt-xp]^2 + [yt-yp]^2 + [0]^2)

|BP| = |(xp,yp,zp) - (xp,yp,zt)| = Sqrt([0]^2 + [0]^2 + [zp-zt]^2)

90° -> pyth: |r| = |PT| = Sqrt(|BT|^2+|BP|^2) = Sqrt([xt-xp]^2 + [yt-yp]^2 +[zp-zt]^2)

Now the angle alpha = |_BPT:
cos(alpha) = |BP| / |PT| = Sqrt((zp-zt)^2) / Sqrt([xt-xp]^2 + [yt-yp]^2 +[zp-zt]^2)

sqrt(x^2) = x

-> alpha = arccos((zt-zp) / sqrt((xt-xp)^2 + (yt-yp)^2 + (zt-zp)^2)

and thats

taken from wikipedia ( Spherical coordinate system - Wikipedia, the free encyclopedia )

or much easier if you know the formula for a sphere:

(x-xm)^2 + (y-ym)^2 + (z-zm)^2 = r^2, solving for r gives again that ugly squareroot.

Finally, WoWs a bit 'special' -> pitch = pi/2 - pitch so that looking down results in -pi/2 and thats very close to your findings of the max. pitch values.

This post might very well contain lots of mistakes, I'm tired and have had a very long pause . The applet I linked is really helpful though.

9. Originally Posted by SKU
Cabri Java Applet

It's from a german site but the important things about it don't require any spoken language.

As you already have the formula for the facing, this 3D problem becomes an easier 2D problem.

(that y should actually be a z meh..)

Now that it's 2 dimensional we can rotate our imaginary camera until we get this 'standard' 2d cartesian view. (jesus my math-english sucks even more than my nonmath-english)

there are several ways on how to get the specific angle we're looking for but a general attempt is to first get the length of all the sides. We know the coordinates of the point P (the player): P(xp,yp,zp), and we know the coordinates of our target T: T(xt,yt,zt).

Imagine you're standing behind a window in a tall building. You are P, and T might be a car parking outside. Then the base B would be the entrence of the building you're in (right beneath you).

As we don't care about the facing anymore, you, the car and the entrence form a triangle.

This leads to the coordinates of the base B: B(xp,yp,zt). It has the same x/y coordinates as you and shares the z coordinate with your target.

|BT| is the length of the 'line/side' from the point B to the point T.

|BT| = |(xt,yt,zt) - (xp,yp,zt)| = Sqrt([xt-xp]^2 + [yt-yp]^2 + [0]^2)

|BP| = |(xp,yp,zp) - (xp,yp,zt)| = Sqrt([0]^2 + [0]^2 + [zp-zt]^2)

90° -> pyth: |r| = |PT| = Sqrt(|BT|^2+|BP|^2) = Sqrt([xt-xp]^2 + [yt-yp]^2 +[zp-zt]^2)

Now the angle alpha = |_BPT:
cos(alpha) = |BP| / |PT| = Sqrt((zp-zt)^2) / Sqrt([xt-xp]^2 + [yt-yp]^2 +[zp-zt]^2)

sqrt(x^2) = x

-> alpha = arccos((zt-zp) / sqrt((xt-xp)^2 + (yt-yp)^2 + (zt-zp)^2)

and thats

taken from wikipedia ( Spherical coordinate system - Wikipedia, the free encyclopedia )

or much easier if you know the formula for a sphere:

(x-xm)^2 + (y-ym)^2 + (z-zm)^2 = r^2, solving for r gives again that ugly squareroot.

Finally, WoWs a bit 'special' -> pitch = pi/2 - pitch so that looking down results in -pi/2 and thats very close to your findings of the max. pitch values.

This post might very well contain lots of mistakes, I'm tired and have had a very long pause . The applet I linked is really helpful though.
Thanks, that is a massive help in understanding what I am doing. I'll go back over what you said and read it carefully. But I am VERY grateful you took the time to explain it to me.

10. And don't copy/paste it. The origin (your position) won't be (0,0,0) so you'll have to alter the formulae

http://www.mmowned.com/forums/wow-me...ng-z-axis.html

11. Do not use atan2, it has some points that are not true to rotation; If you have to, use atan.

12. Originally Posted by suicidity
Do not use atan2, it has some points that are not true to rotation; If you have to, use atan.
Actually atan2 works fine. ISXWoW has been using it since.. ever, and its never had problems with rotation.

I personally also use it and have never had a problem soooooo....

13. Although this has been pretty well addressed already, I thought I should post as the problem is actually very simple conceptually.

First, forget about the z axis, and work out your horizontal distance from the object using simple pythagoras.

eg. You are are (100,100), your target is at (200,200).

Distance = √( (200-100)^2 + (200-100)^2)
(Horizontal) Distance = √(200)

Now, suppose your z value is 100, and your target's is 200. Vertical distance is (200-100) = 100. Therefore you have a simple right angled triangle.

Using inverse tan you can find the required angle.

So breaking up a 3d problem into 2 2d ones makes it much simpler.
Hopefully this post helped.

14. thats my code (rewritten a little bit):

Code:
```double dx = thisUnit.coordX - otherUnit.coordX;
double dy = thisUnit.coordY - otherUnit.coordY;
double dz = thisUnit.coordZ - otherUnit.coordZ;
double targetDistance = Math.Sqrt(dx*dx + dy*dy + dz*dz);

double pitchAngleIs = thisUnit.pitch;
double pitchAngleShould = - Math.Sign(dz) * (Math.Acos(Math.Sqrt(dx*dx + dy*dy) / targetDistance));
double pitchAngleDiff = pitchAngleIs - pitchAngleShould ;

double facingAngleIs = refUnit.facing;
double facingAngleShould = (Math.Atan2(dx, dy) + Math.PI) % (2 * Math.PI);
double facingAngleDiff = facingAngleIs - facingAngleShould ;```
maybe it helps.
should be easy to implement.

15. Oh noes another node Farmer!! :O :O :O well its very easy how i did it.

Btw i never did pitch b/c it ****ed up alot while flying b/c i never had a full map of all objcects / terrain to see if i am gonna run into a wall or big ass tree.

Page 1 of 2 12 Last
All times are GMT -5. The time now is 06:51 AM. Powered by vBulletin® Version 4.2.2