Skip to content

Commit a95d2e2

Browse files
committed
fix #33 NaN prevention not working
1 parent e672e20 commit a95d2e2

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

collision.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func (v0 MinkowskiPoint) ClosestPoints(v1 MinkowskiPoint) ClosestPoints {
245245

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

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

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

331331
// Project the endpoints of the two edges onto the opposing edge, clamping them as necessary.
332332
// Compare the projected points to the collision normal to see if the shapes overlap there.

vector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (v Vector) Lerp(other Vector, t float64) Vector {
8686
}
8787

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

9292
func (v Vector) SLerp(other Vector, t float64) Vector {

vector_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cp
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestVector_Normalize(t *testing.T) {
8+
v := Vector{}
9+
u := v.Normalize()
10+
if u.X != 0.0 || u.Y != 0.0 {
11+
t.Errorf("Expected zero vector, got %v", u)
12+
}
13+
}

0 commit comments

Comments
 (0)