View Single Post
09-14-14, 01:13 AM   #20
Gello
A Molten Giant
AddOn Author - Click to view addons
Join Date: Jan 2005
Posts: 521
Originally Posted by p3lim View Post
Code:

UnitDistanceSquared
UnitPosition
For anyone interested in using these functions:

These work for other players in the group. No pets or friendly targets outside the group.

When a given unit has no valid returns:
  • UnitDistanceSquared returns 0
  • UnitPosition returns nil

These functions appear to deal with in-game yards on the global coordinate system previously limited to the map transformation functions.

UnitDistanceSquared is, as it says, the distance from you to the unit, squared. If your friendly target is 50 yards away, UnitDistanceSquared("target") returns 2500. I've not done any debugprofilestop testing on it to tell if it's faster than (x2-x1)^2 + (y2-y1)^2, but it's very likely.

The first two returns of UnitPosition freaks me out. Moving north will increase the first return, which is ordinarily the x-axis. Moving west will increase the second return, which is ordinarily the y axis. The coordinate system is flipped somehow. But with them ordered as returned, x,y=UnitPosition(unit), the math seems to work out. So if you're moving from GetPlayerMapPosition to this, you might want to hold off on reversing returns.

It's always nice when Blizzard gives us API stuff they don't even use. These are especially cool since we can now deal with player coordinates without having to deal with maps at all.