diff options
| author | James Barnett <noreply@jamesbarnett.xyz> | 2020-12-30 23:13:40 +0000 |
|---|---|---|
| committer | James Barnett <noreply@jamesbarnett.xyz> | 2020-12-30 23:13:40 +0000 |
| commit | 3c3322ef1a7aca3517ff94f723004fb809dec6cd (patch) | |
| tree | fd195eeedbb03cea8b25795b5e3c8fcf1a10c05c /src/main/kotlin/Raycaster.kt | |
| parent | c05b68f786715b20d0a9aef6538141c4227642ae (diff) | |
| download | kotlin-raycaster-3c3322ef1a7aca3517ff94f723004fb809dec6cd.tar.xz kotlin-raycaster-3c3322ef1a7aca3517ff94f723004fb809dec6cd.zip | |
Add adjustable render options. Update UI
Diffstat (limited to 'src/main/kotlin/Raycaster.kt')
| -rw-r--r-- | src/main/kotlin/Raycaster.kt | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/main/kotlin/Raycaster.kt b/src/main/kotlin/Raycaster.kt index ff2cc24..04b4d6f 100644 --- a/src/main/kotlin/Raycaster.kt +++ b/src/main/kotlin/Raycaster.kt @@ -1,11 +1,16 @@ import kotlin.js.Date import kotlin.math.pow -class Raycaster(private val stepPrecision: Int) { +data class RaycastOptions( + var fixFisheye: Boolean, + var stepPrecision: Int +) + +class Raycaster { fun raycast(raycastContext: RaycastContext) { val raycastStartMs = Date().getTime() - val (renderer, textureManager, camera, map, _) = raycastContext + val (options, renderer, textureManager, camera, map, _) = raycastContext val viewportWidth = renderer.viewportWidth val viewportHeight = renderer.viewportHeight @@ -20,8 +25,8 @@ class Raycaster(private val stepPrecision: Int) { var objectTypeHit: Int do { - rayX += raySweepAngle.cosine() / stepPrecision - rayY += raySweepAngle.sine() / stepPrecision + rayX += raySweepAngle.cosine() / options.stepPrecision + rayY += raySweepAngle.sine() / options.stepPrecision // TODO bounds checking objectTypeHit = map.data[rayY.toFlooredInt()][rayX.toFlooredInt()] @@ -31,7 +36,9 @@ class Raycaster(private val stepPrecision: Int) { val textureXIndex = ((texture.width * (rayX + rayY)) % texture.width).toFlooredInt() var distanceToWall = kotlin.math.sqrt((camera.xPos - rayX).pow(2) + (camera.yPos - rayY).pow(2)) -// distanceToWall *= (raySweepAngle-camera.rotation).cosine() + if (options.fixFisheye) { + distanceToWall *= (raySweepAngle-camera.rotation).cosine() + } val wallHeight = viewportHeightHalf / distanceToWall // Ceiling |