GM:S 1.4 Vision Cone Problem

Discussion in 'Programming' started by Ben Hubble, Jul 1, 2016.

  1. Ben Hubble

    Ben Hubble Guest

    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:
    Code:
    ///Draw Vision Cone
    draw_self();
    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));
    draw_text(32,32,rayCast);
    
    scr_draw_3d_obj(rayCast);
    }
    
    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)
     
  2. Ctl-F

    Ctl-F Guest

    This can be done with simple trig.
    Math.png
    Here is an example implementation.
    screenshot100.png screenshot101.png screenshot102.png screenshot103.png

    Hope this helps.
     
    elementbound likes this.
  3. bojack29

    bojack29 Member

    Joined:
    Jun 21, 2016
    Posts:
    407
    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.
     
    Roa and elementbound like this.
  4. Genetix

    Genetix Member

    Joined:
    Jun 22, 2016
    Posts:
    611
    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.
     
  5. Ben Hubble

    Ben Hubble Guest

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

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice