Rather math question... menu

Shout-Out

User Tag List

Results 1 to 15 of 15
  1. #1
    nerexis's Avatar Member CoreCoins Purchaser
    Reputation
    2
    Join Date
    Mar 2008
    Posts
    56
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rather math question...

    I need to obtain angle between 2 vectors (waypoints...) but i dont know if im doing that corectly because it returns strange angle like 0,00015 or something like that.

    So is it correct way of calculating angle?

    Example waypoints:
    1. 373.735474 -5148.517578 -1.944246
    2. 366.841614 -5157.729980 3.432772

    Function:
    Code:
        float AngleTo(const Vector3df& q)
        {
            Vector3df v1(this->X,this->Y,this->Z);
            Vector3df v2(q);
    
            float v1_magnitude = sqrt(v1.X*v1.X + v1.Y*v1.Y + v1.Z*v1.Z);
            float v2_magnitude = sqrt(v2.X*v2.X + v2.Y*v2.Y + v2.Z*v2.Z);
    
            float d = v1.DotProduct(v2);
    
            float t = acos(d/v1_magnitude*v2_magnitude); 
    
            return t*PI/180;
        }
    Last edited by nerexis; 01-09-2011 at 06:15 AM.

    Rather math question...
  2. #2
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uhhh, Math.Atan2(float a, float b)? It's a lot easier than doing it THAT way.

    EDIT:
    You're using C++, so just include math and use atan2(...).

  3. #3
    namreeb's Avatar Legendary

    Reputation
    668
    Join Date
    Sep 2008
    Posts
    1,029
    Thanks G/R
    8/222
    Trade Feedback
    0 (0%)
    Mentioned
    9 Post(s)
    Tagged
    0 Thread(s)
    Just to add to lanman92's response, you want to do atan2(y2-y1, x2-x1) ... or maybe its the reverse of that: atan2(x2 - x1, y2 - y1).

  4. #4
    nerexis's Avatar Member CoreCoins Purchaser
    Reputation
    2
    Join Date
    Mar 2008
    Posts
    56
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Huh, isnt it for facing angle? I want angle on Z axis, I mean to detect if way between two vectors is walkable so angle between them should be < 50 degress.

    Look at screenshot:

  5. #5
    Azzie2k8's Avatar Member
    Reputation
    11
    Join Date
    Apr 2009
    Posts
    190
    Thanks G/R
    0/0
    Trade Feedback
    1 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know the code I am about to post is just ugly and there are faster ways to do this but maybe this explains the math a bit better

    Code:
    	float disty = Target->Position.fX - MyPosition.fX;
    	float distx = Target->Position.fY - MyPosition.fY;
    
    	if (distx > 0 && disty > 0)
    	{
    		face = abs(atan( distx / disty ));
    	}
    	else if (distx > 0 && disty < 0)
    	{
    		face = (0.5 * 3.14) + abs(atan( disty / distx ));
    	}
    	else if (distx < 0 && disty < 0)
    	{
    		face = (1 * 3.14) + abs(atan( distx / disty ));
    	}
    	else if (distx < 0 && disty > 0)
    	{
    		face = (1.5 * 3.14) + abs(atan( disty / distx ));
    	}
    btw thats obviously in radians so you will need to convert it to degrees if you want to

    edit: note that the change of X and Y coordinates is wanted since my layout was different than wows layout.

    face is the direction your charecter needs to face if he wants to lookt at the object/point

    not sure if this answers your question but if you go ahead and change it to fit your approach it will do the job

    but you should consider using atan2 like they said

    In trigonometry, the two-argument function atan2 is a variation of the arctangent function. For any real arguments x and y not both equal to zero, atan2(y, x) is the angle in radians between the positive x-axis of a plane and the point given by the coordinates (x, y) on it. The angle is positive for counter-clockwise angles (upper half-plane, y > 0), and negative for clockwise angles (lower half-plane, y < 0).
    http://en.wikipedia.org/wiki/Atan2
    Last edited by Azzie2k8; 01-09-2011 at 09:13 AM.

  6. #6
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just use atan2(z2 - z1, 0). I think that should work?

  7. #7
    nerexis's Avatar Member CoreCoins Purchaser
    Reputation
    2
    Join Date
    Mar 2008
    Posts
    56
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why? Isn't angle dependent on X,Y too?

  8. #8
    Azzie2k8's Avatar Member
    Reputation
    11
    Join Date
    Apr 2009
    Posts
    190
    Thanks G/R
    0/0
    Trade Feedback
    1 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by nerexis View Post
    Why? Isn't angle dependent on X,Y too?
    mh if you you rotate the unit circle it will actually match z coordinates...I think it will also work with atan2 you will only need to figure out what is what

    I think it will work

    Just try atan2(10,0) giving you about 0,5*pi meaning you will need to face straight up. so you could simply make it work with atan2(DstanceToYourToonOnZ,DistanceToYourToonOnX)

    edit: i didnt give it much thought so I might be wrong in the parameters but I am positive that it will work with atan2
    Last edited by Azzie2k8; 01-09-2011 at 02:00 PM.

  9. #9
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, gotta stop posting so early.... It's acos(sqrt((v2.x * v1.x)^2 + (v2.y * v1.y)) / |v2 - v1|). |v| is magnitude of vector v. That's right, I believe.

  10. #10
    nerexis's Avatar Member CoreCoins Purchaser
    Reputation
    2
    Join Date
    Mar 2008
    Posts
    56
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like its wrong.

    I made function based on your suggestion, tried to calculate angle between vectors i posted in 1 first post and it doesn't work.

    Returns -1.#IND00 :P

    Function:
    Code:
        float AngleTo(const Vector3df& q)
        {
            Vector3df v1(this->X,this->Y,this->Z);
            Vector3df v2(q);
    
            return acos(sqrt( (v2.X * v1.X) + (v2.Y * v1.Y)) / v2.Magnitude() - v1.Magnitude() )*180/PI;
        }


    Why '^2'?..

  11. #11
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow. I'm having a bad math day... It's v2.x - v1.x and v2.y - v1.y. You square those results and add them, then sqrt that. It's just the distance formula.

  12. #12
    Azzie2k8's Avatar Member
    Reputation
    11
    Join Date
    Apr 2009
    Posts
    190
    Thanks G/R
    0/0
    Trade Feedback
    1 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by nerexis View Post
    Looks like its wrong.

    I made function based on your suggestion, tried to calculate angle between vectors i posted in 1 first post and it doesn't work.

    Returns -1.#IND00 :P

    Function:
    Code:
        float AngleTo(const Vector3df& q)
        {
            Vector3df v1(this->X,this->Y,this->Z);
            Vector3df v2(q);
    
            return acos(sqrt( (v2.X * v1.X) + (v2.Y * v1.Y)) / v2.Magnitude() - v1.Magnitude() )*180/PI;
        }


    Why '^2'?..
    I think your first appraoch is right.

    The angle between 2 vectors is defined by
    Code:
    cos(a) = abs(vec1 * vec2) / ( abs(vec1) * abs(vec2) )
    which means
    Code:
    cos(a) = abs(vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z + ) / ( sqrt(vec1.x^2 + vec1.y^2 + vec1.z^2) * sqrt(vec2.x^2 + vec2.y^2 + vec2.z^2) )
    edit

    This is what a code could look like

    Code:
    acos( abs(vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z + ) / ( sqrt( pow(vec1.x,2) + pow(vec1.y,2) + pow(vec1.z,2) ) * sqrt( pow(vec2.x,2) + pow(vec2.y,2) + pow(vec2.z,2) ) ) )
    Last edited by Azzie2k8; 01-09-2011 at 04:10 PM.

  13. #13
    !@^^@!'s Avatar Active Member
    Reputation
    23
    Join Date
    Feb 2007
    Posts
    155
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    here's one way that works to calc the angle between 2 3D points/ 3D vectors depending on how you look at the problem:


  14. #14
    nerexis's Avatar Member CoreCoins Purchaser
    Reputation
    2
    Join Date
    Mar 2008
    Posts
    56
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Omfg O_O WORKS!

    Your's formula pwned world and made one of bots better!

    Thank you x1000

  15. #15
    alynel's Avatar Member
    Reputation
    4
    Join Date
    Jun 2010
    Posts
    8
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The maths in your first post was right, you've gotta put brackets around (v1_magnitude*v2_magnitude) though

Similar Threads

  1. [Question] Wow radar math?
    By gononono64 in forum WoW Memory Editing
    Replies: 14
    Last Post: 03-08-2011, 07:36 PM
  2. VB2005/Math logic question
    By katnegermis in forum Programming
    Replies: 9
    Last Post: 05-29-2009, 10:18 PM
  3. VB2005/Math logic question
    By katnegermis in forum WoW Memory Editing
    Replies: 9
    Last Post: 05-29-2009, 10:18 PM
  4. Question..
    By janzi9 in forum Community Chat
    Replies: 3
    Last Post: 04-02-2006, 10:20 AM
  5. A GALB question
    By bassman in forum World of Warcraft General
    Replies: 4
    Last Post: 03-28-2006, 09:49 AM
All times are GMT -5. The time now is 11:47 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search