• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

Windows have problems with ray-casting

Good afternoon! I have a problem. I want to make a game like wolfenstein 3d 1992, but I faced an incomprehensible problem: I made rays that are drawn until an obstacle is encountered, but the ray ends earlier, be it a wall more details in the picture 1603836075853.png
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
Please post code as code, not as screenshots.

 
ok, i post a code

GML:
function radiandrawer(x,y,dir,size){
    
    var i;
    for (i = 70; i != 0; i -=1)
    {   
        var checkedsize = size ;
        var c;
        var unchecked;
        unchecked = true
        for (c = 0; c != size && unchecked ; c += 5)
        {
             if !place_empty(x+xfinder(c,dir+i-35),y+yfinder(c,dir+i-35),obj_wall)
            {
                unchecked = false;
                checkedsize = c;
            }
        }
        draw_line(x,y,x +xfinder(checkedsize,dir+i-35),y + yfinder(checkedsize,dir+i-35));
    }
}

GML:
function xfinder(c,alphadir){
    fx = c * dcos(alphadir)
    
    return fx;
}

GML:
function yfinder(c, alphadir){
    fy = c * dsin(alphadir)
    return -fy;
}



GML:
draw_set_colour(c_lime);
radiandrawer(x,y,direction, 400)
draw_text(100, 100, direction);

draw_self()
 

saffeine

Member
You can use this function to check and see if the calling instance would collide with any other instance in your game. Now, it should be noted that for this to work, the instance running the code must have a valid collision mask (either for the sprite itself, or through the mask_index.) and it will only register collisions with those instances that also have a valid mask.

The function itself basically works by taking the instance and testing for collisions when placed at the position specified by the x/y arguments. The collision checking can be either precise or based on the bounding box of the instance, depending on what kind of collision mask has been selected.
the code is finding a collision with the wall if it were to hypothetically place the player object at those points.
you can actually see the further out lines that end earlier get slightly longer since the sprite is a circle.

swapping out place_empty with position_empty will get you a lot closer, but i think since your loop is going up in increments of 5, there's always a chance that the check will return true up to 5 pixels away.
there may be a faster way to do it than this, but you could always have it check in increments of 5, then if it finds something, continue in increments of a smaller number to find out exactly where it found the wall.
 
Top