• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Custom Script Issues

Status
Not open for further replies.

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,180
call SetUnitX(GetOrderedUnit())[udg_IndexLoop], GetLocationX(udg_TempLoc))

The problem is with the unit argument.

GetOrderedUnit())[udg_IndexLoop]

This does not make any sense.
[udg_IndexLoop] is used on arrays to provide an index (required when referencing them) yet you are calling a function to return a value. Arrays can not be returned in JASS as far as I know so this whole statement is impossible (will throw a syntax error).

You eithor want...
If unit is in array... YourArrayName[udg_IndexLoop]
If unit is returned... GetOrderedUnit()

To become...
call SetUnitX(YourArrayName[udg_IndexLoop], GetLocationX(udg_TempLoc))
or
call SetUnitX(GetOrderedUnit(), GetLocationX(udg_TempLoc))
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,180
With a native SetUnitFacing takes unit whichUnit,real facingAngle returns nothing call.
The angle can be computed using triggonometry. Be aware that the trig functions in WC3 opperate in radians and not degrees but the unit facing functions (both accessors and mutators) are in degrees so you will need an appropiate conversion.
 
Level 8
Joined
Feb 17, 2007
Messages
368
Well I want it to face a point not an angle, so I would use something like this?:

call SetUnitFacingToFaceLocTimed(GetOrderedUnit(), (udg_TempLoc2)), 0

I want it to face the point over 0 seconds. I know the codes wrong btw, just don't know how to fix it =p
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,180
You use mathematics to calculate the angle needed. from the poisition of the unit and the position of the target location. Just like you can convert from polar form to cordinates in an X/Y plane using sin and cos, you can convert from cordinates in a X/Y plane to an angle.

WC3 even provides you with the
native Atan2 takes real y,real x returns real
function which will even return an angle between -pi and pi (normal arctan just goes between -pi/2 and pi).

This is the pesudo code of what you are after.
SetUnitFacing(theunit,Atan2(targety-unity,targetx-unitx)*180/pi)

What you do is create a X/Y vector of the target point relative to the units position and pass that to the Atan2 function as the appropiate y and x arguments. You then need to perform unit conversion to get from radians to degrees (no idea why WC3 is not consistant...).

This is how the GUI real parameter for angle between 2 points does it.

function AngleBetweenPoints takes location locA,location locB returns real
return bj_RADTODEG * Atan2(GetLocationY(locB) - GetLocationY(locA), GetLocationX(locB) - GetLocationX(locA))
endfunction
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,180
The principles behind the formula I gave should have been taught in a course covering vector mathematics although all you really need to understand it is middle year highschool mathematics.

A Location object in WC3 represents a position vector from the origan of the map. The origan of the map is the lower left corner + a displacement vector defined in the .w3e mesh file (usually this makes it the centre of the map but other behaviours are possible).

A Location can for the most part be represented as an X/Y pair of reals.

Both the unit, and the target location have an X/Y pair of reals representing a displacement vector from the origan of the map.
What you are after is creating a vector from the unit to the target location as that would have the angle you are after. This means making the displacement vector of the unit the origan and making the location realitive to it.

As the origan is 0, subtracting both vectors by the displacement vector of the unit will make the unit located at the origan (a - a = 0) while the location will be adjusted acordingly to match the shift of reference so it will have an angle that matches the unit looking straight at the location.

Vector addition is just adding each component together so likewise subtraction is just subtracting each component.

The end result is a vector of
x = xofthetargetlocation - xoftheunit
y = yofthetargetlocation - yoftheunit

This vector can be seen as a right-angled triangle. The x is the adjecent side while the y is the opposite side. Tan takes an angle and returns a ratio of y/x meaning that using the inverse of tan (arctan) you can get an angle from the ratio. This however usually has a restricted domain as there are 2 angles for the same value with the Tan function but that is avoided by using the Atan2 function which takes both X and Y components separatly and calculates an angle of full domain.

The final hurdle is game related. All Triggometric function in WC3 are in radians yet unit facing is measured in degrees. This means a conversion is needed from radians to degrees.

Both radians and degrees are a linear scale which raps between a certain range so each value on the scale has an infinite number of equal values.

0 radians = 0 degrees = 2pi radians = 360 degrees
pi radians = 180 degrees

Thus a conversion can be worked out.
If you have something in radians, you can divide by pi to get the number of half circles which can be convereted to degrees by multiplying by 180.
If you have something in degrees, you can divide by 180 to get the number of half circles which can be converted to radians by multiplying by pi.

The game accepts any positive or negative angle so 4pi radians will still be equiviently 0 radians same with -2pi or -4pi. This is because it is a circle, if you go around the circle 9 times you will still end up in the same place you started at.
 
Status
Not open for further replies.
Top