-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathray.go
37 lines (31 loc) · 890 Bytes
/
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
package math
type Ray struct {
Origin Vector3
Direction Vector3
}
func NewRay(origin, direction Vector3) *Ray {
ray := &Ray{Origin: Vec3(0, 0, 0), Direction: Vec3(0, 0, 0)}
ray.Origin.SetVec3(origin)
ray.Direction.SetVec3(direction)
return ray
}
func (r *Ray) Cpy() *Ray {
return &Ray{Origin: r.Origin, Direction: r.Direction}
}
func (r *Ray) Set(origin, direction Vector3) *Ray {
r.Origin.SetVec3(origin)
r.Direction.SetVec3(direction)
return r
}
func (r *Ray) GetEndPoint(distance float32) Vector3 {
return r.Origin.Add(r.Direction.Scale(distance))
}
// Multiplies the ray by the given matrix. Use this to transform a ray into another coordinate system.
func (r *Ray) Mul(matrix Matrix4) *Ray {
tmp := r.Origin.Add(r.Direction)
tmp = matrix.MulVec3(tmp)
r.Origin = matrix.MulVec3(r.Origin)
tmp = tmp.Sub(r.Origin)
r.Direction.Set(tmp.X, tmp.Y, tmp.Z)
return r
}