Login | Register

Motion planning

July 18, 2006, 12:22 by Calle
[loading]
-->
Motion planning is really useful, and you will discover that especially for AI’s, it’s almost always a must. Now there is a good example of this in the registered version demo of 6.1. Mark Overmars wrote it himself, the initialization:
{
mpgrid = mp_grid_create(0,0,20,15,32,32);
mp_grid_add_instances(mpgrid,obj_motion_wall,false);
mppath = -1;
}


As you can see, he is creating a gride-based system and this is particularly useful for labyrinth and similar, although it’s hard to use RTS and other. Mark describes why in his help file:

“ In particular the size of the cells is crucial. Remember that the cells must be large enough so that the moving object placed with its origin on the center of a cell must lie completely inside the cell.”

Now this is really hard to plan and make sure in an RTS, but as Mark also points out: the smaller the cells the more optional paths. I would anyway recommend you to use the potential motion planning system if your game contains unforeseeable situations. The advantage of this grid based system is that it easy to add those instances which should be forbidden, rather than all being forbidden. In this example obj_motion_wall is added. This is how the step event goes:

{
var xx,yy;
xx = floor(mouse_x/32)*32 + 16;
yy = floor(mouse_y/32)*32 + 16;
if not position_empty(xx,yy) exit;
mp_grid_path(mpgrid,path_motion,x,y,xx,yy,false);
path_start(path_motion,4,0,true);
}


Notice the connection between the xx and yy calculations and the grid initialization; where each square are set to the width and height of 32. xx and yy is the destination; but not it’s not the exact position of the mouse. Firstly the real x and y positions are divided by 32, and the rounded down to the closest integer, which will the return the square number. To get the real numbers back the new value are multiplied by 32, this return an inexact number – the left-top corner of the square. As the gride squares are 32*32 (and so is the sprite), by adding 16 he places the sprite in the middle of the square. Always in the middle, which is a must, or it would collide with a wall. And if an object in a gride based system collides with an forbidden object it will just stop, and never reach its destination. In the next function a path is called, which must be manually created in the Game Maker interface, but the path might very well be empty.

You may download the test example by Mark Overmars at this address: http://www.gamemaker.nl/games_edit6/demo61.zip

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games