forked from jakecoffman/cp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape.go
212 lines (174 loc) · 3.71 KB
/
shape.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package cp
import "fmt"
type Shaper interface {
Body() *Body
MassInfo() *ShapeMassInfo
HashId() HashValue
SetHashId(HashValue)
SetSpace(*Space)
BB() BB
SetBB(BB)
}
type ShapeClass interface {
CacheData(transform Transform) BB
Destroy()
PointQuery(p Vector, info *PointQueryInfo)
SegmentQuery(a, b Vector, radius float64, info *SegmentQueryInfo)
}
const (
SHAPE_TYPE_NUM = 3
)
type Shape struct {
Class ShapeClass
space *Space
body *Body
massInfo *ShapeMassInfo
bb BB
sensor bool
e, u float64
surfaceV Vector
UserData interface{}
collisionType CollisionType
Filter ShapeFilter
hashid HashValue
collisionInfo *CollisionInfo
}
func (s Shape) String() string {
return fmt.Sprintf("%T", s.Class)
}
func (s *Shape) Order() int {
switch s.Class.(type) {
case *Circle:
return 0
case *Segment:
return 1
case *PolyShape:
return 2
default:
return 3
}
}
func (s *Shape) Sensor() bool {
return s.sensor
}
func (s *Shape) SetSensor(sensor bool) {
s.body.Activate()
s.sensor = sensor
}
func (s *Shape) Space() *Space {
return s.space
}
func (s *Shape) Body() *Body {
return s.body
}
func (s *Shape) MassInfo() *ShapeMassInfo {
return s.massInfo
}
func (s *Shape) HashId() HashValue {
return s.hashid
}
func (s *Shape) SetHashId(hashid HashValue) {
s.hashid = hashid
}
func (s *Shape) SetSpace(space *Space) {
s.space = space
}
func (s *Shape) BB() BB {
return s.bb
}
func (s *Shape) SetBB(bb BB) {
s.bb = bb
}
func (s *Shape) SetCollisionType(collisionType CollisionType) {
s.body.Activate()
s.collisionType = collisionType
}
func (s *Shape) Friction() float64 {
return s.u
}
func (s *Shape) SetFriction(u float64) {
assert(s.u >= 0, "Must be positive")
s.body.Activate()
s.u = u
}
func (s *Shape) SetSurfaceV(surfaceV Vector) {
s.surfaceV = surfaceV
}
func (s *Shape) Elasticity() float64 {
return s.e
}
func (s *Shape) SetElasticity(e float64) {
assert(s.e >= 0, "Must be positive")
s.body.Activate()
s.e = e
}
func (s *Shape) SetFilter(filter ShapeFilter) {
s.body.Activate()
s.Filter = filter
}
func (s *Shape) CacheBB() BB {
return s.Update(s.body.transform)
}
func (s *Shape) Update(transform Transform) BB {
s.bb = s.Class.CacheData(transform)
return s.bb
}
func (s *Shape) Point(i uint32) *SupportPoint {
switch s.Class.(type) {
case *Circle:
return NewSupportPoint(s.Class.(*Circle).tc, 0)
case *Segment:
seg := s.Class.(*Segment)
if i == 0 {
return NewSupportPoint(seg.ta, i)
}
return NewSupportPoint(seg.tb, i)
case *PolyShape:
poly := s.Class.(*PolyShape)
// Poly shapes may change vertex count.
var index int
if i < uint32(poly.count) {
index = int(i)
}
return NewSupportPoint(poly.planes[index].v0, uint32(index))
default:
return NewSupportPoint(Vector{}, 0)
}
}
func (s *Shape) PointQuery(p Vector) PointQueryInfo {
info := PointQueryInfo{nil, Vector{}, INFINITY, Vector{}}
s.Class.PointQuery(p, &info)
return info
}
func (shape *Shape) SegmentQuery(a, b Vector, radius float64, info *SegmentQueryInfo) bool {
blank := SegmentQueryInfo{nil, b, Vector{}, 1}
if info != nil {
*info = blank
} else {
info = &blank
}
var nearest PointQueryInfo
shape.Class.PointQuery(a, &nearest)
if nearest.Distance <= radius {
info.Shape = shape
info.Alpha = 0
info.Normal = a.Sub(nearest.Point).Normalize()
} else {
shape.Class.SegmentQuery(a, b, radius, info)
}
return info.Shape != nil
}
func NewShape(class ShapeClass, body *Body, massInfo *ShapeMassInfo) *Shape {
return &Shape{
Class: class,
body: body,
massInfo: massInfo,
surfaceV: Vector{},
Filter: ShapeFilter{
Group: NO_GROUP,
Categories: ALL_CATEGORIES,
Mask: ALL_CATEGORIES,
},
collisionInfo: new(CollisionInfo),
}
}