Source

pygame / src / pgWorldObject.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
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/*
  pygame physics - Pygame physics module

  Copyright (C) 2008 Zhang Fan

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#define PHYSICS_WORLD_INTERNAL
#include "pgDeclare.h"
#include "pgphysics.h"
#include "pgVector2.h"
#include "pgAABBBox.h"
#include "pgBodyObject.h"
#include "pgJointObject.h"
#include "pgWorldObject.h"
#include "pgShapeObject.h"
#include "pgCollision.h"
#include "pgHelpFunctions.h"

#define MAX_ITERATION 5

static void _FreeBodySimulation(PyWorldObject* world,double stepTime);
static void _BodyCollisionDetection(PyWorldObject* world, double step);
static void _JointSolve(PyWorldObject* world,double stepTime);
static void _BodyPositionUpdate(PyWorldObject* world,double stepTime);
static void _BodyPositionCorrection(PyWorldObject* world,double stepTime);
static void _Update(PyWorldObject* world,double stepTime);

static void _WorldInit(PyWorldObject* world);
static PyWorldObject* _WorldNewInternal(PyTypeObject *type);
static PyObject* _WorldNew(PyTypeObject *type, PyObject *args, PyObject *kwds);
static void _WorldDestroy(PyWorldObject* world);

static PyObject* _World_update (PyWorldObject* world, PyObject* args);
static PyObject* _World_addBody(PyWorldObject* world,PyObject* args);
static PyObject* _World_removeBody(PyWorldObject* world,PyObject* args);
static PyObject* _World_addJoint(PyWorldObject* world,PyObject* args);
static PyObject* _World_removeJoint(PyWorldObject* world,PyObject* args);
static PyObject* _World_getGravity(PyWorldObject* world,void* closure);
static int _World_setGravity(PyWorldObject* world,PyObject* value,
    void* closure);
static PyObject* _World_getDamping(PyWorldObject* world,void* closure);
static int _World_setDamping(PyWorldObject* world,PyObject* value,
    void* closure);
static PyObject* _World_getBodyList(PyWorldObject* world,void* closure);
static PyObject* _World_getJointList(PyWorldObject* world,void* closure);

/* C API */
static PyObject* PyWorld_New (void);
static int PyWorld_AddBody(PyObject* world, PyObject* body);
static int PyWorld_RemoveBody(PyObject* world, PyObject* body);
static int PyWorld_AddJoint(PyObject* world, PyObject* joint);
static int PyWorld_RemoveJoint(PyObject* world, PyObject* joint);
static int PyWorld_Update(PyObject* world, double dt);

/**
 * Getters and setters for the World
 */
static PyGetSetDef _World_getseters[] = {
    { "damping", (getter)_World_getDamping, (setter)_World_setDamping,
      "The damping within the world. Currently not implemented.", NULL },
    { "gravity",(getter)_World_getGravity, (setter)_World_setGravity,
      "The gravity vector of the world as a (x, y) tuple,", NULL, },
    { "bodies",(getter)_World_getBodyList,NULL,
      "Read-only list of bodies currently attached to the world", NULL },
    { "joints",(getter)_World_getJointList,NULL,
      "Read-only list of joints currently attached to the world",NULL },
    { NULL, NULL, NULL, NULL, NULL }
};

/**
 * Methods for the world.
 */
static PyMethodDef _World_methods[] =
{
    { "update", (PyCFunction) _World_update, METH_VARARGS,
      "W.update (steptime=0.1) -> None\n\n"
      "Performs a simulation for the whole world.\n\n"
      "Simulates the movements, collisions and anything else for all bodies\n"
      "and joints within the world. The steptime represents the resolution\n"
      "for the simulation. Thus a higher steptime will result in a faster,\n"
      "but also more inexact simulation, while a low steptime will cause\n"
      "more precise simulation results."
    },
    { "add_body",(PyCFunction) _World_addBody, METH_VARARGS,
      "W.add_body (body) -> None\n\n"
      "Adds a body to the world. If the body was already added, this raises\n"
      "a ValueError."},
     {"add_joint",(PyCFunction) _World_addJoint, METH_VARARGS,
      "W.add_joint (joint) -> None\n\n"
      "Adds a joint to the world. If the joint was already added, this raises\n"
      "a ValueError."},
    { "remove_body",(PyCFunction) _World_removeBody, METH_VARARGS,
      "W.remove_body (body) -> None\n\n"
      "Removes a body from the world. If the body was not added, this raises\n"
      "a ValueError."},
     {"remove_joint",(PyCFunction) _World_removeJoint, METH_VARARGS,
      "W.remove_joint (joint) -> None\n\n"
      "Remove a joint from the world. If the joint was not added, this raises\n"
      "a ValueError."},
    { NULL, NULL, 0, NULL } /* The NULL sentinel is important! */
};

PyTypeObject PyWorld_Type =
{
    PyObject_HEAD_INIT(NULL)
    0,
    "physics.World",            /* tp_name */
    sizeof(PyWorldObject),      /* tp_basicsize */
    0,                          /* tp_itemsize */
    (destructor) _WorldDestroy, /* tp_dealloc */
    0,                          /* tp_print */
    0,                          /* tp_getattr */
    0,                          /* tp_setattr */
    0,                          /* tp_compare */
    0,                          /* tp_repr */
    0,                          /* tp_as_number */
    0,                          /* tp_as_sequence */
    0,                          /* tp_as_mapping */
    0,                          /* tp_hash */
    0,                          /* tp_call */
    0,                          /* tp_str */
    0,                          /* tp_getattro */
    0,                          /* tp_setattro */
    0,                          /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    "World () -> World\n\n"
    "Creates a new World object.",
    0,                          /* tp_traverse */
    0,                          /* tp_clear */
    0,                          /* tp_richcompare */
    0,                          /* tp_weaklistoffset */
    0,                          /* tp_iter */
    0,                          /* tp_iternext */
    _World_methods,             /* tp_methods */
    0,                          /* tp_members */
    _World_getseters,           /* tp_getset */
    0,                          /* tp_base */
    0,                          /* tp_dict */
    0,                          /* tp_descr_get */
    0,                          /* tp_descr_set */
    0,                          /* tp_dictoffset */
    0,                          /* tp_init */
    0,                          /* tp_alloc */
    _WorldNew,                  /* tp_new */
    0,                          /* tp_free */
    0,                          /* tp_is_gc */
    0,                          /* tp_bases */
    0,                          /* tp_mro */
    0,                          /* tp_cache */
    0,                          /* tp_subclasses */
    0,                          /* tp_weaklist */
    0                           /* tp_del */
};

/**
 * Updates the velocity of the bodies attached to the world.
 *
 * @param world The PyWorldObject to update the bodies for.
 * @param stepTime The time passed since the last update.
 */ 
static void _FreeBodySimulation(PyWorldObject* world,double stepTime)
{
    Py_ssize_t i;
    PyBodyObject* body;
    Py_ssize_t size = PyList_Size((PyObject*)(world->bodyList));
    for (i = 0; i < size; ++i)
    {
        body = (PyBodyObject*)(PyList_GetItem(world->bodyList, i));		
        PyBodyObject_FreeUpdateVel(body, world->vecGravity, stepTime);
    }
}

/**
 * Checks the bodies and joints attached to the world for collisions and
 * updates them accordingly.
 *
 * @param world The PyWorldObject to check the bodies and joints for.
 * @param step The time passed since the last update.
 */
static void _BodyCollisionDetection(PyWorldObject* world, double step)
{
    Py_ssize_t i, j, body_cnt, contact_cnt;
    PyBodyObject *refBody, *incBody;
    PyJointObject* contact;
    PyShapeObject *refShape, *incShape;

    body_cnt = PyList_Size(world->bodyList);
    //clear contactList first
    contact_cnt = PyList_Size(world->contactList);
    if(PyList_SetSlice(world->contactList, 0, contact_cnt, NULL) < 0)
        return;
    assert(PyList_Size(world->contactList)==0);
	
    //for all pair of objects, do collision test	
    //update AABB
    for(i = 0; i < body_cnt; ++i)
    {
        refBody = (PyBodyObject*)(PyList_GetItem(world->bodyList, i));
        PyShapeObject_UpdateAABB (refBody);
    }
    
    //collision test
    for(i = 0; i < body_cnt-1; ++i)
    {
        refBody = (PyBodyObject*)(PyList_GetItem(world->bodyList, i));
        for(j = i+1; j < body_cnt; ++j)
        {
            incBody = (PyBodyObject*)(PyList_GetItem(world->bodyList, j));
            if(refBody->bStatic && incBody->bStatic)
                continue;
            refShape = (PyShapeObject*)refBody->shape;
            incShape = (PyShapeObject*)incBody->shape;
            if(AABB_IsOverlap(&(refShape->box), &(incShape->box), 1e-8))
            {
                PyShapeObject_Collision (refBody, incBody, world->contactList);
            }
        }
    }

    contact_cnt = PyList_Size(world->contactList);
/*     if (contact_cnt) */
/*     { */
/*         printf("contact_cnt:%d\n",contact_cnt); */
/*     } */
    for(j = 0; j < MAX_ITERATION; ++j)
    {
        //clear bias
        for(i = 0; i < body_cnt; ++i)
        {
            refBody = (PyBodyObject*)(PyList_GetItem(world->bodyList, i));
            PyVector2_Set(refBody->cBiasLV, 0.f, 0.f);
            refBody->cBiasW = 0.f;
        }
        //clear impulse
        for(i = 0; i < contact_cnt; ++i)
        {
            contact = (PyJointObject*)(PyList_GetItem(world->contactList, i));
            PyVector2_Set(**(((PyContact*)contact)->ppAccMoment), 0, 0);
            PyVector2_Set(**(((PyContact*)contact)->ppSplitAccMoment), 0, 0);
        }

        //collision reaction
        for(i = 0; i < contact_cnt; ++i)
        {
            contact = (PyJointObject*)(PyList_GetItem(world->contactList, i));
            Collision_ApplyContact((PyObject*)contact, step);
        }
        //update V	
        for(i = 0; i < contact_cnt; ++i)
        {
            contact = (PyJointObject*)(PyList_GetItem(world->contactList, i));
            JointObject_SolveConstraintVelocity (contact, step);
        }
    }
}

/**
 * Updates all joints attached to the world.
 *
 * @param world The PyWorldObject to update the joints for.
 * @param stepTime The time passed since the last update.
 */
static void _JointSolve(PyWorldObject* world,double stepTime)
{
    Py_ssize_t size = PyList_Size((PyObject*)(world->jointList));
    Py_ssize_t i;
    PyJointObject* joint;

    for (i = 0; i < size; ++i)
    {
        joint = (PyJointObject*)(PyList_GetItem(world->jointList,i));
        //what happened here?
        if (joint->SolveConstraintVelocity)
        {
            JointObject_SolveConstraintVelocity (joint, stepTime);
        }
        /*if (joint->SolveConstraintPosition)
          {
          joint->SolveConstraintPosition(joint,stepTime);
          }*/
    }
}

/**
 * Updates the positions of the bodies attached to the world.
 *
 * @param world The PyWorldObject to update the body positions for.
 * @param stepTime The time passed since the last update.
 */
static void _BodyPositionUpdate(PyWorldObject* world,double stepTime)
{
    Py_ssize_t size = PyList_Size(world->bodyList);
    Py_ssize_t i;
    PyBodyObject* body;
    for (i = 0; i < size; ++i)
    {
        body = (PyBodyObject*)(PyList_GetItem(world->bodyList,i));
        PyBodyObject_FreeUpdatePos(body,stepTime);	
    }
}

/**
 * Performs any necessary position correction for the attached bodies.
 *
 * @param world The PyWorldObject to update the bodies for.
 * @param stepTime The time passed since the last update.
 */
static void _BodyPositionCorrection(PyWorldObject* world,double stepTime)
{
    Py_ssize_t size = PyList_Size((PyObject*)(world->bodyList));
    Py_ssize_t i;
    PyBodyObject* body;
    for (i = 0; i < size; ++i)
    {
        body = (PyBodyObject*)(PyList_GetItem((PyObject*)(world->bodyList),i));
        PyBodyObject_CorrectPos(body,stepTime);	
    }
}

/**
 * Performs a full world update inluding body updates, collision dection and
 * any necessary correctins.
 *
 * @param world The PyWorldObject to update.
 * @param stepTime The time passed since the last update.
*/
static void _Update(PyWorldObject* world,double stepTime)
{
    int i;
    _FreeBodySimulation(world, stepTime);
    //_PG_BodyPositionUpdate(world, stepTime);
    for(i = 0; i < MAX_ITERATION; ++i)
    {
        _BodyCollisionDetection(world, stepTime);
        _BodyPositionCorrection(world,stepTime);
        _JointSolve(world, stepTime);
    }
    _BodyPositionUpdate(world, stepTime);
	
}

/**
 * Initializes the internals of the passed PyWorldObject with their defaults.
 *
 * @param world The PyWorldObject to initialize.
 */
static void _WorldInit(PyWorldObject* world)
{
    world->bodyList = PyList_New(0);
    world->jointList = PyList_New(0);
    world->contactList = PyList_New(0);
    world->fDamping = 0.0;
    world->fStepTime = 0.1;
    PyVector2_Set(world->vecGravity,0.0,-10);
    world->fTotalTime = 0.0;
}

/**
 * Creates a new PyWorldObject and initializes it.
 */
static PyWorldObject* _WorldNewInternal(PyTypeObject *type)
{
    PyWorldObject* op = (PyWorldObject*)type->tp_alloc(type, 0);
    if (!op)
        return NULL;

    _WorldInit(op);
    return op;
}

/**
 * Creates a new PyWorldObject and initializes it.
 */
static PyObject* _WorldNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    /* In case we have arguments in the python code, parse them later
     * on.
     */
    return (PyObject*) _WorldNewInternal(type);
}

/**
 * Deallocates the passed PyWorldObject.
 *
 * @param world The PyWorldObject to deallocate.
 */
static void _WorldDestroy(PyWorldObject* world)
{
    /*
     * DECREF anything related to the world, such as the lists and
     * release any other memory hold by it.
     */
    Py_XDECREF(world->bodyList);
    Py_XDECREF(world->jointList);
    Py_XDECREF(world->contactList);

    world->ob_type->tp_free((PyObject*)world);
}

/* World getters/setters */

/**
 * Getter for World.gravity.
 */
static PyObject* _World_getGravity(PyWorldObject* world,void* closure)
{
    return Py_BuildValue ("(ff)", world->vecGravity.real,
        world->vecGravity.imag);
}

/**
 * Setter for World.gravity = (x, y)
 */
static int _World_setGravity(PyWorldObject* world,PyObject* value,void* closure)
{
    PyObject *item;
    double real, imag;
    

    if (!PySequence_Check(value) || PySequence_Size (value) != 2)
    {
        PyErr_SetString (PyExc_TypeError, "gravity must be a x, y sequence");
        return -1;
    }

    item = PySequence_GetItem (value, 0);
    if (!DoubleFromObj (item, &real))
    {
        Py_DECREF (item);
        return -1;
    }
    Py_DECREF (item);

    item = PySequence_GetItem (value, 1);
    if (!DoubleFromObj (item, &imag))
    {
        Py_DECREF (item);
        return -1;
    }    
    Py_DECREF (item);

    world->vecGravity.real = real;
    world->vecGravity.imag = imag;
    return 0;
}

/**
 * Getter for World.damping
 */
static PyObject* _World_getDamping(PyWorldObject* world,void* closure)
{
    return PyFloat_FromDouble (world->fDamping);
}

/**
 * Setter for World.damping = x
 */
static int _World_setDamping(PyWorldObject* world,PyObject* value,
    void* closure)
{
    if (PyNumber_Check (value))
    {
        PyObject *tmp = PyNumber_Float (value);

        if (tmp)
        {
            double damping = PyFloat_AsDouble (tmp);
            Py_DECREF (tmp);
            if (PyErr_Occurred ())
                return -1;
            world->fDamping = damping;
            return 0;
        }
    }
    PyErr_SetString (PyExc_TypeError, "damping must be a float");
    return -1;
    
}

/**
 * Getter for World.bodies
 */
static PyObject* _World_getBodyList(PyWorldObject* world,void* closure)
{
    /* Return a copy of the list, so the user cannot manipulate the bodies
     * directly. */
    return PySequence_List (world->bodyList);
}

/**
 * Getter for World.joints
 */
static PyObject* _World_getJointList(PyWorldObject* world,void* closure)
{
    /* Return a copy of the list, so the user cannot manipulate the bodies
     * directly. */
    return PySequence_List (world->jointList);
}


/* World methods */

/**
 * World.update (x)
 */
static PyObject* _World_update (PyWorldObject* world, PyObject* args)
{
    double dt;
    
    if (!PyArg_ParseTuple(args,"|d", &dt))
        dt = 0.1;

    if (dt < 0)
    {
        PyErr_SetString (PyExc_ValueError,
            "step time must not be smaller than 0");
        return NULL;
    }
    _Update (world,dt);
    Py_RETURN_NONE;
}

/**
 * World.add_body (x)
 */
static PyObject* _World_addBody(PyWorldObject* world, PyObject* args)
{
    PyObject* body;
    if (!PyArg_ParseTuple(args,"O",&body))
        return NULL;

    if (!PyBody_Check (body))
    {
        PyErr_SetString(PyExc_ValueError, "argument must be a body");
        return NULL;
    }

    if(!PyWorld_AddBody((PyObject*)world, body))
        return NULL;
    Py_RETURN_NONE;
}

/**
 * World.remove_body (x)
 */
static PyObject* _World_removeBody(PyWorldObject* world, PyObject* args)
{
    PyObject* body;
    if (!PyArg_ParseTuple(args,"O",&body))
        return NULL;

    if (!PyBody_Check (body))
    {
        PyErr_SetString(PyExc_ValueError, "argument must be a body");
        return NULL;
    }

    if(!PyWorld_RemoveBody((PyObject*)world, body))
        return NULL;
    Py_RETURN_NONE;
}

/**
 * World.add_joint (x)
 */
static PyObject* _World_addJoint(PyWorldObject* world,PyObject* args)
{
    PyObject* joint;
    if (!PyArg_ParseTuple(args,"O",&joint))
        return NULL;

    if (!PyJoint_Check (joint))
    {
        PyErr_SetString(PyExc_ValueError,"argument must be a joint");
        return NULL;
    }
    if(!PyWorld_AddJoint((PyObject*)world,joint))
        return NULL;
    Py_RETURN_NONE;
}

/**
 * World.remove_joint (x)
 */
static PyObject* _World_removeJoint(PyWorldObject* world, PyObject* args)
{
    PyObject* joint;
    if (!PyArg_ParseTuple(args,"O",&joint))
        return NULL;

    if (!PyJoint_Check (joint))
    {
        PyErr_SetString(PyExc_ValueError, "argument must be a joint");
        return NULL;
    }

    if(!PyWorld_RemoveJoint((PyObject*)world, joint))
        return NULL;
    Py_RETURN_NONE;
}

/* C API */
static PyObject* PyWorld_New (void)
{
    return _WorldNew(&PyWorld_Type, NULL, NULL);
}

static int PyWorld_AddBody(PyObject* world, PyObject* body)
{
    int contained;

    if (!PyWorld_Check (world))
    {
        PyErr_SetString (PyExc_TypeError, "world must be a World");
        return 0;
    }
 
    if (!PyBody_Check (body))
    {
        PyErr_SetString (PyExc_TypeError, "body must be a Body");
        return 0;
    }

    contained = PySequence_Contains (((PyWorldObject*)world)->bodyList, body);
    if (contained == 1)
    {
        PyErr_SetString (PyExc_ValueError, "body already added");
        return 0;
    }
    else if (contained == -1)
        return 0; /* Error set by the sequence */

    if (PyList_Append(((PyWorldObject*)world)->bodyList, body) == 0)
        return 1;
    return 0;
}

static int PyWorld_RemoveBody(PyObject* world, PyObject* body)
{
    Py_ssize_t _index;

    if (!PyWorld_Check (world))
    {
        PyErr_SetString (PyExc_TypeError, "world must be a World");
        return 0;
    }
 
    if (!PyBody_Check (body))
    {
        PyErr_SetString (PyExc_TypeError, "body must be a Body");
        return 0;
    }

    _index = PySequence_Index (((PyWorldObject*)world)->bodyList, body);
    if (_index != -1)
    {
        if (PySequence_DelItem (((PyWorldObject*)world)->bodyList, _index) == -1)
            return 0; /* Could not delete */
        return 1;
    }
    /* Not in list. */
    return 0;
}

static int PyWorld_AddJoint(PyObject* world, PyObject* joint)
{
    int contained;

    if (!PyWorld_Check (world))
    {
        PyErr_SetString (PyExc_TypeError, "world must be a World");
        return 0;
    }
 
    if (!PyJoint_Check (joint))
    {
        PyErr_SetString (PyExc_TypeError, "joint must be a Joint");
        return 0;
    }
    
    contained = PySequence_Contains (((PyWorldObject*)world)->jointList, joint);
    if (contained == 1)
    {
        PyErr_SetString (PyExc_ValueError, "joint already added");
        return 0;
    }
    else if (contained == -1)
        return 0; /* Error set by the sequence */

    if (PyList_Append(((PyWorldObject*)world)->jointList, joint) == 0)
        return 1;
    return 0;
}

static int PyWorld_RemoveJoint(PyObject* world, PyObject* joint)
{
    Py_ssize_t _index;

    if (!PyWorld_Check (world))
    {
        PyErr_SetString (PyExc_TypeError, "world must be a World");
        return 0;
    }
 
    if (!PyJoint_Check (joint))
    {
        PyErr_SetString (PyExc_TypeError, "joint must be a Joint");
        return 0;
    }

    _index = PySequence_Index (((PyWorldObject*)world)->jointList, joint);
    if (_index != -1)
    {
        if (PySequence_DelItem (((PyWorldObject*)world)->jointList, _index) == -1)
            return 0; /* Could not delete */
        return 1;
    }
    /* Not in list. */
    return 0;
}

static int PyWorld_Update(PyObject* world, double dt)
{
    if (!PyWorld_Check (world))
    {
        PyErr_SetString (PyExc_TypeError, "world must be a World");
        return 0;
    }

    _Update ((PyWorldObject*)world, dt);
    return 1;
}

void PyWorldObject_ExportCAPI (void **c_api)
{
    c_api[PHYSICS_WORLD_FIRSTSLOT] = &PyWorld_Type;
    c_api[PHYSICS_WORLD_FIRSTSLOT + 1] = &PyWorld_New;
    c_api[PHYSICS_WORLD_FIRSTSLOT + 2] = &PyWorld_AddBody;
    c_api[PHYSICS_WORLD_FIRSTSLOT + 3] = &PyWorld_RemoveBody;
    c_api[PHYSICS_WORLD_FIRSTSLOT + 4] = &PyWorld_AddJoint;
    c_api[PHYSICS_WORLD_FIRSTSLOT + 5] = &PyWorld_RemoveJoint;
    c_api[PHYSICS_WORLD_FIRSTSLOT + 6] = &PyWorld_Update;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.