forked from g3n/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphere.go
75 lines (53 loc) · 2.09 KB
/
sphere.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
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package shape
import "github.com/leapar/engine/math32"
// Sphere is an analytical collision sphere.
type Sphere struct {
radius float32
}
// NewSphere creates and returns a pointer to a new analytical collision sphere.
func NewSphere(radius float32) *Sphere {
s := new(Sphere)
s.radius = radius
return s
}
// SetRadius sets the radius of the analytical collision sphere.
func (s *Sphere) SetRadius(radius float32) {
s.radius = radius
}
// Radius returns the radius of the analytical collision sphere.
func (s *Sphere) Radius() float32 {
return s.radius
}
// IShape =============================================================
// BoundingBox computes and returns the bounding box of the analytical collision sphere.
func (s *Sphere) BoundingBox() math32.Box3 {
return math32.Box3{math32.Vector3{-s.radius, -s.radius, -s.radius}, math32.Vector3{s.radius, s.radius, s.radius}}
}
// BoundingSphere computes and returns the bounding sphere of the analytical collision sphere.
func (s *Sphere) BoundingSphere() math32.Sphere {
return *math32.NewSphere(math32.NewVec3(), s.radius)
}
// Area computes and returns the surface area of the analytical collision sphere.
func (s *Sphere) Area() float32 {
return 4 * math32.Pi * s.radius * s.radius
}
// Volume computes and returns the volume of the analytical collision sphere.
func (s *Sphere) Volume() float32 {
return (4/3) * math32.Pi * s.radius * s.radius * s.radius
}
// RotationalInertia computes and returns the rotational inertia of the analytical collision sphere.
func (s *Sphere) RotationalInertia(mass float32) math32.Matrix3 {
v := (2/5) * mass * s.radius * s.radius
return *math32.NewMatrix3().Set(
v, 0, 0,
0, v, 0,
0, 0, v,
)
}
// ProjectOntoAxis computes and returns the minimum and maximum distances of the analytical collision sphere projected onto the specified local axis.
func (s *Sphere) ProjectOntoAxis(localAxis *math32.Vector3) (float32, float32) {
return -s.radius, s.radius
}