Jump to content


Photo

move towards point on a grid


  • Please log in to reply
2 replies to this topic

#1 ConnerCoConnerB

ConnerCoConnerB

    GMC Member

  • GMC Member
  • 84 posts
  • Version:GM8

Posted 02 July 2012 - 03:11 AM

how can i make an object move towards a point while keeping it aligned with the grid
  • 0

#2 $pecter

$pecter

    GMC Member

  • GMC Member
  • 1616 posts
  • Version:GM8

Posted 02 July 2012 - 06:28 AM

Easiest way I can think of is to just use the grid-based motion planning functions. You first need to set-up the grid, and then later you can generate a path to the goal (this can be when a certain event occurs or every few steps [I would not recommend doing it too often as you may slow the game down]).

Set-up Grid (Create event)
grid_size = 16;
grid = mp_grid_create(0, 0, room_width/grid_size, room_height/grid_size, grid_size, grid_size);
mp_grid_add_instances(grid, wall, 0);//don't need this line if you don't need path finding

Generate a path (Alarm or other event)
if(mp_grid_path(grid, path0, x, y, goalx, goaly, 0))
path_start(path0, 5, 0, false);

EDIT: Added successful path check

Edited by $pecter, 02 July 2012 - 06:46 AM.

  • 0

#3 joshuaallen64

joshuaallen64

    GMC Member

  • GMC Member
  • 676 posts
  • Version:GM:Studio

Posted 02 July 2012 - 08:12 AM

I made a simple script for you that's a bit faster then mp_grid:
//scr_move(pointX,pointY,grid size)

var gSize,toX,toY,poX,poY;

gSize = argument2;

toX = (argument0 div gSize) * gSize;
toY = (argument1 div gSize) * gSize;
poX = x
poY = y

if (poY < toY) {
    y += gSize;
}
if (poY > toY){
    y -= gSize;
}

if (poX < toX){
    x += gSize;
}
if (poX > toX){
    x -= gSize;
}

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users