Esempio n. 1
0
func (b *Box) inSolid(world mapgen.BlockSource) bool {
	blockCollide := func(x, y, z int) bool {
		blockCoord := coords.Block{x, y, z}
		if world.Block(blockCoord).Solid() {
			center := blockCoord.Center()
			blockBox := NewBox(center.Vec3(), vmath.Vec3{0.5, 0.5, 0.5})
			return b.Collides(blockBox)
		}
		return false
	}
	f := func(n float64) int {
		return int(math.Floor(n))
	}
	xs := f(b.xs)
	xe := f(b.xe)
	ys := f(b.ys)
	ye := f(b.ye)
	zs := f(b.zs)
	ze := f(b.ze)

	for x := xs; x <= xe; x++ {
		for y := ys; y <= ye; y++ {
			for z := zs; z <= ze; z++ {
				if blockCollide(x, y, z) {
					return true
				}
			}
		}
	}
	return false
}
Esempio n. 2
0
func (ray *Ray) FindAnyIntersect(blocks mapgen.BlockSource, boxes []*Box) (*vmath.Vec3, int) {
	pos := ray.pos
	for dist := 0.0; dist < RAY_MAX_DIST; dist += RAY_PRECISION {
		pos.Translate(&ray.dir, RAY_PRECISION)

		for i, v := range boxes {
			if v == nil {
				continue
			}
			if !v.Contains(pos) {
				continue
			}
			return &pos, i
		}

		wc := coords.World(pos)
		if blocks.Block(wc.Block()).Solid() {
			return &pos, -1
		}
	}
	return nil, -1
}