Commits

Anonymous committed 29d6920

Bodies now require a shape on creation.
Disallowed changing a body shape
Disallowed own shape implementations for now.

Comments (0)

Files changed (7)

 def init_world():
     w = physics.World()
     w.gravity = 0, 1
-    body1 = physics.Body()
-    body1.shape = physics.RectShape(80,33,0)
+    body1 = physics.Body(physics.RectShape(80,33,0))
     body1.position = 100, 100
     body1.velocity = 2,0
     body1.restitution = 1.0
     body1.rotation = 2
     body1.mass = 80*33
     w.add_body(body1)
-    body2 = physics.Body()
-    body2.shape = physics.RectShape (20,20,0)
+    body2 = physics.Body(physics.RectShape (20,20,0))
     body2.position = 200, 100
     body2.velocity = -2, 0
     body1.restitution = 1.0
     w = physics.World()
     w.gravity = 0, 5
     
-    body = physics.Body()
-    body.shape = physics.RectShape (20, 20, 0)
+    body = physics.Body(physics.RectShape (20, 20, 0))
     body.position = 200, 100
     body.static = True
     w.add_body(body)
-    body1 = physics.Body()
-    body1.shape = physics.RectShape (20,20,0)
+    body1 = physics.Body(physics.RectShape (20,20,0))
     body1.position = 200, 200
     w.add_body(body1)
-    body2 = physics.Body()
-    body2.shape = physics.RectShape (20,20,0)
+    body2 = physics.Body(physics.RectShape (20,20,0))
     body2.position = 300, 200
     w.add_body(body2)
     
     w.gravity = 0, 1
     
     for i in range(1, 30):
-        body1 = physics.Body()
-        body1.shape = physics.RectShape(30, 28, 0)
+        body1 = physics.Body(physics.RectShape(30, 28, 0))
         body1.position = 400, -900 + 40*i
         body1.rotation = i
         body1.restitution = 0.0
         w.add_body(body1)
     
     
-    body2 = physics.Body()
-    body2.shape = physics.RectShape (760, 20, 0)
+    body2 = physics.Body(physics.RectShape (760, 20, 0))
     body2.position = 400, 600
     body2.restitution = 0.0
     body2.mass = 1e100
 def init_world():
     w = physics.World()
     w.gravity = 0, 5
-    body = physics.Body()
-    body.shape = physics.RectShape(1, 1, 0)
+    body = physics.Body(physics.RectShape(1, 1, 0))
     body.position = 400, 20
     body.restitution = 0.0
     body.static = True
     w.add_body(body)
     pre_body = body
     for i in range(1, 30):
-        body = physics.Body()
-        body.shape = physics.RectShape(5, 5, 0)
+        body = physics.Body(physics.RectShape(5, 5, 0))
         body.position = 400+10*i, 10*i+20
         body.restitution = 0.0
         body.mass = 20
 } PyBodyObject;
 
 #define PHYSICS_BODY_FIRSTSLOT (PHYSICS_MATH_FIRSTSLOT + PHYSICS_MATH_NUMSLOTS)
-#define PHYSICS_BODY_NUMSLOTS 4
+#define PHYSICS_BODY_NUMSLOTS 3
 #ifndef PHYSICS_BODY_INTERNAL
 #define PyBody_Check(x)                                                 \
     (PyObject_TypeCheck(x,                                              \
         (PyTypeObject*)PyPhysics_C_API[PHYSICS_BODY_FIRSTSLOT+0]))
 #define PyBody_New                                                      \
-    (*(PyObject*(*)(void))PyPhysics_C_API[PHYSICS_BODY_FIRSTSLOT+1])
-#define PyBody_SetShape                                                 \
-    (*(int(*)(PyObject*,PyObject*))PyPhysics_C_API[PHYSICS_BODY_FIRSTSLOT+2])
+    (*(PyObject*(*)(PyObject*))PyPhysics_C_API[PHYSICS_BODY_FIRSTSLOT+1])
 #define PyBody_GetGlobalPos                                             \
-    (*(PyVector2(*)(PyObject*,PyVector2))PyPhysics_C_API[PHYSICS_BODY_FIRSTSLOT+3])
+    (*(PyVector2(*)(PyObject*,PyVector2))PyPhysics_C_API[PHYSICS_BODY_FIRSTSLOT+2])
 #endif /* PYGAME_BODY_INTERNAL */
 
 /**
 #include "pgHelpFunctions.h"
 #include "pgBodyObject.h"
 
-static void _BodyInit(PyBodyObject* body);
+static void _BodyNewInternal (PyBodyObject* body);
+static int _BodyInit (PyBodyObject *body, PyObject *args, PyObject *kwds);
 static PyObject* _BodyNew(PyTypeObject *type, PyObject *args, PyObject *kwds);
 static void _BodyDestroy(PyBodyObject* body);
 
 static PyObject* _Body_getBStatic (PyBodyObject* body,void* closure);
 static int _Body_setBStatic (PyBodyObject* body,PyObject* value,void* closure);
 static PyObject* _Body_getShape(PyBodyObject* body,void* closure);
-static int _Body_setShape(PyBodyObject* body,PyObject* value,void* closure);
 static PyObject *_Body_getPointList(PyObject *self, PyObject *args);
 
 /* C API */
-static PyObject* PyBody_New(void);
-static int PyBody_SetShape(PyObject *body, PyObject *shape);
+static PyObject* PyBody_New(PyObject *shape);
 static PyVector2 PyBody_GetGlobalPos (PyObject *body, PyVector2 point);
 
 /**
 static PyGetSetDef _Body_getseters[] = {
     { "mass", (getter) _Body_getMass, (setter) _Body_setMass, "Mass",
       NULL },
-    { "shape",(getter)_Body_getShape,(setter)_Body_setShape,"Shape", NULL},
+    { "shape",(getter)_Body_getShape, NULL,"Shape", NULL},
     { "rotation", (getter) _Body_getRotation, (setter) _Body_setRotation,
       "Rotation", NULL },
     { "torque", (getter) _Body_getTorque, (setter) _Body_setTorque,
     0,                          /* tp_descr_get */
     0,                          /* tp_descr_set */
     0,                          /* tp_dictoffset */
-    0,                          /* tp_init */
+    (initproc)_BodyInit,                  /* tp_init */
     0,                          /* tp_alloc */
     _BodyNew,                   /* tp_new */
     0,                          /* tp_free */
  *
  * @param body The PyBodyObject to initialize.
  */
-static void _BodyInit(PyBodyObject* body)
+static void _BodyNewInternal(PyBodyObject* body)
 {
     body->fAngleVelocity = 0.0;
     body->fFriction = 0.0;
  */
 static PyObject* _BodyNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    //TODO: parse args later on
     PyObject* op = type->tp_alloc(type, 0);
     if (!op)
         return NULL;
 
-    _BodyInit((PyBodyObject*)op);
+    _BodyNewInternal((PyBodyObject*)op);
     return op;
 }
 
+static int _BodyInit (PyBodyObject *body, PyObject *args, PyObject *kwds)
+{
+    PyObject *shape;
+
+    if (!PyArg_ParseTuple (args, "O", &shape))
+        return -1;
+    if (!PyShape_Check (shape))
+    {
+        PyErr_SetString (PyExc_TypeError, "shape must be a Shape");
+        return -1;
+    }
+    Py_INCREF (shape);
+    body->shape = shape;
+
+    // I = M(a^2 + b^2)/12
+    // TODO:
+    // This should be automatically be done by the shape.
+    if (((PyShapeObject*)shape)->type == ST_RECT)
+    {
+        PyRectShapeObject* rsh = (PyRectShapeObject*) shape;
+        double width = ABS (rsh->bottomright.real - rsh->bottomleft.real);
+        double height = ABS (rsh->bottomright.imag - rsh->topright.imag);
+        ((PyShapeObject*)shape)->rInertia = body->fMass *
+            (width * width + height * height) / 12;
+    }
+
+    return 0;
+}
+
 /**
  * Deallocates the passed PyBodyObject.
  *
     return body->shape;
 }
 
-/**
- * Setter for Body.shape = x
- */
-static int _Body_setShape(PyBodyObject* body,PyObject* value,void* closure)
-{
-    PyShapeObject *shape;
-    if (!PyShape_Check (value))
-    {
-        PyErr_SetString (PyExc_TypeError, "shape must be a Shape");
-        return -1;
-    }
-    if (body->shape)
-    {
-        Py_DECREF (body->shape);
-    }
-    Py_INCREF (value);
-    body->shape = value;
-
-    // I = M(a^2 + b^2)/12
-    // TODO:
-    // This should be automatically be done by the shape.
-    shape = (PyShapeObject*) value;
-    if (shape->type == ST_RECT)
-    {
-        PyRectShapeObject* rsh = (PyRectShapeObject*) shape;
-        double width = ABS (rsh->bottomright.real - rsh->bottomleft.real);
-        double height = ABS (rsh->bottomright.imag - rsh->topright.imag);
-        shape->rInertia = body->fMass *
-            (width * width + height * height) / 12;
-    }
-    return 0;
-}
-
 
 /* Body methods */
 
 }
 
 /* C API */
-static PyObject* PyBody_New(void)
+static PyObject* PyBody_New(PyObject *shape)
 {
-    return _BodyNew(&PyBody_Type, NULL, NULL);
-}
+    PyBodyObject* body;
+    PyShapeObject *sh;
+    
+    if (!PyShape_Check (shape))
+    {
+        PyErr_SetString (PyExc_TypeError, "shape must be Shape");
+        return NULL;
+    }
 
-static int PyBody_SetShape(PyObject *body, PyObject *shape)
-{
-    if (!PyBody_Check (body))
+    body = (PyBodyObject*) _BodyNew(&PyBody_Type, NULL, NULL);
+    body->shape = shape;
+
+    // I = M(a^2 + b^2)/12
+    // TODO:
+    // This should be automatically be done by the shape.
+    if (((PyShapeObject*)shape)->type == ST_RECT)
     {
-        PyErr_SetString (PyExc_TypeError, "body must be a Body");
-        return 0;
+        PyRectShapeObject* rsh = (PyRectShapeObject*) shape;
+        double width = ABS (rsh->bottomright.real - rsh->bottomleft.real);
+        double height = ABS (rsh->bottomright.imag - rsh->topright.imag);
+        ((PyShapeObject*)shape)->rInertia = body->fMass *
+            (width * width + height * height) / 12;
     }
-    if (_Body_setShape ((PyBodyObject*)body, shape, NULL) == -1)
-        return 0;
-    return 1;
+    return (PyObject*)body;
+    
 }
 
 static PyVector2 PyBody_GetGlobalPos (PyObject *body, PyVector2 point)
 {
     c_api[PHYSICS_BODY_FIRSTSLOT] = &PyBody_Type;
     c_api[PHYSICS_BODY_FIRSTSLOT + 1] = &PyBody_New;
-    c_api[PHYSICS_BODY_FIRSTSLOT + 2] = &PyBody_SetShape;
-    c_api[PHYSICS_BODY_FIRSTSLOT + 3] = &PyBody_GetGlobalPos;
+    c_api[PHYSICS_BODY_FIRSTSLOT + 2] = &PyBody_GetGlobalPos;
 }
  Methods used by the Shape.
  */
 static PyMethodDef _Shape_methods[] = {
-    { "_collision",(PyCFunction)_Shape_collision,METH_VARARGS,"" },
-    { "_update_aabb",(PyCFunction)_Shape_updateAABB,METH_VARARGS,"" },
+
+/*     { "_collision",(PyCFunction)_Shape_collision,METH_VARARGS, */
+/*       "S._collision (refbody, incbody, contacts) -> int\n\n" */
+/*       "Simulates a collision and performs appropriate updates.\n\n" */
+/*       "Simulates a collision between the passed bodies and updates their\n" */
+/*       "information such as the velocity or rotation according to it.\n" */
+/*       "Not fully supported yet!" */
+/*     }, */
+/*     { "_update_aabb",(PyCFunction)_Shape_updateAABB,METH_VARARGS, */
+/*       "S._update_aabb (refbody) -> int\n\n" */
+/*       "Updates the passed body's shape AABB information.\n" */
+/*       "Not fully supported yet!" */
+/*     }, */
     { NULL, NULL, 0, NULL }   /* Sentinel */
 };
 
     0,                          /* tp_setattro */
     0,                          /* tp_as_buffer */
     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
-    "",                         /* tp_doc */
+    "Shape () -> Shape\n\n"
+    "Creates a new Shape.",
     0,                          /* tp_traverse */
     0,                          /* tp_clear */
     0,                          /* tp_richcompare */
  Methods used by the RectShape.
  */
 static PyMethodDef _RectShape_methods[] = {
-    { "_collision",(PyCFunction)_RectShape_collision,METH_VARARGS,"" },
-    { "_update_aabb",(PyCFunction)_RectShape_updateAABB,METH_VARARGS,"" },
+/*     { "_collision",(PyCFunction)_RectShape_collision,METH_VARARGS, */
+/*       "" */
+/*     }, */
+/*     { "_update_aabb",(PyCFunction)_RectShape_updateAABB,METH_VARARGS,"" }, */
     { NULL, NULL, 0, NULL }   /* Sentinel */
 };
 
     0,                          /* tp_setattro */
     0,                          /* tp_as_buffer */
     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
-    "",                         /* tp_doc */
+    "RectShape (width, height, rotation=0) -> RectShape\n\n"
+    "Creates a new RectShape with the given dimensions and initial rotation.\n\n",
     0,                          /* tp_traverse */
     0,                          /* tp_clear */
     0,                          /* tp_richcompare */