Example #1
0
// IntersectAxis checks whether there's interaction between the ray and the box.
func (b *BoundingBox) IntersectAxis(ray *ray.Ray, axis int) bool {
	directions := [3]float64{ray.Direction.X, ray.Direction.Y, ray.Direction.Z}
	start := [3]float64{ray.Start.X, ray.Start.Y, ray.Start.Z}

	// if the ray isn't pointing at the box there wouldn't be an intersection
	if (directions[axis] > 0 && start[axis] > b.MaxVolume[axis]) ||
		(directions[axis] < 0 && start[axis] < b.MinVolume[axis]) {
		return false
	}
	// or if the ray isn't moving in this direction at all
	if math.Abs(directions[axis]) < maths.Epsilon {
		return false
	}
	// we take the other two axes
	otherAxis1, otherAxis2 := otherAxes(axis)

	multiplier := ray.Inverse[axis]
	var intersectionX, intersectionY float64

	distance := (b.MinVolume[axis] - start[axis]) * multiplier
	if distance < 0 {
		return false
	}

	intersectionX = start[otherAxis1] + directions[otherAxis1]*distance
	if maths.Between(b.MinVolume[otherAxis1], b.MaxVolume[otherAxis1], intersectionX) {
		intersectionY = start[otherAxis2] + directions[otherAxis2]*distance
		if maths.Between(b.MinVolume[otherAxis2], b.MaxVolume[otherAxis2], intersectionY) {
			return true
		}
	}

	distance = (b.MaxVolume[axis] - start[axis]) * multiplier
	if distance < 0 {
		return false
	}
	intersectionX = start[otherAxis1] + directions[otherAxis1]*distance
	if maths.Between(b.MinVolume[otherAxis1], b.MaxVolume[otherAxis1], intersectionX) {
		intersectionY = start[otherAxis2] + directions[otherAxis2]*distance
		if maths.Between(b.MinVolume[otherAxis2], b.MaxVolume[otherAxis2], intersectionY) {
			return true
		}
	}
	return false
}
Example #2
0
// Inside checks if a point is inside the box
func (b *BoundingBox) Inside(point *maths.Vec3) bool {
	return (maths.Between(b.MinVolume[0], b.MaxVolume[0], point.X) &&
		maths.Between(b.MinVolume[1], b.MaxVolume[1], point.Y) &&
		maths.Between(b.MinVolume[2], b.MaxVolume[2], point.Z))
}