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