|
lengthdir_x / y (len,dir) |
|
len = Is the length of the imaginary line you are drawing from point (0,0) outwards. dir = The direction this imaginary line goes.
lengthdir_x /_y are so that people who don't know trigonometry can do things that only trig allows. A common use for them is the "make my bullet appear at the end of the barrel" scenario.
The arguments for lengthdir_x are length and direction. so you would supply the length of the barrel as length, and the direction the gun is pointing as direction. the result would be the amount you have to offset your bullets x coordinate to create it in the right place. You would do the same thing with lengthdir_y.
So lets assume the barrel is 32 pixels long, and is pointing in the direction gun_direction. To create the bullet in the right place you would put the following code in the gun's Shoot event.
Example #1:
If you wanted to draw a line going from your player, and pointing in the players direction, and you wanted the line to be 100 pixels long, you would use the following code:
Example #2:
MATHS For those interested, here's the maths behind this function. Ok so lets take the function lengthdir_x(len, dir). the code for that would be as follows:
return (cos(argument1*pi/180)*argument0)
where argument0 is the length, and argument 1 is the angle
so lets break that down a bit.
return well that's simple enough, that is just saying that whatever comes next should be returned by the function.
so we are returning (cos(argument1*pi/180)*argument0)
cos is a little more complicated to explain. This is a function in its self, and it takes a single parameter. that parameter is an angle (argument1). but why do we say argument1*pi/180? because trigonometry works with things called "radians". we as humans are used to representing angles in degrees (360 degrees in 1 full turn). However for a lot of complex mathematical functions, angles must be represented in radians. there are 2*pi radians in one complete turn (a useful thing to remember). Therefore the translation from degrees to radians can be done with the equation (angle_in_degrees*pi/180). (this can also be done with the GM function degtorad(angle). think degrees to radians)
ok so now we have sorted out what parameter we are giving to the function cos, but we haven't found out what cos does yet. imagine we draw a line from point 0, 0 in the direction of angle, and that line happens to be exactly 1 long. the measurement unit is irrelevant here.
there we see our line. the function cos will return the horizontal coordinate of the endpoint of this line. so this will range from positive 1 (pointing straight right, angle = 0 or 360) to negative 1 (pointing straight, left angle = 180) with 0 being returned at angle = 90 or 270.
so that pretty much covers the main part of the function.
return (cos(argument1*pi/180)*argument0)
the length(argument0) is easy to explain. because the cos function will always return the horizontal coordinate of the endpoint assuming the line is of length 1, we simply multiply the result by the length of the line we were really wanting to know about. this is where the *argument0 (or *length) comes in.
lengthdir_y is essentially the same function, except instead of returning the x coordinate, it returns the y coordinate by using sin instead of cos. well.. actually that's not true. just to be nice, it returns the upside down y coordinate. so instead of
return (sin(argument1*pi/180)*argument0)
as you might expect, it is actually
return (-sin(argument1*pi/180)*argument0)
monkey dude - Revision #1 |