-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbox_seg_overlap.go
More file actions
56 lines (45 loc) · 1.32 KB
/
box_seg_overlap.go
File metadata and controls
56 lines (45 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package coll
import (
"math"
"github.com/setanarut/v"
)
// BoxSegmentOverlap returns true if they intersect, false otherwise.
//
// Params:
//
// - box - Bounding box to check
// - start - Ray segment origin/start position
// - delta - Ray segment move/displacement vector
// - padding - Padding added to the radius of the bounding box
// - h - Contact info for segment. Filled when argument isn't nil and a collision occurs
func BoxSegmentOverlap(box *AABB, start, delta, padding v.Vec, h *Hit) bool {
scale := v.One.Div(delta)
signVec := v.Vec{X: math.Copysign(1, scale.X), Y: math.Copysign(1, scale.Y)}
signedExtent := box.Half.Add(padding).Mul(signVec)
nearTimes := box.Pos.Sub(signedExtent).Sub(start).Mul(scale)
farTimes := box.Pos.Add(signedExtent).Sub(start).Mul(scale)
if math.IsNaN(nearTimes.Y) {
nearTimes.Y = math.Inf(1)
}
if math.IsNaN(farTimes.Y) {
farTimes.Y = math.Inf(1)
}
if nearTimes.X > farTimes.Y || nearTimes.Y > farTimes.X {
return false
}
nearTime := max(nearTimes.X, nearTimes.Y)
farTime := min(farTimes.X, farTimes.Y)
if nearTime >= 1 || farTime <= 0 {
return false
}
if h == nil {
return true
}
h.Data = max(0, min(1, nearTime))
if nearTimes.X > nearTimes.Y {
h.Normal = v.Vec{X: -signVec.X, Y: 0}
} else {
h.Normal = v.Vec{X: 0, Y: -signVec.Y}
}
return true
}