
-----------------------------------
SNIPERDUDE
Tue Sep 09, 2008 12:01 pm

Raytracing help
-----------------------------------
I don't remember if it is called raycasting or raytracing, but that's not the point.

Either who, I seem to be stuck.
It seems to be not measuring right for some reason, and I can't figure it out.
Here's the code:

setscreen ("graphics:480;300, nobuttonbar, offscreenonly")

type PlayerProp :
    record
       X, Y : int
       Vel, Angle : real 
    end record
    
type TileProp :
    record
        Wall : boolean
    end record

    
var Player : PlayerProp
var Tile : array 1 .. 5 of TileProp
var Grid : array 1 .. 10, 1 .. 10 of int
var Ray : array 0 .. 480 of int

%%
Grid (1, 1) := 0
Grid (1, 2) := 0
Grid (1, 3) := 0
Grid (1, 4) := 0
Grid (1, 5) := 7
Grid (1, 6) := 0
Grid (2, 1) := 0
Grid (2, 2) := 0
Grid (2, 3) := 0
Grid (2, 4) := 0
Grid (2, 5) := 7
Grid (2, 6) := 0
Grid (3, 1) := 0
Grid (3, 2) := 0
Grid (3, 3) := 0
Grid (3, 4) := 0
Grid (3, 5) := 7
Grid (3, 6) := 0
Grid (4, 1) := 0
Grid (4, 2) := 0
Grid (4, 3) := 0
Grid (4, 4) := 0
Grid (4, 5) := 7
Grid (4, 6) := 0
Grid (5, 1) := 7
Grid (5, 2) := 7
Grid (5, 3) := 7
Grid (5, 4) := 7
Grid (5, 5) := 7
Grid (5, 6) := 0
Grid (6, 1) := 0
Grid (6, 2) := 0
Grid (6, 3) := 0
Grid (6, 4) := 0
Grid (6, 5) := 0
Grid (6, 6) := 0


Player.X := 53
Player.Y := 3
Player.Angle := 45


proc Draw2D
    for y : 1 .. 5
        for x : 1 .. 5
            drawfillbox ((x - 1) * 60, (y - 1) * 60, (x - 1) * 60 + 59, (y - 1) * 60 + 59, Grid (x, y))
        end for
    end for
end Draw2D

fcn tangent (angle : real) : real
    result sind (angle) / cosd (angle)
end tangent

fcn getDist (x, y, x2, y2 : real) : real
    if x2 > x and y2 > y then
        result sqrt ((x2 - x) ** 2 + (y2 - y) ** 2)
    elsif x2 > x and y2 < y then
        result sqrt ((x2 - x) ** 2 + (y - y2) ** 2)
    elsif x2 > x and y2 = y then
        result (x2 - x)
    elsif x2 < x and y2 > y then
        result sqrt ((x - x2) ** 2 + (y2 - y) ** 2)
    elsif x2 = x and y2 > y then
        result (y2 - y)
    elsif x2 = x and y2 = y then
        result 0
    elsif x2 < x and y2 < y then
        result sqrt ((x - x2) ** 2 + (y - y2) ** 2)
    elsif x2 < x and y2 = y then
        result (x - x2)
    elsif x2 = x and y2 < y then
        result (y - y2)
    end if
end getDist

proc HorizDist (ray, run, px, py : int, xa, angle : real)
    var Ax, Ay : int
    
    %if angle > 0 and angle < 180 then
        Ay := round (floor (Player.Y / 20) * 20 + 20)
    %elsif angle > 180 and angle < 360 then
        %Ay := round (floor (Player.Y / 20) * 20 - 1)
    %else
        %Ay := Player.Y
    %end if
    
    Ax := floor (((Ay - Player.Y) / tangent(angle)) + Player.X)

    var x : int := Ax + round (xa * run)
    var y : int := Ay + (20 * run)
    
    if floor (x / 60) + 1  270 and angle  90 and angle < 270 then
        %Bx := round (floor (Player.X / 20) * 20 - 1)
    %else
        %Bx := Player.X
    %end if
    
    By := round (((Bx - Player.X) * tangent(angle)) + Player.Y)
    
    drawdot (Bx, By, brightred)
    
    var x : int := Bx + (20 * run)
    var y : int := By + round (ya * run)
    
    %put "Dist: " + intstr(round (getDist (Player.X, Player.Y, x * 20, y * 20)))
    %put "X: " + intstr(x)
    %put "Y: " + intstr(y)
    %put "Run: " + intstr (run)
    
    if floor (x / 60) + 1  0 and Tr1 = 0 then
                Ray(i) := Tr2
            elsif Tr1 > Tr2 and Tr2 > 0 and Tr1 > 0 then
                Ray(i) := Tr1
            elsif Tr2 > Tr1 and Tr1 > 0 and Tr2 > 0 then
                Ray(i) := Tr1
            end if
            
            drawline (Player.X, Player.Y, Player.X + round (cosd (angle) * Ray(i)), Player.Y + round (sind (angle) * Ray(i)), 14)
            %drawline (Player.X, Player.Y, Player.X + round (cosd (angle) * Tr2), Player.Y + round (sind (angle) * Tr2), brightred)
            
            exit when Ray (i) > 0
        end for

        
        %drawline (Player.X, Player.Y, Player.X + round (cosd (angle) * Ray(i)), Player.Y + round (sind (angle) * Ray(i)), 14)
    end for
end FindDist



loop
    var t : int := Time.Elapsed
    cls
    Draw2D
    FindDist
    put intstr (Time.Elapsed - t) + " FPS"
    View.Update
    
    exit when hasch
end loop

Sorry there is no real commenting, as I said I am still just experimenting with it.
A rough copy really.

At the moment it is just drawing the lines just so I can see the problem.
Thanks in advance for any help.

-----------------------------------
syntax_error
Tue Sep 09, 2008 5:42 pm

Re: Raytracing help
-----------------------------------
before you fix anything :


%% 
Grid (1, 1) := 0 
Grid (1, 2) := 0 
Grid (1, 3) := 0 
Grid (1, 4) := 0 
Grid (1, 5) := 7 
Grid (1, 6) := 0 
Grid (2, 1) := 0 
Grid (2, 2) := 0 
Grid (2, 3) := 0 
Grid (2, 4) := 0 
Grid (2, 5) := 7 
Grid (2, 6) := 0 
Grid (3, 1) := 0 
Grid (3, 2) := 0 
Grid (3, 3) := 0 
Grid (3, 4) := 0 
Grid (3, 5) := 7 
Grid (3, 6) := 0 
Grid (4, 1) := 0 
Grid (4, 2) := 0 
Grid (4, 3) := 0 
Grid (4, 4) := 0 
Grid (4, 5) := 7 
Grid (4, 6) := 0 
Grid (5, 1) := 7 
Grid (5, 2) := 7 
Grid (5, 3) := 7 
Grid (5, 4) := 7 
Grid (5, 5) := 7 
Grid (5, 6) := 0 
Grid (6, 1) := 0 
Grid (6, 2) := 0 
Grid (6, 3) := 0 
Grid (6, 4) := 0 
Grid (6, 5) := 0 
Grid (6, 6) := 0 



for loops, whats the point of the array then?

-----------------------------------
CodeMonkey2000
Tue Sep 09, 2008 7:59 pm

RE:Raytracing help
-----------------------------------
I didn't really look at the code, but from the looks of your program, you are choose the wrong ray to draw. You want to draw the ray with the smaller path, you seem to be drawing the ray with a larger distance. It's called raycasting.

-----------------------------------
SNIPERDUDE
Wed Sep 10, 2008 6:21 am

Re: Raytracing help
-----------------------------------
for loops, whats the point of the array then?

It is just there as a temp, it will be read from a file later on.
This is just "rough draft" , or just a small clip of code that is not permanent.

And I tried switching the check to this, but it seems to only have half worked:

if Tr1 > Tr2 then
    Ray(i) := Tr2
elsif Tr2 > Tr1 then
    Ray(i) := Tr1
end if

Any thoughts?

-----------------------------------
SNIPERDUDE
Wed Oct 08, 2008 8:20 pm

RE:Raytracing help
-----------------------------------
*bump*

Same question, decided to work on this old game.
Any help greatly appreciated.

-----------------------------------
HellblazerX
Thu Oct 09, 2008 12:33 am

Re: Raytracing help
-----------------------------------
if Tr1 > Tr2 then
    Ray(i) := Tr2
elsif Tr2 > Tr1 then
    Ray(i) := Tr1
end if
You should have the elsif simply be an else, because you losing cases where the vertical and horizontal rays are equal to each in lengths.  Normally, this is very unlikely, but the fact that you using integers for all your values greatly increases the chances.
