This repository was archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspace.go
144 lines (115 loc) · 4.35 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
package cp
/*
#cgo CFLAGS: -DCP_USE_CGPOINTS=0
#cgo LDFLAGS: -lchipmunk
#include <chipmunk/chipmunk.h>
void cpgoSpaceAddPostStepCallback(cpSpace *space, void *object, void *data);
cpBool cpgoGenericBeginAndPreHandler(cpArbiter *arb, cpSpace *space, void *data);
void cpgoGenericPostAndSeparateHandler(cpArbiter *arb, cpSpace *space, void *data);
void cpgoSpaceAddBeginCollisionHandler(cpSpace *space, cpCollisionType a, cpCollisionType b, void *data);
void cpgoSpaceAddPreSolveCollisionHandler(cpSpace *space, cpCollisionType a, cpCollisionType b, void *data);
void cpgoSpaceAddPostSolveCollisionHandler(cpSpace *space, cpCollisionType a, cpCollisionType b, void *data);
void cpgoSpaceAddSeparateCollisionHandler(cpSpace *space, cpCollisionType a, cpCollisionType b, void *data);
*/
import "C"
import "unsafe"
//export c2goPostStepCallback
func c2goPostStepCallback(space *C.cpSpace, unused unsafe.Pointer, data unsafe.Pointer) {
d := *(*PostStepCallbackData)(data)
d.Callback(d.Space, d.Data)
}
//export c2goCollisionHandler
func c2goCollisionHandler(data unsafe.Pointer, arb *C.cpArbiter) int {
arbiter := NewArbiter(arb)
d := *(*CollisionHandlerData)(data)
return d.Handler(d.Space, arbiter, d.Data)
}
var uglyassshit []*CollisionHandlerData = make([]*CollisionHandlerData, 0)
type Space struct {
CPSpace *C.cpSpace
staticBody *Body
}
type CollisionHandlerData struct {
Space *Space
Data interface{}
Handler CollisionHandler
}
type CollisionHandler func(space *Space, arbiter *Arbiter, data interface{}) int
type PostStepCallbackData struct {
Space *Space
Data interface{}
Callback PostStepCallback
}
type PostStepCallback func(space *Space, object interface{})
func NewSpace() *Space {
var cpspace *C.cpSpace = C.cpSpaceNew();
return &Space{cpspace, nil}
}
func (s *Space) Free() {
C.cpSpaceFree(s.CPSpace)
s.CPSpace = nil
}
func (s *Space) StaticBody() *Body {
if (s.staticBody == nil) {
s.staticBody = &Body{s.CPSpace.staticBody}
}
return s.staticBody
}
func (s *Space) GetIterations() int {
return int(s.CPSpace.iterations)
}
func (s *Space) SetIterations(iter int) {
s.CPSpace.iterations = C.int(iter)
}
func (s *Space) SetGravity(v Vect) {
s.CPSpace.gravity = v.CPVect
}
func (s *Space) AddBody(body *Body) {
C.cpSpaceAddBody(s.CPSpace, body.CPBody)
}
func (s *Space) RemoveBody(body *Body) {
C.cpSpaceRemoveBody(s.CPSpace, body.CPBody)
}
func (s *Space) AddShape(shape *Shape) {
C.cpSpaceAddShape(s.CPSpace, shape.CPShape)
}
func (s *Space) RemoveShape(shape *Shape) {
C.cpSpaceRemoveShape(s.CPSpace, shape.CPShape)
}
func (s *Space) AddStaticShape(shape *Shape) {
C.cpSpaceAddStaticShape(s.CPSpace, shape.CPShape)
}
func (s *Space) RemoveStaticShape(shape *Shape) {
C.cpSpaceRemoveStaticShape(s.CPSpace, shape.CPShape)
}
func (s *Space) AddPostStepCallback(data interface{}, callback PostStepCallback) {
c := PostStepCallbackData{s, data, callback}
C.cpgoSpaceAddPostStepCallback(s.CPSpace, unsafe.Pointer(&data), unsafe.Pointer(&c))
}
func (s *Space) AddBeginCollisionHandler(typeA uint, typeB uint, data interface{}, handler CollisionHandler) {
h := CollisionHandlerData{s, data, handler}
uglyassshit = append(uglyassshit, &h)
C.cpgoSpaceAddBeginCollisionHandler(s.CPSpace,
C.cpCollisionType(typeA), C.cpCollisionType(typeB), unsafe.Pointer(&h))
}
func (s *Space) AddPreSolveCollisionHandler(typeA uint, typeB uint, data interface{}, handler CollisionHandler) {
h := CollisionHandlerData{s, data, handler}
uglyassshit = append(uglyassshit, &h)
C.cpgoSpaceAddPreSolveCollisionHandler(s.CPSpace,
C.cpCollisionType(typeA), C.cpCollisionType(typeB), unsafe.Pointer(&h))
}
func (s *Space) AddPostSolveCollisionHandler(typeA uint, typeB uint, data interface{}, handler CollisionHandler) {
h := CollisionHandlerData{s, data, handler}
uglyassshit = append(uglyassshit, &h)
C.cpgoSpaceAddPostSolveCollisionHandler(s.CPSpace,
C.cpCollisionType(typeA), C.cpCollisionType(typeB), unsafe.Pointer(&h))
}
func (s *Space) AddSeparateCollisionHandler(typeA uint, typeB uint, data interface{}, handler CollisionHandler) {
h := CollisionHandlerData{s, data, handler}
uglyassshit = append(uglyassshit, &h)
C.cpgoSpaceAddSeparateCollisionHandler(s.CPSpace,
C.cpCollisionType(typeA), C.cpCollisionType(typeB), unsafe.Pointer(&h))
}
func (s *Space) Step(dt float64) {
C.cpSpaceStep(s.CPSpace, C.cpFloat(dt))
}