This repository has been archived by the owner on Oct 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpSpaceComponent.c
349 lines (283 loc) · 12 KB
/
cpSpaceComponent.c
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
/* Copyright (c) 2007 Scott Lembcke
*
* 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 <string.h>
#include "chipmunk/chipmunk_private.h"
//MARK: Sleeping Functions
void
cpSpaceActivateBody(cpSpace *space, cpBody *body)
{
cpAssertHard(cpBodyGetType(body) == CP_BODY_TYPE_DYNAMIC, "Internal error: Attempting to activate a non-dynamic body.");
if(space->locked){
// cpSpaceActivateBody() is called again once the space is unlocked
if(!cpArrayContains(space->rousedBodies, body)) cpArrayPush(space->rousedBodies, body);
} else {
cpAssertSoft(body->sleeping.root == NULL && body->sleeping.next == NULL, "Internal error: Activating body non-NULL node pointers.");
cpArrayPush(space->dynamicBodies, body);
CP_BODY_FOREACH_SHAPE(body, shape){
cpSpatialIndexRemove(space->staticShapes, shape, shape->hashid);
cpSpatialIndexInsert(space->dynamicShapes, shape, shape->hashid);
}
CP_BODY_FOREACH_ARBITER(body, arb){
cpBody *bodyA = arb->body_a;
// Arbiters are shared between two bodies that are always woken up together.
// You only want to restore the arbiter once, so bodyA is arbitrarily chosen to own the arbiter.
// The edge case is when static bodies are involved as the static bodies never actually sleep.
// If the static body is bodyB then all is good. If the static body is bodyA, that can easily be checked.
if(body == bodyA || cpBodyGetType(bodyA) == CP_BODY_TYPE_STATIC){
int numContacts = arb->count;
struct cpContact *contacts = arb->contacts;
// Restore contact values back to the space's contact buffer memory
arb->contacts = cpContactBufferGetArray(space);
memcpy(arb->contacts, contacts, numContacts*sizeof(struct cpContact));
cpSpacePushContacts(space, numContacts);
// Reinsert the arbiter into the arbiter cache
const cpShape *a = arb->a, *b = arb->b;
const cpShape *shape_pair[] = {a, b};
cpHashValue arbHashID = CP_HASH_PAIR((cpHashValue)a, (cpHashValue)b);
cpHashSetInsert(space->cachedArbiters, arbHashID, shape_pair, NULL, arb);
// Update the arbiter's state
arb->stamp = space->stamp;
cpArrayPush(space->arbiters, arb);
cpfree(contacts);
}
}
CP_BODY_FOREACH_CONSTRAINT(body, constraint){
cpBody *bodyA = constraint->a;
if(body == bodyA || cpBodyGetType(bodyA) == CP_BODY_TYPE_STATIC) cpArrayPush(space->constraints, constraint);
}
}
}
static void
cpSpaceDeactivateBody(cpSpace *space, cpBody *body)
{
cpAssertHard(cpBodyGetType(body) == CP_BODY_TYPE_DYNAMIC, "Internal error: Attempting to deactivate a non-dynamic body.");
cpArrayDeleteObj(space->dynamicBodies, body);
CP_BODY_FOREACH_SHAPE(body, shape){
cpSpatialIndexRemove(space->dynamicShapes, shape, shape->hashid);
cpSpatialIndexInsert(space->staticShapes, shape, shape->hashid);
}
CP_BODY_FOREACH_ARBITER(body, arb){
cpBody *bodyA = arb->body_a;
if(body == bodyA || cpBodyGetType(bodyA) == CP_BODY_TYPE_STATIC){
cpSpaceUncacheArbiter(space, arb);
// Save contact values to a new block of memory so they won't time out
size_t bytes = arb->count*sizeof(struct cpContact);
struct cpContact *contacts = (struct cpContact *)cpcalloc(1, bytes);
memcpy(contacts, arb->contacts, bytes);
arb->contacts = contacts;
}
}
CP_BODY_FOREACH_CONSTRAINT(body, constraint){
cpBody *bodyA = constraint->a;
if(body == bodyA || cpBodyGetType(bodyA) == CP_BODY_TYPE_STATIC) cpArrayDeleteObj(space->constraints, constraint);
}
}
static inline cpBody *
ComponentRoot(cpBody *body)
{
return (body ? body->sleeping.root : NULL);
}
void
cpBodyActivate(cpBody *body)
{
if(body != NULL && cpBodyGetType(body) == CP_BODY_TYPE_DYNAMIC){
body->sleeping.idleTime = 0.0f;
cpBody *root = ComponentRoot(body);
if(root && cpBodyIsSleeping(root)){
// TODO should cpBodyIsSleeping(root) be an assertion?
cpAssertSoft(cpBodyGetType(root) == CP_BODY_TYPE_DYNAMIC, "Internal Error: Non-dynamic body component root detected.");
cpSpace *space = root->space;
cpBody *body = root;
while(body){
cpBody *next = body->sleeping.next;
body->sleeping.idleTime = 0.0f;
body->sleeping.root = NULL;
body->sleeping.next = NULL;
cpSpaceActivateBody(space, body);
body = next;
}
cpArrayDeleteObj(space->sleepingComponents, root);
}
CP_BODY_FOREACH_ARBITER(body, arb){
// Reset the idle timer of things the body is touching as well.
// That way things don't get left hanging in the air.
cpBody *other = (arb->body_a == body ? arb->body_b : arb->body_a);
if(cpBodyGetType(other) != CP_BODY_TYPE_STATIC) other->sleeping.idleTime = 0.0f;
}
}
}
void
cpBodyActivateStatic(cpBody *body, cpShape *filter)
{
cpAssertHard(cpBodyGetType(body) == CP_BODY_TYPE_STATIC, "cpBodyActivateStatic() called on a non-static body.");
CP_BODY_FOREACH_ARBITER(body, arb){
if(!filter || filter == arb->a || filter == arb->b){
cpBodyActivate(arb->body_a == body ? arb->body_b : arb->body_a);
}
}
// TODO: should also activate joints?
}
static inline void
cpBodyPushArbiter(cpBody *body, cpArbiter *arb)
{
cpAssertSoft(cpArbiterThreadForBody(arb, body)->next == NULL, "Internal Error: Dangling contact graph pointers detected. (A)");
cpAssertSoft(cpArbiterThreadForBody(arb, body)->prev == NULL, "Internal Error: Dangling contact graph pointers detected. (B)");
cpArbiter *next = body->arbiterList;
cpAssertSoft(next == NULL || cpArbiterThreadForBody(next, body)->prev == NULL, "Internal Error: Dangling contact graph pointers detected. (C)");
cpArbiterThreadForBody(arb, body)->next = next;
if(next) cpArbiterThreadForBody(next, body)->prev = arb;
body->arbiterList = arb;
}
static inline void
ComponentAdd(cpBody *root, cpBody *body){
body->sleeping.root = root;
if(body != root){
body->sleeping.next = root->sleeping.next;
root->sleeping.next = body;
}
}
static inline void
FloodFillComponent(cpBody *root, cpBody *body)
{
// Kinematic bodies cannot be put to sleep and prevent bodies they are touching from sleeping.
// Static bodies are effectively sleeping all the time.
if(cpBodyGetType(body) == CP_BODY_TYPE_DYNAMIC){
cpBody *other_root = ComponentRoot(body);
if(other_root == NULL){
ComponentAdd(root, body);
CP_BODY_FOREACH_ARBITER(body, arb) FloodFillComponent(root, (body == arb->body_a ? arb->body_b : arb->body_a));
CP_BODY_FOREACH_CONSTRAINT(body, constraint) FloodFillComponent(root, (body == constraint->a ? constraint->b : constraint->a));
} else {
cpAssertSoft(other_root == root, "Internal Error: Inconsistency dectected in the contact graph.");
}
}
}
static inline cpBool
ComponentActive(cpBody *root, cpFloat threshold)
{
CP_BODY_FOREACH_COMPONENT(root, body){
if(body->sleeping.idleTime < threshold) return cpTrue;
}
return cpFalse;
}
void
cpSpaceProcessComponents(cpSpace *space, cpFloat dt)
{
cpBool sleep = (space->sleepTimeThreshold != INFINITY);
cpArray *bodies = space->dynamicBodies;
#ifndef NDEBUG
for(int i=0; i<bodies->num; i++){
cpBody *body = (cpBody*)bodies->arr[i];
cpAssertSoft(body->sleeping.next == NULL, "Internal Error: Dangling next pointer detected in contact graph.");
cpAssertSoft(body->sleeping.root == NULL, "Internal Error: Dangling root pointer detected in contact graph.");
}
#endif
// Calculate the kinetic energy of all the bodies.
if(sleep){
cpFloat dv = space->idleSpeedThreshold;
cpFloat dvsq = (dv ? dv*dv : cpvlengthsq(space->gravity)*dt*dt);
// update idling and reset component nodes
for(int i=0; i<bodies->num; i++){
cpBody *body = (cpBody*)bodies->arr[i];
// TODO should make a separate array for kinematic bodies.
if(cpBodyGetType(body) != CP_BODY_TYPE_DYNAMIC) continue;
// Need to deal with infinite mass objects
cpFloat keThreshold = (dvsq ? body->m*dvsq : 0.0f);
body->sleeping.idleTime = (cpBodyKineticEnergy(body) > keThreshold ? 0.0f : body->sleeping.idleTime + dt);
}
}
// Awaken any sleeping bodies found and then push arbiters to the bodies' lists.
cpArray *arbiters = space->arbiters;
for(int i=0, count=arbiters->num; i<count; i++){
cpArbiter *arb = (cpArbiter*)arbiters->arr[i];
cpBody *a = arb->body_a, *b = arb->body_b;
if(sleep){
// TODO checking cpBodyIsSleepin() redundant?
if(cpBodyGetType(b) == CP_BODY_TYPE_KINEMATIC || cpBodyIsSleeping(a)) cpBodyActivate(a);
if(cpBodyGetType(a) == CP_BODY_TYPE_KINEMATIC || cpBodyIsSleeping(b)) cpBodyActivate(b);
}
cpBodyPushArbiter(a, arb);
cpBodyPushArbiter(b, arb);
}
if(sleep){
// Bodies should be held active if connected by a joint to a kinematic.
cpArray *constraints = space->constraints;
for(int i=0; i<constraints->num; i++){
cpConstraint *constraint = (cpConstraint *)constraints->arr[i];
cpBody *a = constraint->a, *b = constraint->b;
if(cpBodyGetType(b) == CP_BODY_TYPE_KINEMATIC) cpBodyActivate(a);
if(cpBodyGetType(a) == CP_BODY_TYPE_KINEMATIC) cpBodyActivate(b);
}
// Generate components and deactivate sleeping ones
for(int i=0; i<bodies->num;){
cpBody *body = (cpBody*)bodies->arr[i];
if(ComponentRoot(body) == NULL){
// Body not in a component yet. Perform a DFS to flood fill mark
// the component in the contact graph using this body as the root.
FloodFillComponent(body, body);
// Check if the component should be put to sleep.
if(!ComponentActive(body, space->sleepTimeThreshold)){
cpArrayPush(space->sleepingComponents, body);
CP_BODY_FOREACH_COMPONENT(body, other) cpSpaceDeactivateBody(space, other);
// cpSpaceDeactivateBody() removed the current body from the list.
// Skip incrementing the index counter.
continue;
}
}
i++;
// Only sleeping bodies retain their component node pointers.
body->sleeping.root = NULL;
body->sleeping.next = NULL;
}
}
}
void
cpBodySleep(cpBody *body)
{
cpBodySleepWithGroup(body, NULL);
}
void
cpBodySleepWithGroup(cpBody *body, cpBody *group){
cpAssertHard(cpBodyGetType(body) == CP_BODY_TYPE_DYNAMIC, "Non-dynamic bodies cannot be put to sleep.");
cpSpace *space = body->space;
cpAssertHard(!cpSpaceIsLocked(space), "Bodies cannot be put to sleep during a query or a call to cpSpaceStep(). Put these calls into a post-step callback.");
cpAssertHard(cpSpaceGetSleepTimeThreshold(space) < INFINITY, "Sleeping is not enabled on the space. You cannot sleep a body without setting a sleep time threshold on the space.");
cpAssertHard(group == NULL || cpBodyIsSleeping(group), "Cannot use a non-sleeping body as a group identifier.");
if(cpBodyIsSleeping(body)){
cpAssertHard(ComponentRoot(body) == ComponentRoot(group), "The body is already sleeping and it's group cannot be reassigned.");
return;
}
CP_BODY_FOREACH_SHAPE(body, shape) cpShapeCacheBB(shape);
cpSpaceDeactivateBody(space, body);
if(group){
cpBody *root = ComponentRoot(group);
body->sleeping.root = root;
body->sleeping.next = root->sleeping.next;
body->sleeping.idleTime = 0.0f;
root->sleeping.next = body;
} else {
body->sleeping.root = body;
body->sleeping.next = NULL;
body->sleeping.idleTime = 0.0f;
cpArrayPush(space->sleepingComponents, body);
}
cpArrayDeleteObj(space->dynamicBodies, body);
}