| 1 |
//go:build !tinygo |
| 2 |
// +build !tinygo |
| 3 |
|
| 4 |
package gfx |
| 5 |
|
| 6 |
// Box is a 3D cuboid with a min and max Vec3 |
| 7 |
type Box struct { |
| 8 |
Min Vec3 |
| 9 |
Max Vec3 |
| 10 |
} |
| 11 |
|
| 12 |
// NewBox creates a new Box. |
| 13 |
func NewBox(min, max Vec3) Box { |
| 14 |
return Box{ |
| 15 |
Min: min, |
| 16 |
Max: max, |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
// B returns a new Box with given the Min and Max coordinates. |
| 21 |
func B(minX, minY, minZ, maxX, maxY, maxZ float64) Box { |
| 22 |
return NewBox( |
| 23 |
V3(minX, minY, minZ), |
| 24 |
V3(maxX, maxY, maxZ), |
| 25 |
) |
| 26 |
} |
| 27 |
|
| 28 |
// Overlaps checks if two boxes overlap or not. |
| 29 |
func (b Box) Overlaps(a Box) bool { |
| 30 |
return (!(b.Min.X >= a.Max.X || a.Min.X >= b.Max.X) && |
| 31 |
!(b.Min.Y >= a.Max.Y || a.Min.Y >= b.Max.Y) && |
| 32 |
!(b.Min.Z >= a.Max.Z || a.Min.Z >= b.Max.Z)) |
| 33 |
} |
| 34 |
|
| 35 |
// Behind checks if b is in front of the a box. |
| 36 |
func (b Box) Behind(a Box) bool { |
| 37 |
// Test for intersection in X-axis (lower X value is in front) |
| 38 |
if b.Min.X >= a.Max.X { |
| 39 |
return true |
| 40 |
} else if a.Min.X >= b.Max.X { |
| 41 |
return false |
| 42 |
} |
| 43 |
|
| 44 |
// Test for intersection in Y-axis (lower Y value is in front) |
| 45 |
if b.Min.Y >= a.Max.Y { |
| 46 |
return true |
| 47 |
} else if a.Min.Y >= b.Max.Y { |
| 48 |
return false |
| 49 |
} |
| 50 |
|
| 51 |
// Test for intersection in Z-axis (higher Z value is in front) |
| 52 |
if b.Min.Z >= a.Max.Z { |
| 53 |
return false |
| 54 |
} else if a.Min.Z >= b.Max.Z { |
| 55 |
return true |
| 56 |
} |
| 57 |
|
| 58 |
return true |
| 59 |
} |