Datky
Member
Hey here,
I'm currently on GMS 2.3 and I'm trying to do a simple raycasting prototype.
I have an array of 0s and 1s giving the position of walls in the grid of the room (so the size of the array is room_width div 32, room_height div 32)
I first cast an "imaginary" ray that will check for a collision on the y axis, and stop when it finds a wall on a horizontal line. I save the x, y coordinates of that collision.
Then I do the same with the x axis, check for collisions on vertical lines, save the coordinates.
I now hace 2 positions in the room, I now cast the "real" ray to the shortest coordinate, and it should stop to the wall. At first, it seems to be working well :
A green ray is a collision on the y axis, a red ray is a collision on the x axis.
The issue is, it doesn't *always* work ( I drew both rays for the example but in reality I would get the red one) :
On the top left, distX is the distance to the nearest vertical collision, distY the distance to the nearest horizontal collision, I get these values from :
Where
are the coordinates of the collision with the y axis,
with the x axis.
As you can see, each of the two rays are working the way I intended them to work, but the distance values are off. distY should be smaller than distX in this case, and since I'm comparing the two to choose which ray to cast, I get the wrong way like shown on the previous image.
There must be something wrong with the way I calculate the distance but I'm using the GameMaker fonctions, so it should give me the good value, right ?
What am I doing wrong here ?
Thanks
I'm currently on GMS 2.3 and I'm trying to do a simple raycasting prototype.
I have an array of 0s and 1s giving the position of walls in the grid of the room (so the size of the array is room_width div 32, room_height div 32)
I first cast an "imaginary" ray that will check for a collision on the y axis, and stop when it finds a wall on a horizontal line. I save the x, y coordinates of that collision.
Then I do the same with the x axis, check for collisions on vertical lines, save the coordinates.
I now hace 2 positions in the room, I now cast the "real" ray to the shortest coordinate, and it should stop to the wall. At first, it seems to be working well :
A green ray is a collision on the y axis, a red ray is a collision on the x axis.
The issue is, it doesn't *always* work ( I drew both rays for the example but in reality I would get the red one) :
On the top left, distX is the distance to the nearest vertical collision, distY the distance to the nearest horizontal collision, I get these values from :
Code:
var distY = point_distance(x, y, ynextX, ynextY);
var distX = point_distance(x, y, xnextX, xnextY);
draw_line_color(x, y, x + xnextX, y + xnextY, c_red, c_red);
draw_line_color(x, y, x + ynextX, y + ynextY, c_lime, c_lime);
Code:
ynextX, ynextY
Code:
xnextX, xnextY
As you can see, each of the two rays are working the way I intended them to work, but the distance values are off. distY should be smaller than distX in this case, and since I'm comparing the two to choose which ray to cast, I get the wrong way like shown on the previous image.
There must be something wrong with the way I calculate the distance but I'm using the GameMaker fonctions, so it should give me the good value, right ?
What am I doing wrong here ?
Thanks