-
Notifications
You must be signed in to change notification settings - Fork 0
/
ray.go
95 lines (78 loc) · 1.95 KB
/
ray.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package math
type Ray struct {
origin Vector3
direction Vector3
}
func NewDefaultRay() *Ray {
return &Ray {
origin: *NewDefaultVector3(),
direction: *NewDefaultVector3(),
}
}
func NewRay(origin *Vector3, direction *Vector3) *Ray {
return &Ray {
origin: *origin.Clone(),
direction: *direction.Clone(),
}
}
func (ray *Ray) Set(origin *Vector3, direction *Vector3) {
ray.origin.Copy(origin)
ray.direction.Copy(direction)
}
func (ray *Ray) Copy(source *Ray) {
ray.origin.Copy(&source.origin)
ray.direction.Copy(&source.direction)
}
func (ray *Ray) Clone() *Ray {
nr := NewDefaultRay()
nr.Copy(ray)
return nr
}
func (ray *Ray) GetAt(t float32) *Vector3 {
target := NewDefaultVector3()
target.Copy(&ray.direction)
target.MultiplyScalar(t)
target.Add(&ray.origin)
return target
}
func (ray *Ray) SetLookAt(v *Vector3) {
ray.direction.Copy(v)
ray.direction.Sub(&ray.origin)
ray.direction.Normalize()
}
func (ray *Ray) SetRecast(t float32) {
target := ray.GetAt(t)
ray.origin.Copy(target)
}
func (ray *Ray) GetClosestPointToPoint(point *Vector3) *Vector3 {
target := NewDefaultVector3()
target.SetSubVectors(point, &ray.origin)
directionDistance := target.Dot(&ray.direction)
if directionDistance < 0 {
target.Copy(&ray.origin)
} else {
target.Copy(&ray.direction)
target.MultiplyScalar(directionDistance)
target.Add(&ray.origin)
}
return target
}
func (ray *Ray) GetDistanceToPoint(point *Vector3) float32 {
return Sqrt(ray.GetDistanceSqToPoint(point))
}
func (ray *Ray) GetDistanceSqToPoint(point *Vector3) float32 {
v1 := NewDefaultVector3()
v1.SetSubVectors(point, &ray.origin)
directionDistance := v1.Dot(&ray.direction)
if directionDistance < 0 {
return ray.origin.GetDistanceToSquared(point)
} else {
v1.Copy(&ray.direction)
v1.MultiplyScalar(directionDistance)
v1.Add(&ray.origin)
return v1.GetDistanceToSquared(point)
}
}
func (ray *Ray) GetDistanceSqToSegment(v0 *Vector3, v1 *Vector3) float32 {
return 0 // TODO
}