Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Raytracing help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SNIPERDUDE




PostPosted: Tue Sep 09, 2008 12:01 pm   Post subject: 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:

Turing:
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 <= 6 and floor (y / 60) + 1 <= 6 then
    if Grid (floor (x / 60) + 1, floor (y / 60) + 1) = 7 then
        Ray (ray) := round (getDist (Player.X, Player.Y, x, y))
    end if
    end if
end HorizDist

proc VertDist (ray, run, px, py : int, ya, angle : real)
    var Bx, By : int
   
    %if angle >= 0 and angle < 90 or angle > 270 and angle <= 360 then
        Bx := round (floor (Player.X / 20) * 20 + 20)
    %elsif 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 <= 6 and floor (y / 60) + 1 <= 6 then
        if Grid (floor (x / 60) + 1, floor (y / 60) + 1) = 7 then
            Ray (ray) := round (getDist (Player.X, Player.Y, x, y))
        end if
    end if
end VertDist

proc FindDist
    var angle : real := Player.Angle + (60 / 2)
    var angleI : real := 60 / 480
    var xa, ya : real := 0
    var px, py : int := 0
   
    px := floor (Player.X / 20) * 20
    py := floor (Player.Y / 20) * 20

    for i : 0 .. 479
        angle -= angleI
        Ray (i) := 0
        xa := 20 / tangent(angle)
        ya := 20 * tangent(angle)
       
        for k : 0 .. 100
            var Tr1, Tr2 : int := 0
            VertDist (i, k, px, py, ya, angle)
            Tr1 := Ray(i)
            HorizDist (i, k, px, py, xa, angle)
            Tr2 := Ray(i)
           
            %drawline (Player.X, Player.Y, Player.X + round (cosd (angle) * Tr2), Player.Y + round (sind (angle) * Tr2), 14)
            %drawline (Player.X, Player.Y, Player.X + round (cosd (angle) * Tr1), Player.Y + round (sind (angle) * Tr1), brightred)
            %drawline (Player.X, Player.Y, Player.X + round (cosd (angle) * Tr2), Player.Y + round (sind (angle) * Tr2), 14)
           
            if Tr1 = 0 and Tr2 = 0 then
                Ray(i) := 0
            elsif Tr1 > 0 and Tr2 = 0 then
                Ray(i) := Tr1
            elsif Tr2 > 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.
Sponsor
Sponsor
Sponsor
sponsor
syntax_error




PostPosted: Tue Sep 09, 2008 5:42 pm   Post subject: Re: Raytracing help

before you fix anything :

code:

%%
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




PostPosted: Tue Sep 09, 2008 7:59 pm   Post subject: 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




PostPosted: Wed Sep 10, 2008 6:21 am   Post subject: Re: Raytracing help

syntax_error @ September 9th 2008 wrote:
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:

Turing:
if Tr1 > Tr2 then
    Ray(i) := Tr2
elsif Tr2 > Tr1 then
    Ray(i) := Tr1
end if


Any thoughts?
SNIPERDUDE




PostPosted: Wed Oct 08, 2008 8:20 pm   Post subject: RE:Raytracing help

*bump*

Same question, decided to work on this old game.
Any help greatly appreciated.
HellblazerX




PostPosted: Thu Oct 09, 2008 12:33 am   Post subject: Re: Raytracing help

Turing:
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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: