gl01/internal/world/viewray.go

56 lines
1.4 KiB
Go
Raw Permalink Normal View History

2022-02-22 14:28:59 +08:00
package world
2022-02-24 20:20:33 +08:00
import (
"edgaru089.ink/go/gl01/internal/util/itype"
2022-02-24 20:20:33 +08:00
)
2022-02-22 14:28:59 +08:00
2022-02-24 20:20:33 +08:00
func (w *World) castworker(from, dir itype.Vec3d, maxlen float64, current itype.Vec3i, skipdir itype.Direction) (ok bool, blockcoord itype.Vec3i, face itype.Direction, where itype.Vec3d, dist float64) {
2022-02-22 14:28:59 +08:00
2022-02-24 20:20:33 +08:00
// Length test
if current.ToFloat64().Addv(0.5, 0.5, 0.5).Add(from.Negative()).Length() > maxlen+1 {
return
}
2022-02-22 14:28:59 +08:00
2022-02-24 20:20:33 +08:00
// Loose intersect test
if ok, _, _, _ = (itype.Boxd{
OffX: float64(current[0]),
OffY: float64(current[1]),
OffZ: float64(current[2]),
SizeX: 1, SizeY: 1, SizeZ: 1,
}).IntersectRay(from, dir, maxlen); !ok {
return
}
// Go through the bounding boxes
ba := w.Block(current).Appearance(current)
if !ba.NotSolid {
for _, b := range ba.Lookbox {
if ok, face, where, dist = b.Offset(current.ToFloat64()).IntersectRay(from, dir, maxlen); ok {
blockcoord = current
return
2022-02-22 14:28:59 +08:00
}
}
}
2022-02-24 20:20:33 +08:00
// Test the directions
for i := itype.Direction(0); i < 6; i++ {
if i == skipdir {
continue
}
ok, blockcoord, face, where, dist = w.castworker(from, dir, maxlen, current.Add(itype.DirectionVeci[i]), i.Opposite())
if ok {
return
}
}
2022-02-22 14:28:59 +08:00
ok = false
return
}
2022-02-24 20:20:33 +08:00
// CastViewRay
func (w *World) CastViewRay(from, dir itype.Vec3d, maxlen float64) (ok bool, blockcoord itype.Vec3i, face itype.Direction, where itype.Vec3d, dist float64) {
return w.castworker(from, dir, maxlen, from.Floor(), -1)
}