This repository was archived by the owner on Oct 29, 2018. It is now read-only.
forked from vova616/chipmunk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircleShape.go
More file actions
63 lines (53 loc) · 1.59 KB
/
circleShape.go
File metadata and controls
63 lines (53 loc) · 1.59 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
57
58
59
60
61
62
63
package chipmunk
import (
. "github.com/oniproject/chipmunk/algebra"
)
type CircleShape struct {
Shape *Shape
// Center of the circle. Call Update() on the parent shape if changed.
Position Vect
// Radius of the circle. Call Update() on the parent shape if changed.
Radius Float
// Global center of the circle. Do not touch!
Tc Vect
}
// Creates a new CircleShape with the given center and radius.
func NewCircle(pos Vect, radius float32) *Shape {
shape := newShape()
circle := &CircleShape{
Position: pos,
Radius: 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) Float {
return (Float(mass) * (0.5 * (circle.Radius * circle.Radius))) + LengthSqr(circle.Position)
}
// Recalculates the global center of the circle and the the bounding box.
func (circle *CircleShape) update(xf Transform) AABB {
//global center of the circle
center := xf.TransformVect(circle.Position)
circle.Tc = center
rv := Vect{circle.Radius, circle.Radius}
return AABB{
Sub(center, rv),
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) bool {
d := Sub(point, circle.Tc)
return Dot(d, d) <= circle.Radius*circle.Radius
}