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

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    jbrauman's Avatar Member
    Reputation
    65
    Join Date
    Dec 2007
    Posts
    72
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 currentRotation = RadiansToDegrees(LocalPlayer.Rotation);
            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;
            f = RadiansToDegrees(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";
            if (DifferenceBetween(RadiansToDegrees(LocalPlayer.Rotation), f) < Accuracy)
                return;
            // move our mouse to center of screen
            MC.MoveMouseToCenter();
            Thread.Sleep(100);
            // press right mouse button down
            MC.RightMouseDown();
            Thread.Sleep(100);
            double DegreesToGo;
            int oldX = CenterScreenX;
            if (turnDirection == "left")
            {
                // 'l' is how many degrees we have to turn
                while (DifferenceBetween(RadiansToDegrees(LocalPlayer.Rotation), f) > Accuracy)
                {
                    DegreesToGo = DifferenceBetween(RadiansToDegrees(LocalPlayer.Rotation), f);
                    MC.MoveMouse(oldX - Math.Sqrt(DegreesToGo / Speed), CenterScreenY);
                    oldX = oldX - (int)Math.Round(Math.Sqrt(DegreesToGo / Speed));
                    Thread.Sleep(25);
                }
            }
            if (turnDirection == "right")
            {
                // 'r' is how many degrees we have to turn
                while (DifferenceBetween(RadiansToDegrees(LocalPlayer.Rotation), f) > Accuracy)
                {
                    DegreesToGo = DifferenceBetween(RadiansToDegrees(LocalPlayer.Rotation), f);
                    MC.MoveMouse(oldX + Math.Sqrt(DegreesToGo / Speed), CenterScreenY);
                    oldX = oldX + (int)Math.Round(Math.Sqrt(DegreesToGo / Speed));
                    Thread.Sleep(25);
                }
            }
            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.

    Thankyou for your time, I hope someone knows (Google doesnt!)
    Last edited by jbrauman; 01-29-2009 at 11:05 PM.

    How do I figure out what Z-Angle (pitch) I need to be at in order to face a target?
  2. #2
    SKU's Avatar Contributor
    Reputation
    306
    Join Date
    May 2007
    Posts
    565
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    Might be missing something obvious.. also, you may want to read this article:
    Spherical coordinate system - Wikipedia, the free encyclopedia

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

  3. #3
    shomoiog's Avatar Member
    Reputation
    20
    Join Date
    Dec 2008
    Posts
    11
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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).

    It's 5th grade math. Google DOES know, try "basic geometry" instead of "source code" as terms.

  4. #4
    jbrauman's Avatar Member
    Reputation
    65
    Join Date
    Dec 2007
    Posts
    72
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Last edited by jbrauman; 01-30-2009 at 12:43 AM.

  5. #5
    jbrauman's Avatar Member
    Reputation
    65
    Join Date
    Dec 2007
    Posts
    72
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by shomoiog View Post
    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).

    It's 5th grade math. Google DOES know, try "basic geometry" instead of "source code" as terms.
    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. #6
    shomoiog's Avatar Member
    Reputation
    20
    Join Date
    Dec 2008
    Posts
    11
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK let me put it this way:

    Stand up, spread your arms in front of you. That's facing.
    Lay down, spread your arms in front of you. That's pitch.

    See the resemblance?

  7. #7
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1356
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by shomoiog View Post
    OK let me put it this way:

    Stand up, spread your arms in front of you. That's facing.
    Lay down, spread your arms in front of you. That's pitch.

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

  8. #8
    SKU's Avatar Contributor
    Reputation
    306
    Join Date
    May 2007
    Posts
    565
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Last edited by SKU; 01-30-2009 at 01:57 AM.

  9. #9
    jbrauman's Avatar Member
    Reputation
    65
    Join Date
    Dec 2007
    Posts
    72
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by SKU View Post
    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. #10
    Robske's Avatar Contributor
    Reputation
    305
    Join Date
    May 2007
    Posts
    1,062
    Thanks G/R
    3/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    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
    "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - Martin Golding
    "I cried a little earlier when I had to poop" - Sku

  11. #11
    suicidity's Avatar Contributor
    Reputation
    207
    Join Date
    Oct 2006
    Posts
    1,439
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do not use atan2, it has some points that are not true to rotation; If you have to, use atan.


  12. #12
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1356
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by suicidity View Post
    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. #13
    Gamer's Avatar Active Member
    Reputation
    239
    Join Date
    Jan 2007
    Posts
    198
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #14
    g3gg0's Avatar Active Member
    Reputation
    32
    Join Date
    Mar 2008
    Posts
    86
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Last edited by g3gg0; 01-31-2009 at 08:40 PM.

  15. #15
    luciferc's Avatar Contributor
    Reputation
    90
    Join Date
    Jul 2008
    Posts
    373
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 LastLast

Similar Threads

  1. Need Helping Figuring Out What Clothes
    By Gummibär in forum Community Chat
    Replies: 10
    Last Post: 07-12-2009, 04:35 PM
  2. Is it possible to figure out what texture a item use?
    By Omahz in forum WoW ME Questions and Requests
    Replies: 0
    Last Post: 06-09-2009, 11:24 AM
  3. I can't figure out whats wrong with...
    By V1cinity in forum WoW ME Questions and Requests
    Replies: 0
    Last Post: 07-20-2007, 04:25 PM
  4. Figure out what it says (Hard)
    By Enfeebleness in forum Community Chat
    Replies: 13
    Last Post: 07-13-2007, 05:35 PM
  5. Figure Out What This Says, And Dont Win Anything!
    By XaVe in forum Community Chat
    Replies: 7
    Last Post: 07-12-2007, 04:35 PM
All times are GMT -5. The time now is 12:20 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search