Fixed the wierd wall overlap

This commit is contained in:
Sasha Koshka 2023-02-21 17:57:52 -05:00
parent 20fa445cdd
commit ce1d938f7a
2 changed files with 17 additions and 9 deletions

View File

@ -120,6 +120,7 @@ func (ray *Ray) Cast (
func (ray *Ray) castH (world World, max int) (distance float64, hit Vector, wall int) { func (ray *Ray) castH (world World, max int) (distance float64, hit Vector, wall int) {
var position Vector var position Vector
var delta Vector var delta Vector
var offset Vector
ray.Angle = math.Mod(ray.Angle, math.Pi * 2) ray.Angle = math.Mod(ray.Angle, math.Pi * 2)
if ray.Angle < 0 { if ray.Angle < 0 {
ray.Angle += math.Pi * 2 ray.Angle += math.Pi * 2
@ -127,8 +128,9 @@ func (ray *Ray) castH (world World, max int) (distance float64, hit Vector, wall
tan := math.Tan(math.Pi - ray.Angle) tan := math.Tan(math.Pi - ray.Angle)
if ray.Angle > math.Pi { if ray.Angle > math.Pi {
// facing up // facing up
position.Y = math.Floor(ray.Y) - (1.0 / 64) position.Y = math.Floor(ray.Y)
delta.Y = -1 delta.Y = -1
offset.Y = -1
} else if ray.Angle < math.Pi { } else if ray.Angle < math.Pi {
// facing down // facing down
position.Y = math.Floor(ray.Y) + 1 position.Y = math.Floor(ray.Y) + 1
@ -143,24 +145,28 @@ func (ray *Ray) castH (world World, max int) (distance float64, hit Vector, wall
// cast da ray // cast da ray
steps := 0 steps := 0
for { for {
cell := world.At(position.Point()) cell := world.At(position.Add(offset).Point())
if cell > 0 || steps > max { break } if cell > 0 || steps > max { break }
position = position.Add(delta) position = position.Add(delta)
steps ++ steps ++
} }
return position.Sub(ray.Vector).Hypot(), position, world.At(position.Point()) return position.Sub(ray.Vector).Hypot(),
position,
world.At(position.Add(offset).Point())
} }
func (ray *Ray) castV (world World, max int) (distance float64, hit Vector, wall int) { func (ray *Ray) castV (world World, max int) (distance float64, hit Vector, wall int) {
var position Vector var position Vector
var delta Vector var delta Vector
var offset Vector
tan := math.Tan(math.Pi - ray.Angle) tan := math.Tan(math.Pi - ray.Angle)
offsetAngle := math.Mod(ray.Angle + math.Pi / 2, math.Pi * 2) offsetAngle := math.Mod(ray.Angle + math.Pi / 2, math.Pi * 2)
if offsetAngle > math.Pi { if offsetAngle > math.Pi {
// facing left // facing left
position.X = math.Floor(ray.X) - (1.0 / 64) position.X = math.Floor(ray.X)
delta.X = -1 delta.X = -1
offset.X = -1
} else if offsetAngle < math.Pi { } else if offsetAngle < math.Pi {
// facing right // facing right
position.X = math.Floor(ray.X) + 1 position.X = math.Floor(ray.X) + 1
@ -175,11 +181,13 @@ func (ray *Ray) castV (world World, max int) (distance float64, hit Vector, wall
// cast da ray // cast da ray
steps := 0 steps := 0
for { for {
cell := world.At(position.Point()) cell := world.At(position.Add(offset).Point())
if cell > 0 || steps > max { break } if cell > 0 || steps > max { break }
position = position.Add(delta) position = position.Add(delta)
steps ++ steps ++
} }
return position.Sub(ray.Vector).Hypot(), position, world.At(position.Point()) return position.Sub(ray.Vector).Hypot(),
position,
world.At(position.Add(offset).Point())
} }

View File

@ -130,7 +130,7 @@ func (element *Raycaster) drawAll () {
shade := 1.0 shade := 1.0
if horizontal { if horizontal {
shade *= 0.7 shade *= 0.8
} }
shade *= 1 - distance / float64(element.renderDistance) shade *= 1 - distance / float64(element.renderDistance)
if shade < 0 { shade = 0 } if shade < 0 { shade = 0 }