-
Notifications
You must be signed in to change notification settings - Fork 33
/
circleShape.go
64 lines (54 loc) · 1.7 KB
/
circleShape.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
package chipmunk
import (
"github.com/vova616/chipmunk/transform"
"github.com/vova616/chipmunk/vect"
)
type CircleShape struct {
Shape *Shape
// Center of the circle. Call Update() on the parent shape if changed.
Position vect.Vect
// Radius of the circle. Call Update() on the parent shape if changed.
Radius vect.Float
// Global center of the circle. Do not touch!
Tc vect.Vect
}
// Creates a new CircleShape with the given center and radius.
func NewCircle(pos vect.Vect, radius float32) *Shape {
shape := newShape()
circle := &CircleShape{
Position: pos,
Radius: vect.Float(radius),
Shape: shape,
}
shape.ShapeClass = circle
return shape
}
// Returns ShapeType_Circle. Needed to implemet the ShapeClass interface.
func (circle *CircleShape) ShapeType() ShapeType {
return ShapeType_Circle
}
func (circle *CircleShape) Moment(mass float32) vect.Float {
return (vect.Float(mass) * (0.5 * (circle.Radius * circle.Radius))) + vect.LengthSqr(circle.Position)
}
// Recalculates the global center of the circle and the the bounding box.
func (circle *CircleShape) update(xf transform.Transform) AABB {
//global center of the circle
center := xf.TransformVect(circle.Position)
circle.Tc = center
rv := vect.Vect{circle.Radius, circle.Radius}
return AABB{
vect.Sub(center, rv),
vect.Add(center, rv),
}
}
// Returns ShapeType_Box. Needed to implemet the ShapeClass interface.
func (circle *CircleShape) Clone(s *Shape) ShapeClass {
clone := *circle
clone.Shape = s
return &clone
}
// Returns true if the given point is located inside the circle.
func (circle *CircleShape) TestPoint(point vect.Vect) bool {
d := vect.Sub(point, circle.Tc)
return vect.Dot(d, d) <= circle.Radius*circle.Radius
}