diff options
| author | James Barnett <noreply@jamesbarnett.xyz> | 2020-12-28 15:49:23 +0000 |
|---|---|---|
| committer | James Barnett <noreply@jamesbarnett.xyz> | 2020-12-28 15:49:23 +0000 |
| commit | f28500d963d6546feda522d7748a0462a568ba28 (patch) | |
| tree | 0240d3528277930b28790c336ed10d77ea8928e1 /src/main/kotlin/Raycaster.kt | |
| parent | 01727649bb1fbe3e0c721a850bd21973d7e760f2 (diff) | |
| download | kotlin-raycaster-f28500d963d6546feda522d7748a0462a568ba28.tar.xz kotlin-raycaster-f28500d963d6546feda522d7748a0462a568ba28.zip | |
Refactor components
Diffstat (limited to 'src/main/kotlin/Raycaster.kt')
| -rw-r--r-- | src/main/kotlin/Raycaster.kt | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/main/kotlin/Raycaster.kt b/src/main/kotlin/Raycaster.kt new file mode 100644 index 0000000..d3fa694 --- /dev/null +++ b/src/main/kotlin/Raycaster.kt @@ -0,0 +1,42 @@ +import kotlin.math.pow + +class Raycaster(private val stepPrecision: Int = 32) { + + fun raycast(raycastContext: RaycastContext) { + val (renderer, camera, map, _) = raycastContext + + val viewportWidth = renderer.viewportWidth + val viewportHeight = renderer.viewportHeight + val viewportHeightHalf = viewportHeight / 2 + + var raySweepAngle = camera.rotation - camera.halfFov + + for (rayIndex in 0 until viewportWidth) { + + var rayX = camera.xPos + var rayY = camera.yPos + + do { + rayX += kotlin.math.cos(toRadians(raySweepAngle)) / stepPrecision + rayY += kotlin.math.sin(toRadians(raySweepAngle)) / stepPrecision + // TODO bounds checking + val wallHit = map.data[kotlin.math.floor(rayY).toInt()][kotlin.math.floor(rayX).toInt()] > 0 + } while (!wallHit) + + val distanceToWall = kotlin.math.sqrt((camera.xPos - rayX).pow(2) + (camera.yPos - rayY).pow(2)) + val wallHeight = kotlin.math.floor(viewportHeightHalf / distanceToWall).toInt() + + // Sky + renderer.drawLine(rayIndex, 0, rayIndex, viewportHeightHalf - wallHeight, "cyan") + // Wall + renderer.drawLine(rayIndex, viewportHeightHalf - wallHeight, rayIndex, viewportHeightHalf + wallHeight, "red") + // Floor + renderer.drawLine(rayIndex, viewportHeightHalf + wallHeight, rayIndex, viewportHeight, "green") + + raySweepAngle += (camera.fov / viewportWidth.toDouble()) + } + } + + + +}
\ No newline at end of file |