Skip to content

Commit

Permalink
fix #33 NaN prevention not working
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecoffman committed Feb 17, 2024
1 parent e672e20 commit a95d2e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 3 additions & 3 deletions collision.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (v0 MinkowskiPoint) ClosestPoints(v1 MinkowskiPoint) ClosestPoints {

// Vertex/vertex collisions need special treatment since the MSA won't be shared with an axis of the minkowski difference.
d2 := p.Length()
n2 := p.Mult(1 / (d2 + math.SmallestNonzeroFloat64))
n2 := p.Mult(1 / (d2 + 1e-15))

return ClosestPoints{pa, pb, n2, d2, id}
}
Expand Down Expand Up @@ -325,8 +325,8 @@ func ContactPoints(e1, e2 Edge, points ClosestPoints, info *CollisionInfo) {
dE2B := e2.b.p.Cross(n)

// TODO + min isn't a complete fix
e1Denom := 1 / (dE1B - dE1A + math.SmallestNonzeroFloat64)
e2Denom := 1 / (dE2B - dE2A + math.SmallestNonzeroFloat64)
e1Denom := 1 / (dE1B - dE1A + 1e-15)
e2Denom := 1 / (dE2B - dE2A + 1e-15)

// Project the endpoints of the two edges onto the opposing edge, clamping them as necessary.
// Compare the projected points to the collision normal to see if the shapes overlap there.
Expand Down
2 changes: 1 addition & 1 deletion vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (v Vector) Lerp(other Vector, t float64) Vector {
}

func (v Vector) Normalize() Vector {
return v.Mult(1.0 / (v.Length() + math.SmallestNonzeroFloat64))
return v.Mult(1.0 / (v.Length() + 1e-15))
}

func (v Vector) SLerp(other Vector, t float64) Vector {
Expand Down
13 changes: 13 additions & 0 deletions vector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cp

import (
"testing"
)

func TestVector_Normalize(t *testing.T) {
v := Vector{}
u := v.Normalize()
if u.X != 0.0 || u.Y != 0.0 {
t.Errorf("Expected zero vector, got %v", u)
}
}

0 comments on commit a95d2e2

Please sign in to comment.