Code: Alles auswählen
;eine kleine Raycasting Engine (Flat shaded)
;Controls: Pfeiltasten (gehen/drehen)
; A,D: strafen
#mapWidth = 24
#mapHeight = 24
Global Dim worldMap.l(#mapWidth,#mapHeight)
For j = 0 To 24
For i = 0 To 24
If j = 0 Or j = 24 Or i = 0 Or i = 24
worldMap(i,j) = 1
EndIf
If i = 12 And j = 12
worldMap(i,j) = 2
EndIf
Next i
Next j
posX.f = 22
posY.f = 12
dirX.f = -1
dirY.f = 0
planeX.f = 0
planeY.f = 0.66
time = 0
oldTime = 0
InitKeyboard()
InitSprite()
OpenScreen(512,384,32,"3D")
Repeat
Delay(0)
ClearScreen(0)
StartDrawing(ScreenOutput())
Box(0,0,512,192,RGB(90,150,220))
Box(0,192,512,192,RGB(160,160,160))
For x = 0 To 512
cameraX.f = 2 * x / 512 - 1
rayPosX.f = posX
rayPosY.f = posY
rayDirX.f = dirX + planeX * cameraX
rayDirY.f = dirY + planeY * cameraX
mapx = Int(rayPosX)
mapy = Int(rayPosY)
sideDistX.f
sideDistY.f
deltaDistX.f = Sqr(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX))
deltaDistY.f = Sqr(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY))
perpWallDist.f
stepX.l
stepY.l
hit.l = 0
side.l
If rayDirX < 0
stepX = -1
sideDistX = (rayPosX - mapx) * deltaDistX
Else
stepX = 1
sideDistX = (mapx + 1.0 - rayPosX) * deltaDistX
EndIf
If rayDirY < 0
stepY = -1
sideDistY = (rayPosY - mapy) * deltaDistY
Else
stepY = 1
sideDistY = (mapy + 1.0 - rayPosY) * deltaDistY
EndIf
While hit = 0
If sideDistX < sideDistY
sideDistX + deltaDistX
mapx + stepX
side = 0
Else
sideDistY + deltaDistY
mapy + stepY
side = 1
EndIf
If worldMap(mapx,mapy) > 0
hit = 1
EndIf
Wend
If side = 0
perpWallDist = Abs((mapx - rayPosX + (1 - stepX) / 2) / rayDirX)
Else
perpWallDist = Abs((mapy - rayPosY + (1 - stepY) / 2) / rayDirY)
EndIf
lineHeight = Abs(Int(384 / perpWallDist))
drawStart = -lineHeight / 2 + 384 / 2
If drawStart < 0
drawStart = 0
EndIf
drawEnd = lineHeight / 2 + 384 / 2
If drawEnd >= 384
drawEnd = 384 - 1
EndIf
If worldMap(mapx,mapy) = 1
color = RGB(255,0,0)
ElseIf worldMap(mapx,mapy) = 2
color = RGB(0,255,0)
Else
color = RGB(0,0,255)
EndIf
If side = 1
color = RGB(Red(color)/2,Green(color)/2,Blue(color)/2)
EndIf
LineXY(x,drawStart,x,drawEnd,color)
Next x
oldTime = time
time = ElapsedMilliseconds()
frameTime.f = (time - oldTime) / 1000.0
DrawingMode(1)
DrawText(0,0,StrF(1.0 / frameTime),RGB(255,255,255))
StopDrawing()
FlipBuffers()
moveSpeed.f = frameTime * 5.0
rotSpeed.f = frameTime * 3.0
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Up)
If(worldMap(Int(posX + dirX * moveSpeed),Int(posY)) = #False)
posX + dirX * moveSpeed
EndIf
If(worldMap(Int(posX),Int(posY + dirY * moveSpeed)) = #False)
posY + dirY * moveSpeed
EndIf
EndIf
If KeyboardPushed(#PB_Key_Down)
If(worldMap(Int(posX - dirX * moveSpeed),Int(posY)) = #False)
posX - dirX * moveSpeed
EndIf
If(worldMap(Int(posX),Int(posY - dirY * moveSpeed)) = #False)
posY - dirY * moveSpeed
EndIf
EndIf
If KeyboardPushed(#PB_Key_A)
If(worldMap(Int(posX - planeX * moveSpeed),Int(posY)) = #False)
posX - planeX * moveSpeed
EndIf
If(worldMap(Int(posX),Int(posY - planeY * moveSpeed)) = #False)
posY - planeY * moveSpeed
EndIf
ElseIf KeyboardPushed(#PB_Key_D)
If(worldMap(Int(posX + planeX * moveSpeed),Int(posY)) = #False)
posX + planeX * moveSpeed
EndIf
If(worldMap(Int(posX),Int(posY + planeY * moveSpeed)) = #False)
posY + planeY * moveSpeed
EndIf
EndIf
If KeyboardPushed(#PB_Key_Right)
oldDirX.f = dirX
dirX = dirX * Cos(-rotSpeed) - dirY * Sin(-rotSpeed)
dirY = oldDirX * Sin(-rotSpeed) + dirY * Cos(-rotSpeed)
oldPlaneX.f = planeX
planeX = planeX * Cos(-rotSpeed) - planeY * Sin(-rotSpeed)
planeY = oldPlaneX * Sin(-rotSpeed) + planeY * Cos(-rotSpeed)
EndIf
If KeyboardPushed(#PB_Key_Left)
oldDirX.f = dirX
dirX = dirX * Cos(rotSpeed) - dirY * Sin(rotSpeed)
dirY = oldDirX * Sin(rotSpeed) + dirY * Cos(rotSpeed)
oldPlaneX.f = planeX
planeX = planeX * Cos(rotSpeed) - planeY * Sin(rotSpeed)
planeY = oldPlaneX * Sin(rotSpeed) + planeY * Cos(rotSpeed)
EndIf
Until KeyboardPushed(1)
