GM: lengthdir x

From GMpedia.org Wiki

Jump to: navigation, search

lengthdir_x
 

Type: function
Arguments: 1: length
2: direction
Limitations: none
The correct title of this article is lengthdir_x(). It appears incorrectly here because of technical restrictions.

The lengthdir_x() function in Game Maker is a mathematical function that Returns the horizontal x-component of the vector determined by the indicated length and direction.

as stated by the Game Maker 6.1 help file.

The following image will make this clear:

Image:Lengthdir_x.png


As you can see, on the image are arrows starting at the origin and pointing to a certain point in the xy-plane. These arrows are called vectors. They have a certain length and form an angle with the positive x-axis. The distance in horizontal direction between those two points (as seen on the x-axis) is lengthdir_x, or mathematically: cos(angle) * length


You might encounter such a situation in Game Maker where you've calculated that length and direction with the functions point_distance() and point_direction(). The first point (x1,y1) would then be the origin and the second point (x2,y2) would be the arrow top.


To know the distance in horizontal direction (so the distance on the x-axis) between those two points, you can use lengthdir_x(l,x), where l is point_distance(x1,y1,x2,y2) and x is point_direction(x1,y1,x2,y2).


On the image, you should notice two things:

  • When the second point is to the left of the first, lengthdir_x is negative.
  • When the second point is to the right of the first, lengthdir_x is positive.
Personal tools