• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

Using X,Y offsets instead of polar projections

Status
Not open for further replies.
Level 8
Joined
Mar 3, 2009
Messages
327
I need to use X,Y offsets instead of polar projections, but i need to still have the same effect as polar projections, like moving a unit by X towards Y degrees. Im pretty sure you need to use sin and cos to calulate the right offset, but I dunno how. Could anyone give me an explanation of sin and cos please? thanks :D
 
Level 11
Joined
Sep 30, 2009
Messages
697
These are all working functions with coordinates instead of locations(added a few more if you need them)

JASS:
function PolarProjectionX takes real x, real dist, real angle returns real
    return x + dist * Cos(angle * bj_DEGTORAD)
endfunction

function PolarProjectionY takes real y, real dist, real angle returns real
    return y + dist * Sin(angle * bj_DEGTORAD)
endfunction

function DistanceBetweenCoordinates takes real x, real y, real x2, real y2 returns real
    local real dx = x - x2
    local real dy = y - y2
    return SquareRoot(dx * dx + dy * dy)
endfunction

function AngleBetweenCoordinates takes real x, real y, real x2, real y2 returns real
    return bj_RADTODEG * Atan2(y2 - y, x2 - x)
endfunction
 
Level 8
Joined
Mar 3, 2009
Messages
327
Thanks. Whats the bj_DEGTORAD? Wait nevermind... degrees to radians isnt it. Is it like a default number to multiply the degrees by to convert them to radians?

Im guessing that the Cos and Sin need to.... Cos and Sin the radians rather than the degrees?
 
Level 28
Joined
Jan 26, 2007
Messages
4,789
Thanks. Whats the bj_DEGTORAD? Wait nevermind... degrees to radians isnt it. Is it like a default number to multiply the degrees by to convert them to radians?

Im guessing that the Cos and Sin need to.... Cos and Sin the radians rather than the degrees?

If you want to know, DEGTORAD is 0.017453292 ( = pi/180) (I usually use '0.017453292' instead of 'bj_DEGTORAD').
That's because the GUI-form of Cos and Sin use degrees, while the native JASS form uses radians.

Anyways, this is the form I usually use:
JASS:
function PolarProjections takes unit u returns nothing
    local real X = GetUnitX(u)
    local real Y = GetUnitY(u)
    local real F = Angle
    local real S = Distance

    call SetUnitX(u, X + S * Cos(F * 0.017453292))
    call SetUnitY(u, Y + S * Sin(F * 0.017453292))
endfunction

I hope this speaks for itself: it moves a unit to a location that is S distance from the unit towards F degrees.
You can also use it in GUI, if that's what you prefer ^^

I know Axarion already gave the answer, but it's just to show that you don't need multiple functions and can do it with this one as well.
 
Level 28
Joined
Jan 26, 2007
Messages
4,789
@ ap0calypse:
shouldn't your function take the reals f and s, too? Just wondering^^ Also this function is only useful if you want to move a unit and not something else but its anyway another good solution
It's just an example :p
The angle/distance may be preset, but they can also be taken, yeah.
I just don't like working with many functions that return something, so I thought this could be useful as well =D

And I don't know what else you should move O_O
You can move rects by using the "SetRect" function, if that's what you mean...

GoGoTauren said:
like moving a unit by X towards Y degrees.

Well, your method is basically the same as mine, I just 'combined' the functions because I don't like seperate ones ^^
 
Level 8
Joined
Mar 3, 2009
Messages
327
Ok, what am i doing wrong here? I set the velocity 30 and the unit doesnt move in game. I set the unit variable, the velocity, the X and Y positions, and the XY facing.

  • Custom script: call SetUnitX(udg_Unit[GetForLoopIndexA()], udg_Xposition[GetForLoopIndexA()] + (udg_Xvelocity[GetForLoopIndexA()]) *Cos( udg_XYfacing[GetForLoopIndexA()] * 0.017453292))
  • Custom script: call SetUnitY(udg_Unit[GetForLoopIndexA()], udg_Yposition[GetForLoopIndexA()] + (udg_Yvelocity[GetForLoopIndexA()]) *Sin( udg_XYfacing[GetForLoopIndexA()] * 0.017453292))
 
Level 28
Joined
Jan 26, 2007
Messages
4,789
It oculd be a typo in the variables (I can't see any typo's in the code itself).

Perhaps you shouldn't use too much variables? :p
X and Y-velocity should be the same, otherwise it will bug (the difference in X and Y the unit needs to move is calculated with the sin/cos-functions).
The XYfacing and X/Yposition can be calculated in the code itself

And create an integer variable for the loops, don't use "Integer A/B" (I always use the variable "LoopInt" as a temporary integer for loops).

This should work (just tested it):

call SetUnitX(udg_Unit[udg_LoopInt], GetUnitX(udg_Unit[udg_LoopInt]) + (udg_Velocity[udg_LoopInt] * Cos(GetUnitFacing(udg_Unit[udg_LoopInt]) * 0.017453292)))
call SetUnitY(udg_Unit[udg_LoopInt], GetUnitY(udg_Unit[udg_LoopInt]) + (udg_Velocity[udg_LoopInt] * Sin(GetUnitFacing(udg_Unit[udg_LoopInt]) * 0.017453292)))

It only uses these variables:
udg_Unit[X]
udg_Velocity[X]
udg_LoopInt

If it doesn't work, use debug-messages to check where the problem lies (before moving, show the unit name, the coords of the unit, the velocity and the angle - then show the new coords).
If one of these values seem to be incorrect, then you know where the problem lies - if you can't see anything at first sight, but the unit still isn't moving, then you can post it here and let others search with you :D
 
Status
Not open for further replies.
Top