gl01/internal/world/viewray.go
2022-02-24 20:20:33 +08:00

56 lines
1.4 KiB
Go

package world
import (
"edgaru089.ml/go/gl01/internal/util/itype"
)
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) {
// Length test
if current.ToFloat64().Addv(0.5, 0.5, 0.5).Add(from.Negative()).Length() > maxlen+1 {
return
}
// 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
}
}
}
// 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
}
}
ok = false
return
}
// 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)
}