ReadySteadyGo
2010-07-25 11:34:11
Can anyone help me? I've planned it all out on paper but it's not giving me the distances. It's driving me mental.
Because there is so much to explain in the script I will just post it then if you don't understand specifics I can explain.
Because there is so much to explain in the script I will just post it then if you don't understand specifics I can explain.
program new;
var
WallArray: TPointArray;
ViewPoint: TPoint;
ViewAngle: Integer;
const
PROJECTION_PLANE_W = 640;
PROJECTION_PLANE_H = 400;
PROJECTION_PLANE_CENTER_X = 320;
PROJECTION_PLANE_CENTER_Y = 200;
WALL_UNIT = 128;
WALL_H = 128;
PLAYER_H = 64;
FOV = 60; // Field of vision (degrees)
DIST_PLAYER_TO_PROJECTION_PLANE = 554;
// RAY_ANGLE = 60 / 640; Angle between subsequent rays. For reference.
MAP_WIDTH = 12;
function Tan(e: Extended): Extended;
begin
Result := sin(e) / cos(e);
end;
function Radians(e: Extended): Extended;
begin
Result := e * 180 / Pi;
end;
procedure LoadWallPoints;
begin
WallArray := [Point(2, 2),
Point(3, 2),
Point(4, 2),
Point(2, 3),
Point(4, 3),
Point(2, 4),
Point(3, 4),
Point(4, 4)];
end;
function IsWall(x, y: Integer): Boolean;
var
j, Hi: Integer;
begin
Result := False;
x := x / WALL_UNIT;
y := y / WALL_UNIT;
Hi := High(WallArray);
for j := 0 to Hi do
if (WallArray[j].x = x) and (WallArray[j].y = y) then
begin
Result := True;
Break;
end;
end;
procedure FindWall;
var
i, k, VertX, VertY, HorX, HorY, Ax, Ay, Xa, Ya, DistA, DistB: Integer;
RayAngle: Extended;
FacingNorth: Boolean;
RayDist: TIntegerArray;
begin
SetArrayLength(RayDist, PROJECTION_PLANE_W);
RayAngle := FixD(ViewAngle - FOV/2);
for i := 0 to 50 do//PROJECTION_PLANE_W - 1 do
begin
//WriteLn(i);
if (RayAngle < 90) or (RayAngle > 270) then // Pointing North
begin
FacingNorth := True;
Ay := ((ViewPoint.y / WALL_UNIT) * WALL_UNIT) - 1
end else
begin
FacingNorth := False;
Ay := ((ViewPoint.y / WALL_UNIT) * WALL_UNIT) + WALL_UNIT;
end;
//WriteLn(Ay);
Ax := Trunc((ViewPoint.x + (ViewPoint.y - Ay)) / Tan(Radians(RayAngle)));
if FacingNorth then
Ya := -WALL_UNIT
else
Ya := WALL_UNIT;
Xa := Trunc(WALL_UNIT / Tan(Radians(RayAngle)));
if not IsWall(Ax, Ay) then
begin
for k := 0 to MAP_WIDTH do
begin
HorX := Ax + Xa;
HorY := Ay + Ya;
if IsWall(HorX, HorY) then
Break;
Ax := HorX;
Ay := HorY;
end
end else
begin
HorX := Ax;
HorY := Ay;
end;
if (RayAngle < 180) then // Pointing Right
begin
FacingNorth := True;
Ax := ((ViewPoint.y / WALL_UNIT) * WALL_UNIT) + WALL_UNIT
end else
begin
FacingNorth := False;
Ax := ((ViewPoint.y / WALL_UNIT) * WALL_UNIT) - 1;
end;
Ay := Trunc((ViewPoint.y + (ViewPoint.x - Ax)) * Tan(Radians(RayAngle)));
if FacingNorth then
Xa := WALL_UNIT
else
Xa := -WALL_UNIT;
Ya := Trunc(WALL_UNIT * Tan(Radians(RayAngle)));
if not IsWall(Ax, Ay) then
begin
for k := 0 to MAP_WIDTH do
begin
VertX := Ax + Xa;
VertY := Ay + Ya;
if IsWall(VertX, VertY) then
Break;
Ax := VertX;
Ay := VertY;
end
end else
begin
VertX := Ax;
VertY := Ay;
end;
DistA := Distance(ViewPoint.x, ViewPoint.y, HorX, HorY);
DistB := Distance(ViewPoint.x, ViewPoint.y, VertX, VertY);
if DistA < DistB then
RayDist[i] := DistA
else
RayDist[i] := DistB;
WriteLn(DistA);
WriteLn(DistB);
//WriteLn(RayDist[i]);
//WriteLn(i);
RayAngle := FixD(RayAngle + 0.09375);
//WriteLn(RayAngle);
end;
end;
begin
LoadWallPoints;
//WriteLn(WallArray[1].x);
ViewPoint := Point(128, 128);
ViewAngle := 45;
FindWall;
end.