Legacy GM Vision Cone Problem


Ben Hubble

Screenshot (9).png Screenshot (10).png Hello, right now i'm making a ray casting game (the illusion of 3D) but I've run into a problem and i'm not able to do myself. Basically, (This is all in the 2D room) I've got my 90 degree vision cone coming out like a triangle from my player. I've done this by drawing 3 lines. Then I send a collision_line across each degree of the vision cone using lengthdir_x and lengthdir_y, that's my problem. I'll show you my code:
Draw Event:
///Draw Vision Cone
direction = point_direction(x, y, mouse_x, mouse_y);

lineLeftPos = direction - 45;
lineRightPos = direction + 45;

leftLine = draw_line(x,y,x + lengthdir_x(250,lineLeftPos),y + lengthdir_y(250,lineLeftPos));
rightLine = draw_line(x,y,x + lengthdir_x(250,lineRightPos),y + lengthdir_y(250,lineRightPos));
topLine = draw_line(x + lengthdir_x(250,lineLeftPos),y + lengthdir_y(250,lineLeftPos),x + lengthdir_x(250,lineRightPos),y + lengthdir_y(250,lineRightPos));

var i;
for(i = lineLeftPos; i < lineRightPos; i++)
rayCast = collision_line(x,y,x + lengthdir_x(250,i),y + lengthdir_y(250,i),obj_wall,false,true);
draw_line(x,y,x + lengthdir_x(250,i),y + lengthdir_y(250,i));

My problem is, since my vision cone is a triangle, the collision_lines stick out past the base of the triangle (Furthest away from the player). I don't have a clue how to get the collision_lines to stop once it reaches the end of the triangle. I've put a few pictures to help you understand (The first picture shows that the lines are meant to stop at the triangle base, the second picture show where the lines actually go)


Why dont you just use the point_in_triangle function?

Its far more optimized to find a point in range than doing endless collision lines.


Or if you don't mind doing it - you could hard code each of the collision lines with an appropriate length... Sounds like a pain, but you may not one for each degree - you could cut it down to 10 or so.

Ben Hubble

Thank you, this is exactly what I need, (although I kinda get lost on what it's doing a bit, ok a lot)