mdx = 1280 / 2: mdy = 720 / 2 ' The Middle of the screen
wheight = 500 ' our wall height
pheight = 0
' lines start and end + player position
px = 0: py = 0
' lines will be in an array
wall(1, 1) = -10: wall(1, 2) = 10: wall(1, 3) = 10: wall(1, 4) = 10: wall(1, 5) = 10: wall(1, 6) = -1000
wall(2, 1) = 10: wall(2, 2) = 10: wall(2, 3) = 10: wall(2, 4) = -10: wall(2, 5) = 20: wall(2, 6) = 0
wall(3, 1) = 10: wall(3, 2) = -10: wall(3, 3) = 5: wall(3, 4) = -15: wall(3, 5) = -20: wall(3, 6) = 0
wall(4, 1) = -5: wall(4, 2) = -15: wall(4, 3) = -10: wall(4, 4) = -10: wall(4, 5) = -5: wall(4, 6) = 0
wall(5, 1) = -10: wall(5, 2) = -10: wall(5, 3) = -10: wall(5, 4) = 10: wall(5, 5) = 10: wall(5, 6) = 0
' players angle
angle = 0
scale = 100
pi = 3.14159
fwd = 0: bwd = 0: left = 0: right = 0: strafel = 0: strafer = 0: scaler = 0
' here goes the action now
' transform the wall into players position
wx1 = wall(i, 1): wy1 = wall(i, 2): wx2 = wall(i, 3): wy2 = wall(i, 4)
tx1 = wx1 - px: ty1 = wy1 - py: tx2 = wx2 - px: ty2 = wy2 - py
' rotate this
rx1
= tx1
* COS(-angle
) - ty1
* SIN(-angle
) ry1
= tx1
* SIN(-angle
) + ty1
* COS(-angle
) rx2
= tx2
* COS(-angle
) - ty2
* SIN(-angle
) ry2
= tx2
* SIN(-angle
) + ty2
* COS(-angle
) ' now we know the start and end point
LINE (mdx
+ rx1
, mdy
+ ry1
)-(mdx
+ rx2
, mdy
+ ry2
), 15 ' draw the line around the player
' now we have our static player and the rotated map
' next thing to do is now project the 2d point to a 3d space
REM ******************** 3D Projection
************************ REM Formula
is quite simple
REM Use ry
as scaling factor ... zx1
= rx1
/ry1
REM upper
and lower will be calculated with zu1
= wheight
/ ry1
' render nothing behind us
' Clip the lines if one is partially behind the player
' this will be done with intersection calculation
' clip the line at y-axis
iy = ry1 - (ry1 - ry2) / (rx1 - rx2) * rx1 ' line intersection calculation with simple linear function y = 0
IF rx1
<= 0 THEN rx1
= 0.01: ry1
= iy
IF rx2
<= 0 THEN rx2
= 0.01: ry2
= iy
' Calculations here
zx1 = -ry1 * scale / rx1
zd1 = -wheight / rx1 - wall(i, 5) / rx1 - pheight
zu1 = wheight / rx1 - wall(i, 6) / rx1 - pheight
zx2 = -ry2 * scale / rx2
zd2 = -wheight / rx2 - wall(i, 5) / rx2 - pheight
zu2 = wheight / rx2 - wall(i, 6) / rx2 - pheight
' draw the lines
'LINE (mdx * zx1 / 2 + mdx, mdy * zu1 / 2 + mdy)-(mdx * zx2 / 2 + mdx, mdy * zu2 / 2 + mdy), 14
'LINE (mdx * zx1 / 2 + mdx, mdy * zd1 / 2 + mdy)-(mdx * zx2 / 2 + mdx, mdy * zd2 / 2 + mdy), 14
'LINE (mdx * zx1 / 2 + mdx, mdy * zu1 / 2 + mdy)-(mdx * zx1 / 2 + mdx, mdy * zd1 / 2 + mdy), 6
'LINE (mdx * zx2 / 2 + mdx, mdy * zu2 / 2 + mdy)-(mdx * zx2 / 2 + mdx, mdy * zd2 / 2 + mdy), 6
' draw the lines
LINE (zx1
+ mdx
, zu1
+ mdy
)-(zx2
+ mdx
, zu2
+ mdy
), 14 LINE (zx1
+ mdx
, zd1
+ mdy
)-(zx2
+ mdx
, zd2
+ mdy
), 14 LINE (zx1
+ mdx
, zu1
+ mdy
)-(zx1
+ mdx
, zd1
+ mdy
), 6 LINE (zx2
+ mdx
, zu2
+ mdy
)-(zx2
+ mdx
, zd2
+ mdy
), 6
notrender:
' modified key input to detect several keypresses
IF kpress
= 18432 THEN fwd
= 1 IF kpress
= -18432 THEN fwd
= 0 IF kpress
= 20480 THEN bwd
= 1 IF kpress
= -20480 THEN bwd
= 0 IF kpress
= 19200 THEN left
= 1 IF kpress
= -19200 THEN left
= 0 IF kpress
= 19712 THEN right
= 1 IF kpress
= -19712 THEN right
= 0 IF kpress
= 97 THEN strafel
= 1 IF kpress
= -97 THEN strafel
= 0 IF kpress
= 100 THEN strafer
= 1 IF kpress
= -100 THEN strafer
= 0 IF kpress
= 43 THEN scaler
= 1 IF kpress
= -43 THEN scaler
= 0 IF kpress
= 45 THEN scaler
= -1 IF kpress
= -45 THEN scaler
= 0 IF fwd
= 1 THEN px
= px
+ 0.2 * COS(angle
): py
= py
+ 0.2 * SIN(angle
):
CLS IF bwd
= 1 THEN px
= px
- 0.2 * COS(angle
): py
= py
- 0.2 * SIN(angle
):
CLS IF strafel
= 1 THEN px
= px
+ 0.2 * COS(angle
+ 0.5 * pi
): py
= py
+ 0.2 * SIN(angle
+ 0.5 * pi
):
CLS IF strafer
= 1 THEN px
= px
- 0.2 * COS(angle
+ 0.5 * pi
): py
= py
- 0.2 * SIN(angle
+ 0.5 * pi
):
CLS
REM (rx1
* ry2
- rx2
* ry1
)/(rx1
- rx2
)