/
space.go
639 lines (533 loc) · 21.2 KB
/
space.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
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 "space.h"
import "C"
import (
"fmt"
"unsafe"
)
////////////////////////////////////////////////////////////////////////////////
// BBQuery is a rectangle query callback function type.
type BBQuery func(s Shape)
// NearestPointQuery is a callback function type for NearestPointQuery function.
type NearestPointQuery func(s Shape, distance float64, point Vect)
// PointQuery is a callback function type for PointQuery function.
type PointQuery func(s Shape)
// SegmentQuery is a query callback function type.
type SegmentQuery func(s Shape, t float64, n Vect)
// Space is a basic unit of simulation in Chipmunk.
type Space uintptr
type spaceData struct {
userData interface{}
}
var (
spaceDataMap = make(map[Space]*spaceData)
)
// SpaceObject is an interface every space object must implement.
type SpaceObject interface {
Free()
addToSpace(Space)
containedInSpace(Space) bool
removeFromSpace(Space)
}
// collisionTypePair is a pair of collision types
type collisionTypePair struct {
a, b CollisionType
}
// collisionHandler is a set of collision handler functions and optional user data
type collisionHandler struct {
beginFunc func(Space, Arbiter, interface{}) bool
preSolveFunc func(Space, Arbiter, interface{}) bool
postStepFunc func(Space, Arbiter, interface{})
separateFunc func(Space, Arbiter, interface{})
data interface{}
}
////////////////////////////////////////////////////////////////////////////////
var (
postStepCallbackMap = make(map[Space]map[interface{}]func(Space, interface{}))
collisionHandlerMap = make(map[Space]map[collisionTypePair]collisionHandler)
defaultCollisionHandlerMap = make(map[Space]collisionHandler)
)
////////////////////////////////////////////////////////////////////////////////
// ActivateShapesTouchingShape activates body (calls Activate()) of any shape
// that overlaps the given shape.
func (s Space) ActivateShapesTouchingShape(sh Shape) {
C.cpSpaceActivateShapesTouchingShape(s.c(), sh.c())
}
// Add an object to space.
func (s Space) Add(obj SpaceObject) SpaceObject {
obj.addToSpace(s)
return obj
}
// AddBody adds a rigid body to the simulation.
func (s Space) AddBody(b Body) Body {
return cpBody(C.cpSpaceAddBody(s.c(), b.c()))
}
// AddConstraint adds a constraint to the simulation.
func (s Space) AddConstraint(c Constraint) Constraint {
return cpConstraint(C.cpSpaceAddConstraint(s.c(), c.c()))
}
// AddPostStepCallback schedules a post-step callback to be called when Space.Step() finishes.
// You can only register one callback per unique value for key.
// Returns true only if the key has never been scheduled before.
func (s Space) AddPostStepCallback(f func(Space, interface{}), key interface{}) bool {
postStepCallbackMap[s][key] = f
return cpBool(C.space_add_poststep(s.c(), dataToC(key), dataToC(f)))
}
// AddCollisionHandler sets a collision handler to be used whenever the two shapes with the
// given collision types collide
func (s Space) AddCollisionHandler(a, b CollisionType,
beginFunc, preSolveFunc func(Space, Arbiter, interface{}) bool,
postStepFunc, separateFunc func(Space, Arbiter, interface{}), data interface{}) {
colTypes := collisionTypePair{a, b}
handler := collisionHandler{beginFunc, preSolveFunc, postStepFunc, separateFunc, data}
collisionHandlerMap[s][colTypes] = handler
C.space_add_collision_handler(s.c(), a.c(), b.c())
}
// SetDefaultCollisionHandler sets a default collision handler for this space. The default
// collision handler is invoked for each colliding pair of shapes that isn't explicitly handled
// by a specific collision handler. You can pass NULL for any function you don't want to
// implement.
func (s Space) SetDefaultCollisionHandler(beginFunc, preSolveFunc func(Space, Arbiter, interface{}) bool,
postStepFunc, separateFunc func(Space, Arbiter, interface{}), data interface{}) {
handler := collisionHandler{beginFunc, preSolveFunc, postStepFunc, separateFunc, data}
defaultCollisionHandlerMap[s] = handler
C.space_set_default_collision_handler(s.c())
}
// AddShape adds a collision shape to the simulation.
// If the shape is attached to a static body, it will be added as a static shape.
func (s Space) AddShape(sh Shape) Shape {
return cpShape(C.cpSpaceAddShape(s.c(), sh.c()))
}
// AddStaticShape explicity adds a shape as a static shape to the simulation.
func (s Space) AddStaticShape(sh Shape) Shape {
return cpShape(C.cpSpaceAddStaticShape(s.c(), sh.c()))
}
// BBQuery performs a fast rectangle query on the space calling a callback
// function for each shape found.
// Only the shape's bounding boxes are checked for overlap, not their full shape.
func (s Space) BBQuery(bb BB, layers Layers, group Group, f BBQuery) {
C.space_bb_query(s.c(), bb.c(), layers.c(), group.c(), unsafe.Pointer(&f))
}
// CollisionBias returns the speed of how fast overlapping shapes are pushed apart.
func (s Space) CollisionBias() float64 {
return float64(C.cpSpaceGetCollisionBias(s.c()))
}
// CollisionSlop returns amount of encouraged penetration between colliding shapes.
func (s Space) CollisionSlop() float64 {
return float64(C.cpSpaceGetCollisionSlop(s.c()))
}
// CollisionPersistence returns the number of frames that contact information should persist.
func (s Space) CollisionPersistence() Timestamp {
return Timestamp(C.cpSpaceGetCollisionPersistence(s.c()))
}
// Contains tests if a collision shape, rigid body or a constraint has been added to the space.
func (s Space) Contains(obj SpaceObject) bool {
return obj.containedInSpace(s)
}
// CurrentTimeStep returns the current (if you are in a callback from SpaceStep())
// or most recent (outside of a SpaceStep() call) timestep.
func (s Space) CurrentTimeStep() float64 {
return float64(C.cpSpaceGetCurrentTimeStep(s.c()))
}
// Damping returns the damping rate expressed as the fraction of velocity bodies retain each second.
func (s Space) Damping() float64 {
return float64(C.cpSpaceGetDamping(s.c()))
}
// Each calls a callback function on each object of specific type (according to iterator) in the space.
func (s Space) Each(iter interface{}) {
switch f := iter.(type) {
case func(Body):
s.EachBody(f)
case func(Constraint):
s.EachConstraint(f)
case func(Shape):
s.EachShape(f)
default:
panic("invalid iterator in Each()")
}
}
// EachBody calls a callback function on each body in the space.
func (s Space) EachBody(iter func(Body)) {
p := unsafe.Pointer(&iter)
C.space_each_body(s.c(), p)
}
// EachConstraint calls a callback function on each constraint in the space.
func (s Space) EachConstraint(iter func(Constraint)) {
p := unsafe.Pointer(&iter)
C.space_each_constraint(s.c(), p)
}
// EachShape calls a callback function on each shape in the space.
func (s Space) EachShape(iter func(Shape)) {
p := unsafe.Pointer(&iter)
C.space_each_shape(s.c(), p)
}
// EnableContactGraph returns true if rebuild of the contact graph during each step is enabled.
func (s Space) EnableContactGraph() bool {
return 0 != int(C.cpSpaceGetEnableContactGraph(s.c()))
}
// Free removes a space.
func (s Space) Free() {
delete(spaceDataMap, s)
delete(postStepCallbackMap, s)
delete(collisionHandlerMap, s)
delete(defaultCollisionHandlerMap, s)
C.cpSpaceFree(s.c())
}
// FreeChildren frees all bodies, constraints and shapes in the space.
func (s Space) FreeChildren() {
remove := func(s Space, obj interface{}) {
s.Remove(obj.(SpaceObject))
s.freeObject(obj.(SpaceObject))
}
s.EachShape(func(shape Shape) {
s.AddPostStepCallback(remove, shape)
})
s.EachConstraint(func(constraint Constraint) {
s.AddPostStepCallback(remove, constraint)
})
s.EachBody(func(body Body) {
s.AddPostStepCallback(remove, body)
})
}
// Gravity returns current gravity used when integrating velocity for rigid bodies.
func (s Space) Gravity() Vect {
return cpVect(C.cpSpaceGetGravity(s.c()))
}
// IdleSpeedThreshold returns speed threshold for a body to be considered idle.
func (s Space) IdleSpeedThreshold() float64 {
return float64(C.cpSpaceGetIdleSpeedThreshold(s.c()))
}
// IsLocked returns true if objects cannot be added/removed inside a callback.
func (s Space) IsLocked() bool {
return cpBool(C.cpSpaceIsLocked(s.c()))
}
// Iterations returns the number of iterations to use in the impulse solver (to solve contacts).
func (s Space) Iterations() int {
return int(C.cpSpaceGetIterations(s.c()))
}
// NearestPointQuery queries the space at a point and calls a callback function for each shape found.
func (s Space) NearestPointQuery(point Vect, maxDistance float64, layers Layers, group Group,
f NearestPointQuery) {
C.space_nearest_point_query(s.c(), point.c(), C.cpFloat(maxDistance), layers.c(), group.c(),
unsafe.Pointer(&f))
}
// PointQuery queries the space at a point and calls a callback function for each shape found.
func (s Space) PointQuery(point Vect, layers Layers, group Group, f PointQuery) {
C.space_point_query(s.c(), point.c(), layers.c(), group.c(), unsafe.Pointer(&f))
}
// PointQueryFirst queries the space at a point and returns
// the first shape found. Returns nil if no shapes were found.
func (s Space) PointQueryFirst(point Vect, layers Layers, group Group) Shape {
return cpShape(C.cpSpacePointQueryFirst(s.c(), point.c(), layers.c(), group.c()))
}
// Remove an object from space.
func (s Space) Remove(obj SpaceObject) {
obj.removeFromSpace(s)
}
// SegmentQuery performs a directed line segment query (like a raycast)
// against the space calling a callback function for each shape intersected.
func (s Space) SegmentQuery(start, end Vect, layers Layers, group Group, f SegmentQuery) {
C.space_segment_query(s.c(), start.c(), end.c(), layers.c(), group.c(), unsafe.Pointer(&f))
}
// SetGravity sets the gravity to pass to rigid bodies when integrating velocity.
func (s Space) SetGravity(g Vect) {
C.cpSpaceSetGravity(s.c(), g.c())
}
// SetCollisionBias sets the speed of how fast overlapping shapes are pushed apart.
// Expressed as a fraction of the error remaining after each second.
// Defaults to pow(1.0 - 0.1, 60.0) meaning that Chipmunk fixes 10% of overlap each frame at 60Hz.
func (s Space) SetCollisionBias(b float64) {
C.cpSpaceSetCollisionBias(s.c(), C.cpFloat(b))
}
// SetCollisionPersistence sets the number of frames that contact information should persist.
// Defaults to 3. There is probably never a reason to change this value.
func (s Space) SetCollisionPersistence(p Timestamp) {
C.cpSpaceSetCollisionPersistence(s.c(), C.cpTimestamp(p))
}
// SetCollisionSlop sets amount of encouraged penetration between colliding shapes.
// Used to reduce oscillating contacts and keep the collision cache warm.
// Defaults to 0.1. If you have poor simulation quality,
// increase this number as much as possible without allowing visible amounts of overlap.
func (s Space) SetCollisionSlop(sl float64) {
C.cpSpaceSetCollisionSlop(s.c(), C.cpFloat(sl))
}
// SetDamping sets the damping rate expressed as the fraction of velocity bodies retain each second.
// A value of 0.9 would mean that each body's velocity will drop 10% per second.
// The default value is 1.0, meaning no damping is applied.
// Note this damping value is different than those of DampedSpring and DampedRotarySpring.
func (s Space) SetDamping(d float64) {
C.cpSpaceSetDamping(s.c(), C.cpFloat(d))
}
// SetEnableContactGraph enables a rebuild of the contact graph during each step.
// Must be enabled to use the EachArbiter() method of Body.
// Disabled by default for a small performance boost.
// Enabled implicitly when the sleeping feature is enabled.
func (s Space) SetEnableContactGraph(cg bool) {
C.cpSpaceSetEnableContactGraph(s.c(), boolToC(cg))
}
// SetIdleSpeedThreshold sets the speed threshold for a body to be considered idle.
// The default value of 0.0 means to let the space guess a good threshold based on gravity.
func (s Space) SetIdleSpeedThreshold(t float64) {
C.cpSpaceSetIdleSpeedThreshold(s.c(), C.cpFloat(t))
}
// SetIterations sets the number of iterations to use in the impulse solver to solve contacts.
func (s Space) SetIterations(i int) {
C.cpSpaceSetIterations(s.c(), C.int(i))
}
// SetSleepTimeThreshold sets the time a group of bodies must remain idle in order to fall asleep.
// Enabling sleeping also implicitly enables the the contact graph.
// The default value of math.Inf(1) disables the sleeping algorithm.
func (s Space) SetSleepTimeThreshold(t float64) {
C.cpSpaceSetSleepTimeThreshold(s.c(), C.cpFloat(t))
}
// SetUserData sets user definable data pointer.
// Generally this points to your game's controller or game state
// so you can access it when given a Space reference in a callback.
func (s Space) SetUserData(data interface{}) {
spaceDataMap[s].userData = data
}
// SleepTimeThreshold returns the time a groups of bodies must remain idle in order to "fall asleep".
func (s Space) SleepTimeThreshold() float64 {
return float64(C.cpSpaceGetSleepTimeThreshold(s.c()))
}
// SpaceNew creates a new space.
func SpaceNew() Space {
s := Space(unsafe.Pointer(C.cpSpaceNew()))
spaceDataMap[s] = &spaceData{}
postStepCallbackMap[s] = make(map[interface{}]func(Space, interface{}))
collisionHandlerMap[s] = make(map[collisionTypePair]collisionHandler)
return s
}
// String converts a space to a human-readable string.
func (s Space) String() string {
return fmt.Sprintf("(Space)%+v", s)
}
// StaticBody returns a dedicated static body for the space.
// You don't have to use it, but because it's memory is managed automatically with the space
// it's very convenient.
// You can set its user data pointer to something helpful if you want for callbacks.
func (s Space) StaticBody() Body {
return cpBody(C.cpSpaceGetStaticBody(s.c()))
}
// ConvertBodyToStatic converts a dynamic rogue body to a static one.
// This will convert any shapes attached to the body into static shapes, but does not handle constra ints.
// If the body is active, you must remove it from the space first.
func (s Space) ConvertBodyToStatic(b Body) {
C.cpSpaceConvertBodyToStatic(s.c(), b.c())
}
// ConvertBodyToDynamic converts a body to a dynamic rogue body.
// This will convert any static shapes attached to the body into regular ones.
// If you want the body to be active after the transition, you must add it to the space also.
func (s Space) ConvertBodyToDynamic(b Body, m, i float64) {
C.cpSpaceConvertBodyToDynamic(s.c(), b.c(), C.cpFloat(m), C.cpFloat(i))
}
// Step makes the space step forward in time by dt seconds.
func (s Space) Step(dt float64) {
C.cpSpaceStep(s.c(), C.cpFloat(dt))
}
// ReindexShape updates the collision detection data for a specific shape in the space.
func (s Space) ReindexShape(sh Shape) {
C.cpSpaceReindexShape(s.c(), sh.c())
}
// ReindexShapesForBody updates the collision detection data for all shapes attached to a body.
func (s Space) ReindexShapesForBody(b Body) {
C.cpSpaceReindexShapesForBody(s.c(), b.c())
}
// ReindexStatic updates the collision detection info for the static shape in the space.
func (s Space) ReindexStatic() {
C.cpSpaceReindexStatic(s.c())
}
// RemoveBody removes a rigid body from the simulation.
func (s Space) RemoveBody(b Body) {
C.cpSpaceRemoveBody(s.c(), b.c())
}
// RemoveCollisionHandler unsets a collision handler.
func (s Space) RemoveCollisionHandler(a CollisionType, b CollisionType) {
colTypes := collisionTypePair{a, b}
delete(collisionHandlerMap[s], colTypes)
C.cpSpaceRemoveCollisionHandler(s.c(), C.cpCollisionType(a), C.cpCollisionType(b))
}
// RemoveConstraint removes a constraint from the simulation.
func (s Space) RemoveConstraint(c Constraint) {
C.cpSpaceRemoveConstraint(s.c(), c.c())
}
// RemoveShape removes a collision shape from the simulation.
func (s Space) RemoveShape(sh Shape) {
C.cpSpaceRemoveShape(s.c(), sh.c())
}
// RemoveStaticShape removes a collision shape added using AddStaticShape() from the simulation.
func (s Space) RemoveStaticShape(sh Shape) {
C.cpSpaceRemoveStaticShape(s.c(), sh.c())
}
// UserData returns user defined data.
func (s Space) UserData() interface{} {
return spaceDataMap[s].userData
}
// UseSpatialHash switches the space to use a spatial has as it's spatial index.
func (s Space) UseSpatialHash(dim float64, count int) {
C.cpSpaceUseSpatialHash(s.c(), C.cpFloat(dim), C.int(count))
}
//export bbQuery
func bbQuery(s *C.cpShape, p unsafe.Pointer) {
f := *(*BBQuery)(p)
f(cpShape(s))
}
// c converts Space to c.cpSpace pointer.
func (s Space) c() *C.cpSpace {
return (*C.cpSpace)(unsafe.Pointer(s))
}
// cpSpace converts C.cpSpace pointer to Space.
func cpSpace(s *C.cpSpace) Space {
return Space(uintptr(unsafe.Pointer(s)))
}
//export eachBodySpace
func eachBodySpace(b *C.cpBody, p unsafe.Pointer) {
f := *(*func(Body))(p)
f(cpBody(b))
}
//export eachConstraintSpace
func eachConstraintSpace(c *C.cpConstraint, p unsafe.Pointer) {
f := *(*func(Constraint))(p)
f(cpConstraint(c))
}
//export eachShapeSpace
func eachShapeSpace(sh *C.cpShape, p unsafe.Pointer) {
f := *(*func(Shape))(p)
f(cpShape(sh))
}
// freeObject frees an object.
func (s Space) freeObject(obj SpaceObject) {
obj.Free()
}
//export nearestPointQuery
func nearestPointQuery(s *C.cpShape, distance C.cpFloat, point C.cpVect, p unsafe.Pointer) {
f := *(*NearestPointQuery)(p)
f(cpShape(s), float64(distance), cpVect(point))
}
//export pointQuery
func pointQuery(s *C.cpShape, p unsafe.Pointer) {
f := *(*PointQuery)(p)
f(cpShape(s))
}
//export postStep
func postStep(s *C.cpSpace, p, data C.cpDataPointer) {
key := cpData(p)
f := cpData(data).(func(Space, interface{}))
// execute callback
f(cpSpace(s), key)
// remove from map
delete(postStepCallbackMap[Space(unsafe.Pointer(s))], key)
}
//export begin
func begin(a *C.cpArbiter, s *C.cpSpace, data C.cpDataPointer) C.cpBool {
arb := cpArbiter(a)
space := cpSpace(s)
sa, sb := arb.Shapes()
colTypes := collisionTypePair{sa.CollisionType(), sb.CollisionType()}
handler := collisionHandlerMap[space][colTypes]
if handler.beginFunc == nil {
return boolToC(true)
}
return boolToC(handler.beginFunc(space, arb, handler.data))
}
//export preSolve
func preSolve(a *C.cpArbiter, s *C.cpSpace, data C.cpDataPointer) C.cpBool {
arb := cpArbiter(a)
space := cpSpace(s)
sa, sb := arb.Shapes()
colTypes := collisionTypePair{sa.CollisionType(), sb.CollisionType()}
handler := collisionHandlerMap[space][colTypes]
if handler.preSolveFunc == nil {
return boolToC(true)
}
return boolToC(handler.preSolveFunc(space, arb, handler.data))
}
//export postSolve
func postSolve(a *C.cpArbiter, s *C.cpSpace, data C.cpDataPointer) {
arb := cpArbiter(a)
space := cpSpace(s)
sa, sb := arb.Shapes()
colTypes := collisionTypePair{sa.CollisionType(), sb.CollisionType()}
handler := collisionHandlerMap[space][colTypes]
if handler.postStepFunc == nil {
return
}
handler.postStepFunc(space, arb, handler.data)
}
//export separate
func separate(a *C.cpArbiter, s *C.cpSpace, data C.cpDataPointer) {
arb := cpArbiter(a)
space := cpSpace(s)
sa, sb := arb.Shapes()
colTypes := collisionTypePair{sa.CollisionType(), sb.CollisionType()}
handler := collisionHandlerMap[space][colTypes]
if handler.separateFunc == nil {
return
}
handler.separateFunc(space, arb, handler.data)
}
//export beginDefault
func beginDefault(a *C.cpArbiter, s *C.cpSpace, data C.cpDataPointer) C.cpBool {
arb := cpArbiter(a)
space := cpSpace(s)
handler := defaultCollisionHandlerMap[space]
if handler.beginFunc == nil {
return boolToC(true)
}
return boolToC(handler.beginFunc(space, arb, handler.data))
}
//export preSolveDefault
func preSolveDefault(a *C.cpArbiter, s *C.cpSpace, data C.cpDataPointer) C.cpBool {
arb := cpArbiter(a)
space := cpSpace(s)
handler := defaultCollisionHandlerMap[space]
if handler.preSolveFunc == nil {
return boolToC(true)
}
return boolToC(handler.preSolveFunc(space, arb, handler.data))
}
//export postSolveDefault
func postSolveDefault(a *C.cpArbiter, s *C.cpSpace, data C.cpDataPointer) {
arb := cpArbiter(a)
space := cpSpace(s)
handler := defaultCollisionHandlerMap[space]
if handler.postStepFunc == nil {
return
}
handler.postStepFunc(space, arb, handler.data)
}
//export separateDefault
func separateDefault(a *C.cpArbiter, s *C.cpSpace, data C.cpDataPointer) {
arb := cpArbiter(a)
space := cpSpace(s)
handler := defaultCollisionHandlerMap[space]
if handler.separateFunc == nil {
return
}
handler.separateFunc(space, arb, handler.data)
}
//export segmentQuery
func segmentQuery(s *C.cpShape, t C.cpFloat, n C.cpVect, p unsafe.Pointer) {
f := *(*SegmentQuery)(p)
f(cpShape(s), float64(t), cpVect(n))
}