forked from ianremmler/ode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
space.go
258 lines (216 loc) · 7.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
package ode
// #include <ode/ode.h>
// extern void callNearCallback(void *data, dGeomID obj1, dGeomID obj2);
import "C"
import (
"unsafe"
)
// Sweep and prune axis orders
const (
SAPAxesXYZ = C.dSAP_AXES_XYZ
SAPAxesXZY = C.dSAP_AXES_XZY
SAPAxesYXZ = C.dSAP_AXES_YXZ
SAPAxesYZX = C.dSAP_AXES_YZX
SAPAxesZXY = C.dSAP_AXES_ZXY
SAPAxesZYX = C.dSAP_AXES_ZYX
)
// Space represents a space containing bodies.
type Space interface {
c() C.dSpaceID
Destroy()
SetCleanup(mode bool)
Cleanup() bool
SetManualCleanup(mode bool)
SetSublevel(sublevel int)
Sublevel() int
ManualCleanup() bool
Clean()
Class() int
Add(g Geom)
Remove(g Geom)
Query(g Geom) bool
NumGeoms(g Geom) int
Geom(index int) Geom
Collide(data interface{}, cb NearCallback)
NewSphere(radius float64) Sphere
NewBox(lens Vector3) Box
NewPlane(params Vector4) Plane
NewCapsule(radius, length float64) Capsule
NewCylinder(radius, length float64) Cylinder
NewRay(length float64) Ray
NewTriMesh(data TriMeshData) TriMesh
NewHeightfield(data HeightfieldData, placeable bool) Heightfield
NewSimpleSpace() SimpleSpace
NewHashSpace() HashSpace
NewQuadTreeSpace(center, extents Vector3, depth int) QuadTreeSpace
NewSweepAndPruneSpace(axisOrder int) SweepAndPruneSpace
}
// SpaceBase implements Space, and is embedded by specific Space types.
type SpaceBase uintptr
func cToSpace(c C.dSpaceID) Space {
base := SpaceBase(unsafe.Pointer(c))
var s Space
switch int(C.dSpaceGetClass(c)) {
case SimpleSpaceClass:
s = SimpleSpace{base}
case HashSpaceClass:
s = HashSpace{base}
case QuadTreeSpaceClass:
s = QuadTreeSpace{base}
case SweepAndPruneSpaceClass:
s = SweepAndPruneSpace{base}
default:
s = base
}
return s
}
// NilSpace returns the top level "0" space
func NilSpace() Space {
return SpaceBase(0)
}
func (s SpaceBase) c() C.dSpaceID {
return C.dSpaceID(unsafe.Pointer(s))
}
// Destroy destroys the space.
func (s SpaceBase) Destroy() {
C.dSpaceDestroy(s.c())
}
// SetCleanup sets whether contained objects will be destroyed.
func (s SpaceBase) SetCleanup(mode bool) {
C.dSpaceSetCleanup(s.c(), C.int(btoi(mode)))
}
// Cleanup returns whether contained objects will be destroyed.
func (s SpaceBase) Cleanup() bool {
return C.dSpaceGetCleanup(s.c()) != 0
}
// SetManualCleanup sets whether this space is marked for manual cleanup.
func (s SpaceBase) SetManualCleanup(mode bool) {
C.dSpaceSetManualCleanup(s.c(), C.int(btoi(mode)))
}
// ManualCleanup returns whether this space is marked for manual cleanup.
func (s SpaceBase) ManualCleanup() bool {
return C.dSpaceGetManualCleanup(s.c()) != 0
}
// SetSublevel sets the sublevel for this space.
func (s SpaceBase) SetSublevel(sublevel int) {
C.dSpaceSetSublevel(s.c(), C.int(sublevel))
}
// Sublevel returns the sublevel for this space.
func (s SpaceBase) Sublevel() int {
return int(C.dSpaceGetSublevel(s.c()))
}
// Clean cleans the space.
func (s SpaceBase) Clean() {
C.dSpaceClean(s.c())
}
// Class returns the space class.
func (s SpaceBase) Class() int {
return int(C.dSpaceGetClass(s.c()))
}
// Add adds a geometry to the space.
func (s SpaceBase) Add(g Geom) {
C.dSpaceAdd(s.c(), g.c())
}
// Remove removes a geometry from the space.
func (s SpaceBase) Remove(g Geom) {
C.dSpaceRemove(s.c(), g.c())
}
// Query returns whether a geometry is contained in the space.
func (s SpaceBase) Query(g Geom) bool {
return C.dSpaceQuery(s.c(), g.c()) != 0
}
// NumGeoms returns the number of geometries contained in the space.
func (s SpaceBase) NumGeoms(g Geom) int {
return int(C.dSpaceGetNumGeoms(s.c()))
}
// Geom returns the specified contained geometry.
func (s SpaceBase) Geom(index int) Geom {
return cToGeom(C.dSpaceGetGeom(s.c(), C.int(index)))
}
// Collide tests for collision between contained objects.
func (s SpaceBase) Collide(data interface{}, cb NearCallback) {
cbData := &nearCallbackData{fn: cb, data: data}
C.dSpaceCollide(s.c(), unsafe.Pointer(cbData),
(*C.dNearCallback)(C.callNearCallback))
}
// NewSphere returns a new Sphere instance.
func (s SpaceBase) NewSphere(radius float64) Sphere {
return cToGeom(C.dCreateSphere(s.c(), C.dReal(radius))).(Sphere)
}
// NewBox returns a new Box instance.
func (s SpaceBase) NewBox(lens Vector3) Box {
return cToGeom(C.dCreateBox(s.c(), C.dReal(lens[0]), C.dReal(lens[1]), C.dReal(lens[2]))).(Box)
}
// NewPlane returns a new Plane instance.
func (s SpaceBase) NewPlane(params Vector4) Plane {
return cToGeom(C.dCreatePlane(s.c(), C.dReal(params[0]), C.dReal(params[1]),
C.dReal(params[2]), C.dReal(params[3]))).(Plane)
}
// NewCapsule returns a new Capsule instance.
func (s SpaceBase) NewCapsule(radius, length float64) Capsule {
return cToGeom(C.dCreateCapsule(s.c(), C.dReal(radius), C.dReal(length))).(Capsule)
}
// NewCylinder returns a new Cylinder instance.
func (s SpaceBase) NewCylinder(radius, length float64) Cylinder {
return cToGeom(C.dCreateCylinder(s.c(), C.dReal(radius), C.dReal(length))).(Cylinder)
}
// NewRay returns a new Ray instance.
func (s SpaceBase) NewRay(length float64) Ray {
return cToGeom(C.dCreateRay(s.c(), C.dReal(length))).(Ray)
}
// NewHeightfield returns a new Heightfield instance.
func (s SpaceBase) NewHeightfield(data HeightfieldData, placeable bool) Heightfield {
return cToGeom(C.dCreateHeightfield(s.c(), data.c(), C.int(btoi(placeable)))).(Heightfield)
}
// NewConvex returns a new Convex instance.
func (s SpaceBase) NewConvex(planes PlaneList, pts VertexList, polyList PolygonList) Convex {
return cToGeom(C.dCreateConvex(s.c(), (*C.dReal)(&planes[0][0]), C.uint(len(planes)),
(*C.dReal)(&pts[0][0]), C.uint(len(pts)), &polyList[0])).(Convex)
}
// NewTriMesh returns a new TriMesh instance.
func (s SpaceBase) NewTriMesh(data TriMeshData) TriMesh {
return cToGeom(C.dCreateTriMesh(s.c(), data.c(), nil, nil, nil)).(TriMesh)
}
// NewSimpleSpace returns a new SimpleSpace instance.
func (s SpaceBase) NewSimpleSpace() SimpleSpace {
return cToSpace(C.dSimpleSpaceCreate(s.c())).(SimpleSpace)
}
// NewHashSpace returns a new HashSpace instance.
func (s SpaceBase) NewHashSpace() HashSpace {
return cToSpace(C.dHashSpaceCreate(s.c())).(HashSpace)
}
// NewQuadTreeSpace returns a new QuadTreeSpace instance.
func (s SpaceBase) NewQuadTreeSpace(center, extents Vector3, depth int) QuadTreeSpace {
return cToSpace(C.dQuadTreeSpaceCreate(s.c(), (*C.dReal)(¢er[0]),
(*C.dReal)(&extents[0]), C.int(depth))).(QuadTreeSpace)
}
// NewSweepAndPruneSpace returns a new SweepAndPruneSpace instance.
func (s SpaceBase) NewSweepAndPruneSpace(axisOrder int) SweepAndPruneSpace {
return cToSpace(C.dSweepAndPruneSpaceCreate(s.c(), C.int(axisOrder))).(SweepAndPruneSpace)
}
// SimpleSpace represents a simple space.
type SimpleSpace struct {
SpaceBase
}
// HashSpace represents a hash space.
type HashSpace struct {
SpaceBase
}
// SetLevels sets the minimum and maximum levels.
func (s HashSpace) SetLevels(min, max int) {
C.dHashSpaceSetLevels(s.c(), C.int(min), C.int(max))
}
// Levels returns the minimum and maximum levels.
func (s HashSpace) Levels() (int, int) {
var min, max C.int
C.dHashSpaceGetLevels(s.c(), &min, &max)
return int(min), int(max)
}
// QuadTreeSpace represents a quad tree space.
type QuadTreeSpace struct {
SpaceBase
}
// SweepAndPruneSpace represents a sweep and prune space.
type SweepAndPruneSpace struct {
SpaceBase
}