MeBoingus
Member
Hi all,
I'm working on a little game that has 'floor tiles' aligned to a 64x64 grid.
I need to be able to click inside of any specific cell in the grid, and have my object create a path to it.
That is easy enough. It doesn't require my little pigeon brain to do maths.
The problem is that I now want the object to create the path with the last point being as close to the perimeter of the cell as possible, before crossing into it.
EDIT: Basically I need to create a path that gets as close to the grid cell as possible, without entering it. Almost as if I wanted to be able to have the object 'collide' with it.
My way of describing this sucks, so here's my code thus far, and a poor illustration of what I mean:
As you can see, my code DOES ensure that the point is "outside" of the grid cell, by placing it 64 pixels away from that position. I need to somehow calculate how far the point inside of the cell is from perimeter of the cell it's in, and then set adjust the point's position by that amount, in that direction.
Here's the above-mentioned poor illustration:
The red box is our object.
The X is the final position on our path.
The blue line is where the object should end up.
The purple line where the object does end up, or more specifically the distance I need to calculate.
Any help would be greatly appreciated. If you know of a better way to achieve this, throw your ideas at me by all means.
I'm working on a little game that has 'floor tiles' aligned to a 64x64 grid.
I need to be able to click inside of any specific cell in the grid, and have my object create a path to it.
That is easy enough. It doesn't require my little pigeon brain to do maths.
The problem is that I now want the object to create the path with the last point being as close to the perimeter of the cell as possible, before crossing into it.
EDIT: Basically I need to create a path that gets as close to the grid cell as possible, without entering it. Almost as if I wanted to be able to have the object 'collide' with it.
My way of describing this sucks, so here's my code thus far, and a poor illustration of what I mean:
GML:
if (mouse_check_button_pressed(mb_left))
{
path_clear_points(myPath);
path_add_point(myPath, x, y, 100);
path_add_point(myPath, mouse_x, mouse_y, 100);
gridX = floor(path_get_point_x(myPath, 1) / 64) * 64;
gridY = floor(path_get_point_y(myPath, 1) / 64) * 64;
var tX = path_get_point_x(myPath, 1);
var tY = path_get_point_y(myPath, 1);
var d = point_direction(tX, tY, path_get_point_x(myPath, 0), path_get_point_y(myPath, 0));
path_change_point(myPath, 1, tX + lengthdir_x(64, d), tY + lengthdir_y(64, d), 100);
}
Here's the above-mentioned poor illustration:
The red box is our object.
The X is the final position on our path.
The blue line is where the object should end up.
The purple line where the object does end up, or more specifically the distance I need to calculate.
Any help would be greatly appreciated. If you know of a better way to achieve this, throw your ideas at me by all means.
Last edited: