GameMaker: Bezier curve/path simulation

Bezier curve/path simulation in GameMaker

If you are working with GameMaker, you might have wandered, how to make paths similar to standard ones but in 3 dimensions, or how to efficiently draw path of non-1px width, or other things not covered by path functions.

For this reason, I have made the following function:

(technically, it is a port of my AS3 function, which in turn was built around explanation by xot)

/// bezier(position, ordinates:ds_list)
var _list, _pos, _length, _index, p, c, n;
_list = argument1
_length = ds_list_size(_list)
_pos = max(0, min(1, argument0)) * _length;
_index = min(floor(_pos), _length - 1);
_pos -= _index // _pos is now (0..1)
// previous\current\next value:
p = ds_list_find_value(_list, max(_index - 1, 0))
c = ds_list_find_value(_list, _index)
n = ds_list_find_value(_list, min(_length - 1, _index + 1))
// actual calculations (yes, you need to know the formula for these):
return .5 * (((p - 2 * c + n) * _pos + 2 * (c - p)) * _pos + p + c)

Arguments:

  • argument0 'position' is position in list (0..1). Script also handles 'out of range' problems just in case you forget to make sure it's in valid range.
  • argument1 'points' is a pointer (index) of ds_list containing list of point coordinates (or rather, values, since this is a 1-dimensional array, but anyway). Normally you would want a single list and script call per each axis (2d = 2 lists, 3d = 3 lists, etc.).

How to use it: first, you will need several ds_list's, which will contain your values. Then, for each point, add point's coordinates into according lists. The following code would convert a existing 2d path into two ds_list's:
list_x = ds_list_create()
list_y = ds_list_create()
path = path_test;
for (i = 0; i < path_get_number(path); i += 1) {
    ds_list_add(list_x, path_get_point_x(path, i))
    ds_list_add(list_y, path_get_point_y(path, i))
}

If you want to simulate exact GameMaker's behavior relating to number of points, set for-loop step to 1 / (length << precision), where length is number of points in path, and precision is 'precision' parameter of path. Initial value of loop variable should be set to half of step value.

Download GMK

Related posts:

5 thoughts on “GameMaker: Bezier curve/path simulation

  1. this is a most useful game maker tutorial page i’ve found.
    Thanks Yallll
    now i can go on making my game with your tut :)

  2. Thanks for the code.
    I’m expanding the script to work with closed paths, but I want understand the formula, how did you get the mathematical expression?, is really a Bézier curve?

    • I cannot easily locate the source material that I have used for formula, but I believe that it was a modification of cubic bézier curve formula. GameMaker’ curves are slighlty non-standard due to “handle points” being decided automatically, which probably means that you cannot particularly classify them as bezier curves of any given order.

      As per closed paths, I believe that you need to taking last point’s ordientate when calculating for the first and vice versa to have that working correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.