Thread Tools Display Modes
03-09-16, 09:00 PM   #1
duzao7667
A Defias Bandit
Join Date: Mar 2016
Posts: 2
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

Last edited by duzao7667 : 03-09-16 at 09:06 PM. Reason: fmod added
  Reply With Quote
03-09-16, 10:24 PM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
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.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
03-10-16, 12:22 AM   #3
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
GetPlayerMapPosition returns a number between 0 and 1, so subtracting 60 and 42 from those values should be drastically throwing off your calculations.
  Reply With Quote
03-10-16, 09:31 AM   #4
duzao7667
A Defias Bandit
Join Date: Mar 2016
Posts: 2
Smile

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)?

Last edited by duzao7667 : 03-10-16 at 09:59 AM.
  Reply With Quote
03-10-16, 02:40 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
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
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 03-10-16 at 02:47 PM.
  Reply With Quote
03-12-16, 05:27 AM   #6
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Originally Posted by SDPhantom View Post
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.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Calculate how much to turn with x,y and facing radians

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off