WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Calculate how much to turn with x,y and facing radians (https://www.wowinterface.com/forums/showthread.php?t=53216)

duzao7667 03-09-16 09:00 PM

Calculate how much to turn with x,y and facing radians
 
Hello everyone o/

I'm trying to get the amount of degrees I need to turn to face object B (x=60,y=42 on map) but theres always mismatches, and I'm not talking about degree normalization (mod 360 or if degree>0 degree - 360) but the calc by itself. When i'm facin the oposite side of the object B i wont get an 180 degree (getting 170) and when facing the object (perfect line of sigh, completely pointed and on sight) I got 350 degrees on howMuchTurn oO
There's something wrong I failed to see or any lesson I forgot to learn?

The code snippet is the folowing:

Code:

SetMapToCurrentZone()
local actualX, actualY = GetPlayerMapPosition("player")
howMuchTurn=(radToDeg(GetPlayerFacing()) - radToDeg(math.atan2(actualX-60,actualY-42)))

...

function radToDeg(rad)
        return rad * (180 / math.pi)
end

--used this to normalize the coords
--howMuchTurn=math.fmod(howMuchTurn,360) --... but yet, unexpected 10 and -10 angles

PS: I used TomTom to verify the perfect 180 and 359/0 degrees to object B.
HALP! xD

SDPhantom 03-09-16 10:24 PM

If you're getting ±10°, it may be the principle in which your calculations are based on. The 0-100 range is a scale on width or height when in fact, the zone area hardly ever is perfectly square. You can either convert these to real system coordinates or multiply X and Y by the width and height of WorldMapButton respectively.

semlar 03-10-16 12:22 AM

GetPlayerMapPosition returns a number between 0 and 1, so subtracting 60 and 42 from those values should be drastically throwing off your calculations.

duzao7667 03-10-16 09:31 AM

Any clues how tomtom does the calculus so?

@semlar GetPlayerMapPosition? I forgot to put on the snippet, but i'm multiplying X and Y coords by 100.
@SDPhanton So it's an scale problem? Based on semlar tip, do you see anything I can do to transform my X and Y in "real world" or plausible/usable arctangent parameters without any external lib or plugin (found only WorldMapButton as an addon)?

SDPhantom 03-10-16 02:40 PM

Turning this into a function of its own, this should do the trick. WorldMapButton is a child of WorldMapFrame that the map and all objects are placed on. TomTom used to use the AstroLab library for real coordinates of the map edges, but they are now provided through GetCurrentMapZone(). There's some additional complexity to using these if you want to include distances, but as you aren't including that in your design, this is a more simple approach.

Lua Code:
  1. function GetDirection(x,y)
  2. --  Only reset map if not shown
  3.     if not WorldMapFrame:IsShown() then SetMapToCurrentZone(); end
  4.  
  5.     local px,py=GetPlayerMapPosition("player");
  6.     if px==0 and py==0 then return nil; end--   Return nil if not on map
  7.     local w,h=WorldMapButton:GetSize();
  8.  
  9. --  Converts radians to degrees and normalizes range to -180 thru 180 (Player X and Y are multiplied by 100)
  10.     return ((GetPlayerFacing()-math.atan2((px*100-x)*w,(py*100-y)*h))*180/math.pi+180)%360-180;
  11. end

myrroddin 03-12-16 05:27 AM

Quote:

Originally Posted by SDPhantom (Post 313541)
TomTom used to use the AstroLabe library for real coordinates of the map edges

TomTom has been updated to use HereBeDragons-1.0, which in turn uses modern API calls provided by Blizzard, along with a much better math system, among lots of other improvements.


All times are GMT -6. The time now is 07:36 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI