Skip to content

Commit

Permalink
Adding some comment docs
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentFarris committed Mar 8, 2024
1 parent 37f79a1 commit e929f27
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/collision/plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Plane struct {
Dot matrix.Float
}

// PlaneCCW creates a plane from three points in counter clockwise order
func PlaneCCW(a, b, c matrix.Vec3) Plane {
var p Plane
e0 := b.Subtract(a)
Expand All @@ -53,6 +54,7 @@ func PlaneCCW(a, b, c matrix.Vec3) Plane {
return p
}

// SetFloatValue sets the value of the plane at the given index (X, Y, Z, Dot)
func (p *Plane) SetFloatValue(value float32, index int) {
switch index {
case 0:
Expand All @@ -66,27 +68,32 @@ func (p *Plane) SetFloatValue(value float32, index int) {
}
}

// ToArray converts the plane to an array of 4 floats
func (p Plane) ToArray() [4]float32 {
return [4]float32{p.Normal.X(), p.Normal.Y(), p.Normal.Z(), p.Dot}
}

// ToVec4 converts the plane to a Vec4 (analogous to ToArray)
func (p Plane) ToVec4() matrix.Vec4 {
return matrix.Vec4{p.Normal.X(), p.Normal.Y(), p.Normal.Z(), p.Dot}
}

// ClosestPoint returns the closest point on the plane to the given point
func (p Plane) ClosestPoint(point matrix.Vec3) matrix.Vec3 {
// If normalized, t := matrix.Vec3Dot(point, p.Normal) - p.Dot
t := (matrix.Vec3Dot(p.Normal, point) - p.Dot) / matrix.Vec3Dot(p.Normal, p.Normal)
return point.Subtract(p.Normal.Scale(t))
}

// Distance returns the distance from the plane to the given point
func (p Plane) Distance(point matrix.Vec3) float32 {
// If normalized, return matrix.Vec3Dot(p.Normal, point) - p.Dot
return (matrix.Vec3Dot(p.Normal, point) - p.Dot) / matrix.Vec3Dot(p.Normal, p.Normal)
}

// PointOutsideOfPlane returns true if the given point is outside of the plane
func PointOutsideOfPlane(p, a, b, c, d matrix.Vec3) bool {
signp := matrix.Vec3Dot(p.Subtract(a), matrix.Vec3Cross(b.Subtract(a), c.Subtract(a)))
signd := matrix.Vec3Dot(d.Subtract(a), matrix.Vec3Cross(b.Subtract(a), c.Subtract(a)))
return signp*signd < 0
signP := matrix.Vec3Dot(p.Subtract(a), matrix.Vec3Cross(b.Subtract(a), c.Subtract(a)))
signD := matrix.Vec3Dot(d.Subtract(a), matrix.Vec3Cross(b.Subtract(a), c.Subtract(a)))
return signP*signD < 0
}
4 changes: 4 additions & 0 deletions src/collision/ray.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ type Ray struct {
Direction matrix.Vec3
}

// Point returns the point at the given distance along the ray
func (r Ray) Point(distance float32) matrix.Vec3 {
return r.Origin.Add(r.Direction.Scale(distance))
}

// TriangleHit returns true if the ray hits the triangle defined by the three points
func (r Ray) TriangleHit(rayLen float32, a, b, c matrix.Vec3) bool {
s := Segment{r.Origin, r.Point(rayLen)}
return s.TriangleHit(a, b, c)
}

// PlaneHit returns the point of intersection with the plane and true if the ray hits the plane
func (r Ray) PlaneHit(planePosition, planeNormal matrix.Vec3) (hit matrix.Vec3, success bool) {
hit = matrix.Vec3{}
success = false
Expand All @@ -70,6 +73,7 @@ func (r Ray) PlaneHit(planePosition, planeNormal matrix.Vec3) (hit matrix.Vec3,
return r.Point(distance), true
}

// SphereHit returns true if the ray hits the sphere
func (r Ray) SphereHit(center matrix.Vec3, radius, maxLen float32) bool {
delta := center.Subtract(r.Origin)
lenght := matrix.Vec3Dot(r.Direction, delta)
Expand Down
2 changes: 2 additions & 0 deletions src/collision/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ type Segment struct {
B matrix.Vec3
}

// LineSegmentFromRay creates a line segment from a ray
func LineSegmentFromRay(ray Ray, length float32) Segment {
return Segment{ray.Origin, ray.Point(length)}
}

// TriangleHit returns true if the segment hits the triangle defined by the three points
func (l Segment) TriangleHit(a, b, c matrix.Vec3) bool {
p := l.A
q := l.B
Expand Down
3 changes: 3 additions & 0 deletions src/collision/triangle.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type DetailedTriangle struct {
Radius matrix.Float
}

// 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
func DetailedTriangleFromPoints(points [3]matrix.Vec3) DetailedTriangle {
tri := DetailedTriangle{
Points: [3]matrix.Vec3{points[0], points[1], points[2]},
Expand Down

0 comments on commit e929f27

Please sign in to comment.