Skip to content

package collision

import "kaiju/engine/collision"

Functions

AddSubBVH

func AddSubBVH(world **BVH, sub *BVH, transform *matrix.Transform)

InsertBVH

func InsertBVH(root **BVH, hitCheck HitObject, transform *matrix.Transform, data any)

PointOutsideOfPlane

func PointOutsideOfPlane(p, a, b, c, d matrix.Vec3) bool

PointOutsideOfPlane returns true if the given point is outside of the plane

RemoveAllLeavesMatchingTransform

func RemoveAllLeavesMatchingTransform(world **BVH, transform *matrix.Transform)

RemoveSubBVH

func RemoveSubBVH(world **BVH, sub *BVH)

Types

AABB

struct

type AABB struct {
    Center matrix.Vec3
    Extent matrix.Vec3
}

AABB is an axis-aligned bounding box

AABBFromMinMax

func AABBFromMinMax(min, max matrix.Vec3) AABB

AABBFromMinMax creates an AABB from the minimum and maximum points

AABBFromTransform

func AABBFromTransform(transform *matrix.Transform) AABB

AABBFromWidth

func AABBFromWidth(center matrix.Vec3, halfWidth matrix.Float) AABB

AABBFromWidth creates an AABB from the center and half-width

AABBUnion

func AABBUnion(a, b AABB) AABB

Union returns the union of two AABBs

AABB.AABBIntersect

func (a *AABB) AABBIntersect(b AABB) bool

AABBIntersect returns whether the AABB intersects another AABB

AABB.Bounds

func (box AABB) Bounds() AABB

////////////////////////////////////////////////////////////////////////////// Satisfying BVH HitObject interface //////////////////////////////////////////////////////////////////////////////

AABB.ClosestDistance

func (a AABB) ClosestDistance(b AABB) matrix.Float

ClosestDistance returns the closest distance between two AABBs

AABB.Contains

func (box *AABB) Contains(point matrix.Vec3) bool

Contains returns whether the AABB contains the point

AABB.ContainsAABB

func (box *AABB) ContainsAABB(b AABB) bool

ContainsAABB returns whether the AABB contains another AABB

AABB.Corners

func (box AABB) Corners() [8]matrix.Vec3

AABB.FromTriangle

func (box *AABB) FromTriangle(triangle DetailedTriangle) AABB

FromTriangle returns an AABB that contains the triangle

AABB.InFrustum

func (box *AABB) InFrustum(frustum Frustum) bool

InFrustum returns whether the AABB is in the frustum

AABB.LongestAxis

func (box *AABB) LongestAxis() int

LongestAxis returns the longest axis of the AABB (0 = X, 1 = Y, 2 = Z)

AABB.Max

func (box *AABB) Max() matrix.Vec3

Max returns the maximum point of the AABB

AABB.Min

func (box *AABB) Min() matrix.Vec3

Min returns the minimum point of the AABB

AABB.PlaneIntersect

func (box *AABB) PlaneIntersect(plane Plane) bool

PlaneIntersect returns whether the AABB intersects a plane

AABB.RayHit

func (box *AABB) RayHit(ray Ray) (matrix.Vec3, bool)

RayHit returns the point of intersection and whether the ray hit the AABB

AABB.RayIntersectTest

func (box AABB) RayIntersectTest(ray Ray, length float32, transform *matrix.Transform) bool

AABB.Size

func (box AABB) Size() matrix.Vec3

Size returns the size of the AABB

AABB.SurfaceArea

func (box AABB) SurfaceArea() matrix.Float

AABB.TriangleIntersect

func (box *AABB) TriangleIntersect(tri DetailedTriangle) bool

TriangleIntersect returns whether the AABB intersects a triangle

BVH

struct

type BVH struct {
    Left   *BVH
    Right  *BVH
    Parent *BVH
    Item   BVHItem
    // Has unexported fields.
}

CloneBVH

func CloneBVH(bvh *BVH) *BVH

NewBVH

func NewBVH(entries []HitObject, transform *matrix.Transform, data any) *BVH

BVH.Bounds

func (b *BVH) Bounds() AABB

BVH.IsLeaf

func (b *BVH) IsLeaf() bool

BVH.RayIntersect

func (b *BVH) RayIntersect(ray Ray, length float32) (any, bool)

BVH.RayIntersectTest

func (b *BVH) RayIntersectTest(ray Ray, length float32, transform *matrix.Transform) bool

BVH.Refit

func (b *BVH) Refit()

BVHItem

struct

type BVHItem struct {
    Transform *matrix.Transform
    HitCheck  HitObject
    Data      any
}

BVHItem.Bounds

func (item BVHItem) Bounds() AABB

BVHItem.IsValid

func (item BVHItem) IsValid() bool

BVHItem.RayIntersect

func (item BVHItem) RayIntersect(ray Ray, length float32, transform *matrix.Transform) bool

DetailedTriangle

struct

type DetailedTriangle struct {
    Points   [3]matrix.Vec3
    Normal   matrix.Vec3
    Centroid matrix.Vec3
    Radius   matrix.Float
}

DetailedTriangleFromPoints

func DetailedTriangleFromPoints(points [3]matrix.Vec3) DetailedTriangle

DetailedTriangleFromPoints creates a detailed triangle from three points, a detailed triangle is different from a regular triangle in that it contains additional information such as the centroid and radius

DetailedTriangle.Bounds

func (t DetailedTriangle) Bounds() AABB

DetailedTriangle.RayIntersectTest

func (t DetailedTriangle) RayIntersectTest(ray Ray, length float32, transform *matrix.Transform) bool

Frustum

struct

type Frustum struct {
    Planes [6]Plane
}

HitObject

interface

type HitObject interface {
    Bounds() AABB
    RayIntersectTest(ray Ray, length float32, transform *matrix.Transform) bool
}

OOBB

struct

type OOBB struct {
    Center      matrix.Vec3
    Extent      matrix.Vec3
    Orientation matrix.Mat3
}

OBBFromAABB

func OBBFromAABB(aabb AABB) OOBB

OBBFromTransform

func OBBFromTransform(baseAABB AABB, transform *matrix.Transform) OOBB

OOBB.Bounds

func (o OOBB) Bounds() AABB

OOBB.ContainsPoint

func (o OOBB) ContainsPoint(point matrix.Vec3) bool

OOBB.Corners

func (o OOBB) Corners() [8]matrix.Vec3

OOBB.Intersect

func (o OOBB) Intersect(other OOBB) bool

OOBB.RayIntersect

func (o OOBB) RayIntersect(ray Ray, length float32) bool

Octree

struct

type Octree struct {
    Center    matrix.Vec3
    HalfWidth matrix.Float
    Children  [8]*Octree
    Objects   []HitObject
}

NewOctree

func NewOctree(center matrix.Vec3, halfWidth matrix.Float, maxDepth int) *Octree

OctreeForMesh

func OctreeForMesh(mesh []matrix.Vec3) *Octree

Octree.AsAABB

func (o *Octree) AsAABB() AABB

Octree.Insert

func (node *Octree) Insert(obj HitObject)

Plane

struct

type Plane struct {
    Normal matrix.Vec3
    Dot    matrix.Float
}

PlaneCCW

func PlaneCCW(a, b, c matrix.Vec3) Plane

PlaneCCW creates a plane from three points in counter clockwise order

Plane.ClosestPoint

func (p Plane) ClosestPoint(point matrix.Vec3) matrix.Vec3

ClosestPoint returns the closest point on the plane to the given point

Plane.Distance

func (p Plane) Distance(point matrix.Vec3) float32

Distance returns the distance from the plane to the given point

Plane.SetFloatValue

func (p *Plane) SetFloatValue(value float32, index int)

SetFloatValue sets the value of the plane at the given index (X, Y, Z, Dot)

Plane.ToArray

func (p Plane) ToArray() [4]float32

ToArray converts the plane to an array of 4 floats

Plane.ToVec4

func (p Plane) ToVec4() matrix.Vec4

ToVec4 converts the plane to a Vec4 (analogous to ToArray)

Ray

struct

type Ray struct {
    Origin    matrix.Vec3
    Direction matrix.Vec3
}

Ray.PlaneHit

func (r Ray) PlaneHit(planePosition, planeNormal matrix.Vec3) (hit matrix.Vec3, success bool)

PlaneHit returns the point of intersection with the plane and true if the ray hits the plane

Ray.Point

func (r Ray) Point(distance float32) matrix.Vec3

Point returns the point at the given distance along the ray

Ray.SphereHit

func (r Ray) SphereHit(center matrix.Vec3, radius, maxLen float32) bool

SphereHit returns true if the ray hits the sphere

Ray.TriangleHit

func (r Ray) TriangleHit(rayLen float32, a, b, c matrix.Vec3) bool

TriangleHit returns true if the ray hits the triangle defined by the three points

Segment

struct

type Segment struct {
    A matrix.Vec3
    B matrix.Vec3
}

LineSegmentFromRay

func LineSegmentFromRay(ray Ray, length float32) Segment

LineSegmentFromRay creates a line segment from a ray

Segment.TriangleHit

func (l Segment) TriangleHit(a, b, c matrix.Vec3) bool

TriangleHit returns true if the segment hits the triangle defined by the three points

Triangle

struct

type Triangle struct {
    P           Plane
    EdgePlaneBC Plane
    EdgePlaneCA Plane
}