gl01/internal/world/viewray.go

49 lines
1.1 KiB
Go
Raw Normal View History

2022-02-22 14:28:59 +08:00
package world
import "edgaru089.ml/go/gl01/internal/util/itype"
// CastViewRay
func (w *World) CastViewRay(from, dir itype.Vec3d, maxlen float64) (ok bool, blockcoord itype.Vec3i, face itype.Direction, where itype.Vec3d, dist float64) {
bfrom := from.Floor()
bfromdir := itype.Direction(-1)
for bfrom.ToFloat64().Addv(0.5, 0.5, 0.5).Add(from.Negative()).Length() < maxlen {
for todir := itype.Direction(0); todir < 6; todir++ {
if todir == bfromdir {
continue
}
bto := bfrom.Add(itype.DirectionVeci[todir])
block := w.Block(bto)
if block.Id != 0 {
outbox := itype.Boxd{
OffX: float64(bto[0]),
OffY: float64(bto[1]),
OffZ: float64(bto[2]),
SizeX: 1,
SizeY: 1,
SizeZ: 1,
}
var outface itype.Direction
if ok, outface, _, _ = outbox.IntersectRay(from, dir, maxlen); ok {
app := block.Appearance(bto)
for _, lb := range app.Lookbox {
if ok, face, where, dist = lb.IntersectRay(from, dir, maxlen); ok {
blockcoord = bto
return
}
}
}
bfromdir = outface.Opposite()
bfrom = bto
break
}
}
}
ok = false
return
}