-
Notifications
You must be signed in to change notification settings - Fork 33
/
boxShape.go
107 lines (89 loc) · 2.21 KB
/
boxShape.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
96
97
98
99
100
101
102
103
104
105
106
107
package chipmunk
import (
"github.com/vova616/chipmunk/transform"
"github.com/vova616/chipmunk/vect"
)
// Convenience wrapper around PolygonShape.
type BoxShape struct {
Shape *Shape
// The polygon that represents this box. Do not touch!
Polygon *PolygonShape
verts [4]vect.Vect
// The width of the box. Call UpdatePoly() if changed.
Width vect.Float
// The height of the box. Call UpdatePoly() if changed.
Height vect.Float
// The center of the box. Call UpdatePoly() if changed.
Position vect.Vect
}
// Creates a new BoxShape with given position, width and height.
func NewBox(pos vect.Vect, w, h vect.Float) *Shape {
shape := newShape()
box := &BoxShape{
Polygon: &PolygonShape{Shape: shape},
Width: w,
Height: h,
Position: pos,
Shape: shape,
}
hw := w / 2.0
hh := h / 2.0
if hw < 0 {
hw = -hw
}
if hh < 0 {
hh = -hh
}
box.verts = [4]vect.Vect{
{-hw, -hh},
{-hw, hh},
{hw, hh},
{hw, -hh},
}
poly := box.Polygon
poly.SetVerts(box.verts[:], box.Position)
shape.ShapeClass = box
return shape
}
func (box *BoxShape) Moment(mass float32) vect.Float {
return (vect.Float(mass) * (box.Width*box.Width + box.Height*box.Height) / 12.0)
}
// Recalculates the internal Polygon with the Width, Height and Position.
func (box *BoxShape) UpdatePoly() {
hw := box.Width / 2.0
hh := box.Height / 2.0
if hw < 0 {
hw = -hw
}
if hh < 0 {
hh = -hh
}
box.verts = [4]vect.Vect{
{-hw, -hh},
{-hw, hh},
{hw, hh},
{hw, -hh},
}
poly := box.Polygon
poly.SetVerts(box.verts[:], box.Position)
}
// Returns ShapeType_Box. Needed to implemet the ShapeClass interface.
func (box *BoxShape) ShapeType() ShapeType {
return ShapeType_Box
}
// Returns ShapeType_Box. Needed to implemet the ShapeClass interface.
func (box *BoxShape) Clone(s *Shape) ShapeClass {
clone := *box
clone.Polygon = &PolygonShape{Shape: s}
clone.Shape = s
clone.UpdatePoly()
return &clone
}
// Recalculates the transformed vertices, axes and the bounding box.
func (box *BoxShape) update(xf transform.Transform) AABB {
return box.Polygon.update(xf)
}
// Returns true if the given point is located inside the box.
func (box *BoxShape) TestPoint(point vect.Vect) bool {
return box.Polygon.TestPoint(point)
}