Skip to content

Commit

Permalink
Adding raytesting for physics.
Browse files Browse the repository at this point in the history
Rays collide with IBoundingObjects. Cameras can also ray test.
Material.String() and Animation.String() now print readably.
IBoundingObject now implements iNode as well.
collisionPlanes are now non-pointer referenced.
Broadphase.TrianglesFromBounding() now returns a pre-populated set of all triangles if the grid size is less than or equal to 1.
Camera Projection matrix is now cached when possible; camera alteration is now done using functions (SetPerspective(), SetFieldOfView(), etc).
Altering Matrix4.Inverted() to use a faster system from stackoverflow.
Adding NewCylinderMesh() function to make cylinder mesh.
Adding Camera.ScreenToWorld().
  • Loading branch information
SolarLune committed Feb 1, 2023
1 parent 97779a6 commit 259b85e
Show file tree
Hide file tree
Showing 27 changed files with 2,284 additions and 209 deletions.
9 changes: 9 additions & 0 deletions animation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tetra3d

import (
"fmt"
"log"
"math"
"time"
Expand Down Expand Up @@ -213,6 +214,14 @@ func (animation *Animation) Library() *Library {
return animation.library
}

func (animation *Animation) String() string {
if ReadableReferences {
return "<" + animation.Name + ">"
} else {
return fmt.Sprintf("%p", animation)
}
}

// AnimationValues indicate the current position, scale, and rotation for a Node.
type AnimationValues struct {
Position Vector
Expand Down
1 change: 1 addition & 0 deletions bounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ type CollisionTestSettings struct {
// concerning whether an object that implements IBoundingObject is colliding with another IBoundingObject, and
// if so, by how much.
type IBoundingObject interface {
INode
// Colliding returns true if the BoundingObject is intersecting the other BoundingObject.
Colliding(other IBoundingObject) bool
// Collision returns a Collision if the BoundingObject is intersecting another BoundingObject. If
Expand Down
25 changes: 23 additions & 2 deletions boundsTriangles.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ type collisionPlane struct {
Distance float64
}

func newCollisionPlane() *collisionPlane {
return &collisionPlane{}
func newCollisionPlane() collisionPlane {
return collisionPlane{}
}

func (plane *collisionPlane) Set(v0, v1, v2 Vector) {
Expand All @@ -170,6 +170,27 @@ func (plane *collisionPlane) ClosestPoint(point Vector) Vector {

}

func (plane *collisionPlane) RayAgainstPlane(from, to Vector) (Vector, bool) {

dir := to.Sub(from).Unit()

nd := dir.Dot(plane.Normal)
pn := from.Dot(plane.Normal)

if nd >= 0 {
return Vector{}, false
}

t := (plane.Distance - pn) / nd

if t >= 0 {
return from.Add(dir.Scale(t)), true
}

return Vector{}, false

}

var colPlane = newCollisionPlane()

func closestPointOnTri(point, v0, v1, v2 Vector) Vector {
Expand Down
12 changes: 6 additions & 6 deletions broadphase.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Broadphase struct {
Center *Node
aabb *BoundingAABB
TriSets [][][][]uint16
allTriSet map[uint16]bool
BoundingTriangles *BoundingTriangles
}

Expand Down Expand Up @@ -57,6 +58,7 @@ func (bp *Broadphase) Clone() *Broadphase {
}

newBroadphase.TriSets = ts
newBroadphase.allTriSet = bp.allTriSet

return newBroadphase
}
Expand All @@ -79,6 +81,7 @@ func (bp *Broadphase) Resize(gridSize int) {
bp.aabb.updateSize()

bp.TriSets = make([][][][]uint16, gridSize)
bp.allTriSet = make(map[uint16]bool)

hg := float64(bp.GridSize) / 2

Expand Down Expand Up @@ -108,6 +111,7 @@ func (bp *Broadphase) Resize(gridSize int) {
if bp.aabb.PointInside(closestOnTri) {
bp.TriSets[i][j][k] = append(bp.TriSets[i][j][k], tri.ID)
}
bp.allTriSet[tri.ID] = true

}

Expand All @@ -124,12 +128,8 @@ func (bp *Broadphase) Resize(gridSize int) {
// once, of course.
func (bp *Broadphase) TrianglesFromBounding(boundingObject IBoundingObject) map[uint16]bool {

if bp.GridSize <= 0 {
trianglesSet := make(map[uint16]bool, len(bp.BoundingTriangles.Mesh.Triangles))
for _, tri := range bp.BoundingTriangles.Mesh.Triangles {
trianglesSet[tri.ID] = true
}
return trianglesSet
if bp.GridSize <= 1 {
return bp.allTriSet
}

trianglesSet := make(map[uint16]bool, len(bp.TriSets))
Expand Down
Loading

0 comments on commit 259b85e

Please sign in to comment.