-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape.go
306 lines (251 loc) · 9.25 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package chipmunk
/*
Copyright © 2012 Serge Zirukin
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// #include <chipmunk/chipmunk.h>
// #include "shape.h"
import "C"
import (
"unsafe"
)
////////////////////////////////////////////////////////////////////////////////
// NearestPointQueryInfo is a nearest point query struct.
type NearestPointQueryInfo struct {
// Shape is the nearest shape. nil if no shape was within range.
Shape
// P is the closest point on the shape's surface (world space coordinates).
P Vect
// D is the distance to the point. Negative if the point is inside the shape.
D float64
// G is the gradient of the signed distance field for the shape.
G Vect
}
// SegmentQueryInfo holds the result of SegmentQuery.
type SegmentQueryInfo struct {
// Shape is the one that was hit. nil if no collision occured.
Shape
// Normalized distance along the query segment in the range [0;1].
T float64
// Normal of the surface hit.
N Vect
}
// Shape is an opaque collision shape struct.
type Shape interface {
BB() BB
Body() Body
CacheBB() BB
CollisionType() CollisionType
Elasticity() float64
Free()
Friction() float64
Group() Group
Layers() Layers
NearestPointQuery(Vect) (float64, NearestPointQueryInfo)
PointQuery(Vect) bool
SegmentQuery(Vect, Vect) (bool, SegmentQueryInfo)
Sensor() bool
SetBody(Body)
SetCollisionType(CollisionType)
SetElasticity(float64)
SetFriction(float64)
SetGroup(Group)
SetLayers(Layers)
SetSensor(bool)
SetSurfaceVelocity(Vect)
String() string
SurfaceVelocity() Vect
Update(Vect, Vect) BB
c() *C.cpShape
}
// shapeBase is a base for every shape.
type shapeBase uintptr
type shapeType int
////////////////////////////////////////////////////////////////////////////////
const (
circleShapeType = shapeType(C.CP_CIRCLE_SHAPE)
segmentShapeType = shapeType(C.CP_SEGMENT_SHAPE)
polyShapeType = shapeType(C.CP_POLY_SHAPE)
)
////////////////////////////////////////////////////////////////////////////////
// BB returns current bounding box of the shape.
func (s shapeBase) BB() BB {
return cpBB(C.cpShapeGetBB(s.c()))
}
// Body returns the rigid body this collision shape is attached to.
func (s shapeBase) Body() Body {
return cpBody(C.cpShapeGetBody(s.c()))
}
// CacheBB updates, caches and returns the bounding box of a shape based on the body it's attached to.
func (s shapeBase) CacheBB() BB {
return cpBB(C.cpShapeCacheBB(s.c()))
}
// CollisionType returns collision type of the shape used when picking collision handlers.
func (s shapeBase) CollisionType() CollisionType {
return CollisionType(C.cpShapeGetCollisionType(s.c()))
}
// Elasticity returns shape's coefficient of restitution.
func (s shapeBase) Elasticity() float64 {
return float64(C.cpShapeGetElasticity(s.c()))
}
// Free removes a shape.
func (s shapeBase) Free() {
C.cpShapeFree(s.c())
}
// Friction returns shape's coefficient of friction.
func (s shapeBase) Friction() float64 {
return float64(C.cpShapeGetFriction(s.c()))
}
// Group returns a group of the shape. Shapes in the same group don't collide.
func (s shapeBase) Group() Group {
return Group(C.cpShapeGetGroup(s.c()))
}
// HitDistance returns the hit distance for a segment query.
func (s SegmentQueryInfo) HitDistance(start, end Vect) float64 {
return start.Dist(end) * s.T
}
// HitPoint returns the hit point for a segment query.
func (s SegmentQueryInfo) HitPoint(start, end Vect) Vect {
return start.Lerp(end, s.T)
}
// Layers returns layers bitmask of the shape. Shapes collide only if bitwise
// of their layers is non-zero.
func (s shapeBase) Layers() Layers {
return Layers(C.cpShapeGetLayers(s.c()))
}
// NearestPointQuery finds the closest point on the surface of shape to a specific point.
// The first returned value is the distance between the points.
// A negative distance means the point is inside the shape.
func (s shapeBase) NearestPointQuery(p Vect) (float64, NearestPointQueryInfo) {
var out C.cpNearestPointQueryInfo
d := float64(C.cpShapeNearestPointQuery(s.c(), p.c(), &out))
return d, NearestPointQueryInfo{Shape: cpShape(out.shape), P: cpVect(out.p), D: float64(out.d), G: cpVect(out.g)}
}
// PointQuery returns true if a point lies within a shape.
func (s shapeBase) PointQuery(p Vect) bool {
return cpBool(C.cpShapePointQuery(s.c(), p.c()))
}
// ResetShapeIDCounter is used to reset the shape ID counter when recreating a space.
// When initializing a shape, it's hash value comes from a counter.
// Because the hash value may affect iteration order, you can reset the shape ID counter
// when recreating a space. This will make the simulation be deterministic.
func ResetShapeIDCounter() {
C.cpResetShapeIdCounter()
}
// SegmentQuery performs a segment query against a shape.
func (s shapeBase) SegmentQuery(a, b Vect) (bool, SegmentQueryInfo) {
var out C.cpSegmentQueryInfo
d := cpBool(C.cpShapeSegmentQuery(s.c(), a.c(), b.c(), &out))
return d, SegmentQueryInfo{Shape: cpShape(out.shape), T: float64(out.t), N: cpVect(out.n)}
}
// Sensor returns true when shape is "sensor" one, i.e. does not produce collisions.
func (s shapeBase) Sensor() bool {
return cpBool(C.cpShapeGetSensor(s.c()))
}
// SetBody sets the rigid body this collision shape is attached to.
func (s shapeBase) SetBody(b Body) {
C.cpShapeSetBody(s.c(), b.c())
}
// SetCollisionType sets collision type of the shape used when picking collision handlers.
func (s shapeBase) SetCollisionType(t CollisionType) {
C.cpShapeSetCollisionType(s.c(), t.c())
}
// SetElasticity sets coefficient of restitution.
func (s shapeBase) SetElasticity(e float64) {
C.cpShapeSetElasticity(s.c(), C.cpFloat(e))
}
// SetFriction sets coefficient of friction.
func (s shapeBase) SetFriction(f float64) {
C.cpShapeSetFriction(s.c(), C.cpFloat(f))
}
// SetGroup sets a group of the shape. Shapes in the same group don't collide.
func (s shapeBase) SetGroup(g Group) {
C.cpShapeSetGroup(s.c(), g.c())
}
// SetLayers sets layers bitmask of the shape. Shapes collide only if bitwise
// of their layers is non-zero.
func (s shapeBase) SetLayers(l Layers) {
C.cpShapeSetLayers(s.c(), l.c())
}
// SetSensor sets if the shape is "sensor" one, i.e. does not produce collisions,
// but still calls collision callbacks.
func (s shapeBase) SetSensor(b bool) {
C.cpShapeSetSensor(s.c(), boolToC(b))
}
// SetSurfaceVelocity sets surface velocity used when solving friction.
func (s shapeBase) SetSurfaceVelocity(v Vect) {
C.cpShapeSetSurfaceVelocity(s.c(), v.c())
}
// SetUserData sets user definable data pointer.
// Generally this points to your the game object so you can access it
// when given a Shape reference in a callback.
func (s shapeBase) SetUserData(data interface{}) {
C.cpShapeSetUserData(s.c(), dataToC(data))
}
// Space returns space the body was added to or nil if the body doesn't belong to any space.
func (s shapeBase) Space() Space {
return cpSpace(C.cpShapeGetSpace(s.c()))
}
// SurfaceVelocity returns surface velocity used when solving friction.
func (s shapeBase) SurfaceVelocity() Vect {
return cpVect(C.cpShapeGetSurfaceVelocity(s.c()))
}
// Update updates, caches and returns the bounding box of a shape with an explicit transformation.
func (s shapeBase) Update(pos, rot Vect) BB {
return cpBB(C.cpShapeUpdate(s.c(), pos.c(), rot.c()))
}
// UserData returns user defined data.
func (s shapeBase) UserData() interface{} {
return cpData(C.cpShapeGetUserData(s.c()))
}
// addToSpace adds a shape to space.
func (s shapeBase) addToSpace(space Space) {
space.AddShape(cpShape(s.c()))
}
// c converts Shape to C.cpShape pointer.
func (s shapeBase) c() *C.cpShape {
return (*C.cpShape)(unsafe.Pointer(s))
}
// containedInSpace returns true if the space contains the shape.
func (s shapeBase) containedInSpace(space Space) bool {
return cpBool(C.cpSpaceContainsShape(space.c(), s.c()))
}
// cpShape converts C.cpShape pointer to Shape.
func cpShape(s *C.cpShape) Shape {
if nil == s {
return nil
}
p := cpshape(s)
switch shapeType(C.shape_type(s)) {
case circleShapeType:
return CircleShape{p}
case segmentShapeType:
return SegmentShape{p}
case polyShapeType:
return PolyShape{p}
}
panic("unknown type of shape in cpShape")
}
// cpshape converts C.cpShape pointer to shapeBase.
func cpshape(s *C.cpShape) shapeBase {
return shapeBase(unsafe.Pointer(s))
}
// removeFromSpace removes a shape from space.
func (s shapeBase) removeFromSpace(space Space) {
space.RemoveShape(cpShape(s.c()))
}