Commits

Anonymous committed ed81767

Added copyright notice to all source files.
Added first C API version, update installer to install the C header.
This breaks the current C tests, though.
Body class uses a shape attribute rather than bind_rect_shape() now.
Renamed get_point_list() to get_points().
Renamed vector2 functions to match C API naming PyVector2_*.
Made structures C99 compatible.
Fixed shape and joint inheritances (hopefully).

Comments (0)

Files changed (22)

 from pygame.locals import *
 
 def render_body(body,surface,color):
-    l = body.get_point_list()
+    l = body.get_points()
     pygame.draw.polygon(surface,color,l)
 
 def render_world(world,surface,color):
     w = physics.World()
     w.gravity = 0, 1
     body1 = physics.Body()
-    body1.bind_rect_shape(80,33,0)
+    body1.shape = physics.RectShape(80,33,0)
     body1.position = 100, 100
     body1.velocity = 2,0
     body1.restitution = 3.0
     w.add_body(body1)
     body2 = physics.Body()
-    body2.bind_rect_shape(20,20,0)
+    body2.shape = physics.RectShape (20,20,0)
     body2.position = 200, 100
     body2.velocity = -2, 0
     body1.restitution = 3.0
 from pygame.locals import *
 
 def render_body(body,surface,color):
-    l = body.get_point_list()
+    l = body.get_points()
     pygame.draw.polygon(surface,color,l)
 
 def render_joint(joint,surface,color):
-	l = joint.get_point_list()
+	l = joint.get_points()
 	pygame.draw.lines(surface,color,False,l)
 
 def render_world(world,surface,body_color,joint_color):
 
 def init_world():
     w = physics.World()
-    w.gravity = 0, 1
+    w.gravity = 0, 5
     
     body = physics.Body()
-    body.bind_rect_shape(20,20,0)
+    body.shape = physics.RectShape (20, 20, 0)
     body.position = 200, 100
     body.restitution = 3.0
     body.static = True
     w.add_body(body)
     body1 = physics.Body()
-    body1.bind_rect_shape(20,20,0)
+    body1.shape = physics.RectShape (20,20,0)
     body1.position = 200, 200
-    body1.restitution = 3.0
+    body1.restitution = 300.0
     w.add_body(body1)
     body2 = physics.Body()
-    body2.bind_rect_shape(20,20,0)
+    body2.shape = physics.RectShape (20,20,0)
     body2.position = 300, 200
     body1.restitution = 3.0
     w.add_body(body2)
     
     joint1 = physics.DistanceJoint(body1,body,1)
-    joint1.distance = 100
+    #joint1.distance = 10
     joint1.anchor1 = 0, 0
     joint1.anchor2 = 0, 0
     w.add_joint(joint1)
     joint2 = physics.DistanceJoint(body1,body2,1)
-    joint2.distance = 100
+    #joint2.distance = 10
     joint2.anchor1 = 0, 0
     joint2.anchor2 = 0, 0
     w.add_joint(joint2)

include/pgAABBBox.h

-#ifndef _PYGAME_MATH_AABBBOX_
-#define _PYGAME_MATH_AABBBOX_
+/*
+  pygame physics - Pygame physics module
 
-#include "pgVector2.h"
+  Copyright (C) 2008 Zhang Fan
 
-//typedef struct _pgAABBBox pgAABBBox;
+  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.
 
-//TODO: complete the AABBBox
-typedef struct _pgAABBBox{
-	union
-	{
-		struct
-		{
-			double left, bottom, right, top;
-		};
+  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.
 
-		struct
-		{
-			double from[2], to[2];
-		};
-	};
-} pgAABBBox;
+  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
+*/
 
-pgAABBBox PG_GenAABB(double left, double right, double bottom, double top);
-void PG_AABBExpandTo(pgAABBBox* box, pgVector2* p);
-void PG_AABBClear(pgAABBBox* box);
-int PG_IsOverlap(pgAABBBox* boxA, pgAABBBox* boxB, double eps);
-int PG_IsIn(pgVector2* p, pgAABBBox* box, double eps);
+#ifndef _PHYSICS_AABBBOX_H_
+#define _PHYSICS_AABBBOX_H_
 
-#endif //_PYGAME_MATH_AABBBOX_
+#include "pgphysics.h"
+
+/**
+ * TODO
+ *
+ * @param left
+ * @param right
+ * @param bottom
+ * @param top
+ * @return 
+ */
+AABBBox AABB_Gen(double left, double right, double bottom, double top);
+
+/**
+ * TODO
+ *
+ * @param box
+ * @param p
+ */
+void AABB_ExpandTo(AABBBox* box, PyVector2* p);
+
+/**
+ * TODO
+ *
+ * @param box
+ */
+void AABB_Clear(AABBBox* box);
+
+/**
+ * TODO
+ *
+ * @param boxA
+ * @param boxB
+ * @param eps
+ * @return 
+ */
+int AABB_IsOverlap(AABBBox* boxA, AABBBox* boxB, double eps);
+
+/**
+ * TODO
+ *
+ * @param p
+ * @param box
+ * @param eps
+ * @return 
+ */
+int AABB_IsIn(PyVector2* p, AABBBox* box, double eps);
+
+#endif /* _PHYSICS_AABBBOX_H_ */

include/pgBodyObject.h

-#ifndef _PYGAME_PHYSICS_BODY_
-#define _PYGAME_PHYSICS_BODY_
+/*
+  pygame physics - Pygame physics module
 
+  Copyright (C) 2008 Zhang Fan
 
-#include "pgVector2.h"
-#include "pgDeclare.h"
+  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.
 
-//! typedef struct _pgWorldObject pgWorldObject;
-//! typedef struct _pgShapeObject pgShapeObject;
+  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.
 
-struct _pgBodyObject{
-	PyObject_HEAD
+  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
+*/
 
-	double		fMass;
-	pgVector2	vecLinearVelocity;
-	double		fAngleVelocity;
-	int			bStatic;
+#ifndef _PHYSICS_BODY_H_
+#define _PHYSICS_BODY_H_
 
-	pgVector2	vecPosition;
-	double		fRotation;
-	pgVector2	vecImpulse;
-	pgVector2	vecForce;
-	double		fTorque;
+#include "pgphysics.h"
 
-	double		fRestitution;
-	double		fFriction;
+/**
+ * TODO
+ *
+ * @param body
+ * @param gravity
+ * @param dt
+ */
+void PyBodyObject_FreeUpdateVel(PyBodyObject* body, PyVector2 gravity,
+    double dt);
 
-	pgShapeObject* shape;
+/**
+ * TODO
+ *
+ * @param body
+ * @param dt
+ */
+void PyBodyObject_FreeUpdatePos (PyBodyObject* body, double dt);
 
-	pgVector2 cBiasLV;
-	double cBiasW;
+/**
+ * TODO
+ *
+ * @param body
+ * @param dt
+ */
+void PyBodyObject_CorrectPos(PyBodyObject* body, double dt);
 
-};
+/**
+ * Transform point local_p's position from body's local coordinate to
+ * the world's global one.
+ * TODO: is the local coordinate necessary?  anyway let it alone right
+ * now.
+ *
+ * @param body
+ * @param local_p
+ * @return
+ */
+PyVector2 PyBodyObject_GetGlobalPos(PyBodyObject* body, PyVector2* local_p);
 
-pgBodyObject* PG_BodyNew();
-void	PG_BodyDestroy(pgBodyObject* body);
+/**
+ * Translate vector from coordinate B to coordinate A
+ *
+ * @param bodyA
+ * @param bodyB
+ * @param p_in_B
+ * @return
+ */
+PyVector2 PyBodyObject_GetRelativePos(PyBodyObject* bodyA, PyBodyObject* bodyB,
+    PyVector2* p_in_B);
 
-void PG_FreeUpdateBodyVel(pgWorldObject* world, pgBodyObject* body, double dt);
-void PG_FreeUpdateBodyPos(pgBodyObject* body, double dt);
-void PG_CorrectBodyPos(pgBodyObject* body, double dt);
+/**
+ * Get velocity with a local point of a body,assume center is local (0,0)
+ *
+ * @param body
+ * @param localPoint
+ * @return
+ */
+PyVector2 PyBodyObject_GetLocalPointVelocity(PyBodyObject* body,
+    PyVector2 localPoint);
 
-//transform point local_p's position from body's local coordinate to the world's global one.
-//TODO: is the local coordinate necessary? anyway let it alone right now.
-pgVector2 PG_GetGlobalPos(pgBodyObject* body, pgVector2* local_p);
+/**
+ * Python C API export hook
+ *
+ * @param c_api Pointer to the C API array.
+ */
+void PyBodyObject_ExportCAPI (void **c_api);
 
-//translate vector from coordinate B to coordinate A
-pgVector2 PG_GetRelativePos(pgBodyObject* bodyA, pgBodyObject* bodyB, pgVector2* p_in_B);
-
-
-//bind rect shape with body
-void PG_Bind_RectShape(pgBodyObject* body, double width, double height, double seta);
-
-//get velocity with a local point of a body,assume center is local (0,0)
-pgVector2 PG_GetLocalPointVelocity(pgBodyObject* body,pgVector2 localPoint);
-
-#endif //_PYGAME_PHYSICS_BODY_
+#endif /* _PHYSICS_BODY_H_ */

include/pgCollision.h

-#ifndef _PYGAME_PHYSICS_COLLISION_
-#define _PYGAME_PHYSICS_COLLISION_
+/*
+  pygame physics - Pygame physics module
 
-#include "pgBodyObject.h"
-#include "pgJointObject.h"
-#include "pgAABBBox.h"
+  Copyright (C) 2008 Zhang Fan
 
-typedef struct _pgContact
+  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
+*/
+
+#ifndef _PHYSICS_COLLISION_H_
+#define _PHYSICS_COLLISION_H_
+
+#include "pgphysics.h"
+
+/**
+ * TODO
+ */
+typedef struct
 {
-	//assert body2 is the incident rigid body
-	//and body1 is the reference rigid body
-	pgJointObject joint;
+    //assert body2 is the incident rigid body
+    //and body1 is the reference rigid body
+    PyJointObject joint;
 
-	pgVector2 pos;
-	pgVector2 normal;
-	pgVector2 dv;
-	double depth;
-	double weight;
-	double resist;
-	double kFactor, tFactor;
-	pgVector2** ppAccMoment;
-	pgVector2** ppSplitAccMoment;
-}pgContact;
+    PyVector2 pos;
+    PyVector2 normal;
+    PyVector2 dv;
+    double depth;
+    double weight;
+    double resist;
+    double kFactor, tFactor;
+    PyVector2** ppAccMoment;
+    PyVector2** ppSplitAccMoment;
+} PyContact;
 
-typedef enum _pgCollisionType
+/**
+ * TODO
+ */
+typedef enum
 {
-	MOVING_AWAY,
-	RESTING,
-	MOVING_TOWARD
-}pgCollisionType;
+    MOVING_AWAY,
+    RESTING,
+    MOVING_TOWARD
+} CollisionType;
 
-typedef enum _pgCollisionAxis
+/**
+ * TODO
+ */
+typedef enum
 {
-	CA_X = 0,
-	CA_Y = 1
-}pgCollisionAxis;
+    CA_X = 0,
+    CA_Y = 1
+} CollisionAxis;
 
-typedef enum _pgCollisionFace
+/**
+ * TODO
+ */
+typedef enum
 {
-	CF_LEFT,
-	CF_BOTTOM,
-	CF_RIGHT,
-	CF_TOP
-}pgCollisionFace;
+    CF_LEFT,
+    CF_BOTTOM,
+    CF_RIGHT,
+    CF_TOP
+} CollisionFace;
 
-int PG_LiangBarskey(pgAABBBox* box, pgVector2* p1, pgVector2* p2, 
-					 pgVector2* ans_p1, pgVector2* ans_p2);
+/**
+ * TODO
+ *
+ * @param
+ * @param
+ * @param
+ * @param
+ * @param
+ * @return
+ */
+int Collision_LiangBarskey(AABBBox* box, PyVector2* p1, PyVector2* p2, 
+    PyVector2* ans_p1, PyVector2* ans_p2);
 
-int PG_PartlyLB(pgAABBBox* box, pgVector2* p1, pgVector2* p2, 
-				pgCollisionAxis axis, pgVector2* ans_p1, pgVector2* ans_p2,
-				int* valid_p1, int* valid_p2);
+/**
+ * TODO
+ *
+ * @param
+ * @param
+ * @param
+ * @param
+ * @param
+ * @param
+ * @param
+ * @param
+ * @return
+ */
+int Collision_PartlyLB(AABBBox* box, PyVector2* p1, PyVector2* p2, 
+    CollisionAxis axis, PyVector2* ans_p1, PyVector2* ans_p2,
+    int* valid_p1, int* valid_p2);
 
-pgJointObject* PG_ContactNew(pgBodyObject* refBody, pgBodyObject* incidBody);
+/**
+ * TODO
+ *
+ * @param
+ * @param
+ * @return
+ */
+PyJointObject* Collision_ContactNew(PyBodyObject* refBody,
+    PyBodyObject* incidBody);
 
-void PG_DetectCollision(pgBodyObject* refBody, pgBodyObject* incidBody, PyObject* contactList);
-void PG_ApplyContact(PyObject* contactObject, double step);
+/**
+ * TODO
+ *
+ * @param
+ * @param
+ * @param
+ */
+void Collision_DetectCollision(PyBodyObject* refBody, PyBodyObject* incidBody,
+    PyObject* contactList);
 
-#endif
+/**
+ * TODO
+ *
+ * @param
+ * @param
+ */
+void Collision_ApplyContact(PyObject* contactObject, double step);
+
+/**
+ * TODO
+ *
+ * @param refBody
+ * @param incidBody
+ */
+PyObject* PyContact_New(PyBodyObject* refBody, PyBodyObject* incidBody);
+
+#endif /* _PHYSICS_COLLISION_H_ */

include/pgDeclare.h

-#ifndef _PG_DECLARE_H
-#define _PG_DECLARE_H
+/*
+  pygame physics - Pygame physics module
 
-//forward declaration of structures used by multiple .h files
-//to resolve circular dependency
+  Copyright (C) 2008 Zhang Fan
 
-typedef struct _pgBodyObject pgBodyObject;
-typedef struct _pgShapeObject pgShapeObject;
-typedef struct _pgJointObject pgJointObject;
-typedef struct _pgWorldObject pgWorldObject;
+  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.
 
-#endif
+  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
+*/
+
+#ifndef _PHYSICS_DECLARE_H_
+#define _PHYSICS_DECLARE_H_
+
+#include <Python.h>
+
+/*
+ * Internally used declarations.
+ */
+
+#ifdef PHYSICS_INTERNAL
+
+extern PyTypeObject PyBody_Type;
+extern PyTypeObject PyContact_Type;
+extern PyTypeObject PyJoint_Type;
+extern PyTypeObject PyDistanceJoint_Type;
+extern PyTypeObject PyWorld_Type;
+extern PyTypeObject PyShape_Type;
+extern PyTypeObject PyRectShape_Type;
+
+#define PHYSICS_BODY_INTERNAL
+#define PHYSICS_JOINT_INTERNAL
+#define PHYSICS_SHAPE_INTERNAL
+#define PHYSICS_WORLD_INTERNAL
+#define PHYSICS_MATH_INTERNAL
+
+#define PyBody_Check(x) (PyObject_TypeCheck(x, &PyBody_Type))
+#define PyContact_Check(x) (PyObject_TypeCheck(x, &PyContact_Type))
+#define PyJoint_Check(x) (PyObject_TypeCheck(x, &PyDistanceJoint_Type))
+#define PyDistanceJoint_Check(x) (PyObject_TypeCheck(x, &PyBody_Type))
+#define PyWorld_Check(x) (PyObject_TypeCheck(x, &PyWorld_Type))
+#define PyShape_Check(x) (PyObject_TypeCheck(x, &PyShape_Type))
+#define PyRechtShape_Check(x) (PyObject_TypeCheck(x, &PyRectShape_Type))
+
+#endif /* PHYSICS_INTERNAL */
+
+#endif /* _PHYSICS_DECLARE_H_ */

include/pgHelpFunctions.h

-#ifndef _PG_HELPFUNCTIONS_H
-#define _PG_HELPFUNCTIONS_H
+/*
+  pygame physics - Pygame physics module
 
-#include <Python.h>
-#include "pgVector2.h"
+  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
+*/
+
+#ifndef _PHYSICS_HELPFUNCTIONS_H_
+#define _PHYSICS_HELPFUNCTIONS_H_
+
+#include "pgphysics.h"
 
 /**
  * Tries to retrieve the double value from a python object.
 int
 DoubleFromObj (PyObject* obj, double* val);
 
+/**
+ * Returns a two-value tuple containing floats representing the passed
+ * PyVector2.
+ *
+ * @param v2 The PyVector2 to convert to a tw-value tuple.
+ * @return A tuple.
+ */
 PyObject*
-FromPhysicsVector2ToPoint (pgVector2 v2);
+FromPhysicsVector2ToPoint (PyVector2 v2);
 
-#endif /* _PG_HELPFUNCTIONS_H */
+#endif /* _PHYSICS_HELPFUNCTIONS_H_ */

include/pgJointObject.h

-#ifndef _PYGAME_PHYSICS_JOINT_
-#define _PYGAME_PHYSICS_JOINT_
+/*
+  pygame physics - Pygame physics module
 
-#include "pgBodyObject.h"
-#include "pgDeclare.h"
+  Copyright (C) 2008 Zhang Fan
 
-struct _pgJointObject{
-	PyObject_HEAD
+  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.
 
-	pgBodyObject*	body1;
-	pgBodyObject*	body2;
-	int		isCollideConnect;
-	void	(*SolveConstraintPosition)(pgJointObject* joint,double stepTime);
-	void	(*SolveConstraintVelocity)(pgJointObject* joint,double stepTime);
-	void	(*Destroy)(pgJointObject* joint);
-};
+  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.
 
-void PG_JointDestroy(pgJointObject* joint);
+  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
+*/
 
-typedef struct _pgDistanceJointObject{
-	pgJointObject		joint;
+#ifndef _PHYSICS_JOINT_H_
+#define _PHYSICS_JOINT_H_
 
-	double		distance;
-	pgVector2	anchor1,anchor2;
-} pgDistanceJointObject;
+/**
+ * Python C API export hook.
+ *
+ * @param c_api Pointer to the C API array.
+ */
+void PyJointObject_ExportCAPI (void **c_api);
 
-pgJointObject* PG_DistanceJointNew(pgBodyObject* b1,pgBodyObject* b2,int bCollideConnect,double distance,pgVector2 a1,pgVector2 a2);
-
-extern PyTypeObject pgJointType;
-
-#endif //_PYGAME_PHYSICS_JOINT_
-
+#endif /* _PHYSICS_JOINT_H_ */

include/pgShapeObject.h

-#ifndef _PYGAME_PHYSICS_SHAPE_
-#define _PYGAME_PHYSICS_SHAPE_
+/*
+  pygame physics - Pygame physics module
 
-#include <Python.h>
-#include "pgAABBBox.h"
-#include "pgVector2.h"
-#include "pgDeclare.h"
+  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.
 
-typedef enum _ShapeType
-{
-	ST_RECT,
-	ST_CIRCLE
-}ShapeType;
+  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
+*/
 
-// shape base type
-struct _pgShapeObject{
-	PyObject_HEAD
+#ifndef _PHYSICS_SHAPE_H_
+#define _PHYSICS_SHAPE_H_
 
-	pgAABBBox box;
-	ShapeType type;
-	double rInertia; //Rotor inertia  
+/**
+ * Python C API export hook
+ *
+ * @param c_api Pointer to the C API array.
+ */
+void PyShapeObject_ExportCAPI (void **c_api);
 
-	//virtual functions
-	void (*Destroy)(pgShapeObject* shape);
-	int (*Collision)(pgBodyObject* selfBody, pgBodyObject* incidBody, PyObject* contactList);
-	void (*UpdateAABB)(pgBodyObject* body);
-};
-
-
-void	PG_ShapeObjectDestroy(pgShapeObject* shape);
-
-//subclass type
-typedef struct _pgRectShape{
-	pgShapeObject shape;
-
-	union
-	{
-		struct
-		{
-			pgVector2 point[4];
-		};
-		struct
-		{
-			pgVector2 bottomLeft, bottomRight, topRight, topLeft;
-		};
-	};
-	
-} pgRectShape;
-
-pgShapeObject*	PG_RectShapeNew(pgBodyObject* body, double width, double height, double seta);
-
-//typedef struct _pgPolygonShape{
-//	pgShapeObject		shape;
-//
-//	PyListObject*		vertexList;
-//}pgPolygonShape;
-
-#endif //_PYGAME_PHYSICS_SHAPE_
+#endif /* _PHYSICS_SHAPE_H_ */

include/pgVector2.h

-#ifndef _PYGAME_COMPLEX_EXTENSIONS_
-#define _PYGAME_COMPLEX_EXTENSIONS_
+/*
+  pygame physics - Pygame physics module
 
+  Copyright (C) 2008 Zhang Fan
 
-#include <Python.h>
-#include <math.h>
+  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.
 
-#define ZERO_EPSILON 1e-6
-#define RELATIVE_ZERO 1e-6
+  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.
 
-#ifndef M_PI
-#define M_PI 3.1415926535897932384626433832795
-#endif
+  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
+*/
 
+#ifndef _PYGAME_VECTOR2_H_
+#define _PYGAME_VECTOR2_H_
 
-typedef Py_complex	pgVector2;
-#define PG_Set_Vector2(vec, x, y) {(vec).real = x; (vec).imag = y;}
+#define PHYSICS_MATH_INTERNAL
 
-int is_zero(double num);
-int is_equal(double a, double b);
-int less_equal(double a, double b);
-int more_equal(double a, double b);
+#include "pgphysics.h"
 
-#define MAX(x, y) ( ((x) > (y)) ? (x) : (y) )
-#define MIN(x, y) ( ((x) < (y)) ? (x) : (y) )
+/*
+ * Internal math function declarations. Used to circumvent the C API
+ * array. See pgphysics.h for their descriptions.
+ */
+int PyMath_IsNearEqual(double a, double b);
+int PyMath_LessEqual(double a, double b);
+int PyMath_MoreEqual(double a, double b);
+int PyVector2_Equal(PyVector2* a, PyVector2* b);
+PyVector2 PyVector2_MultiplyWithReal (PyVector2 a, double f);
+PyVector2 PyVector2_DivideWithReal (PyVector2 a, double f);
+PyVector2 PyVector2_fCross(double f, PyVector2 a);
+PyVector2 PyVector2_Crossf(PyVector2 a, double f);
+PyVector2 PyVector2_Project(PyVector2 a, PyVector2 p);
 
-
-double c_get_length_square(pgVector2 c);
-double c_get_length(pgVector2 c);
-Py_complex c_mul_complex_with_real(pgVector2 c,double d);
-Py_complex c_div_complex_with_real(pgVector2 c,double d);
-void	c_normalize(pgVector2* pVec);
-double c_dot(pgVector2 a,pgVector2 b);
-double c_cross(pgVector2 a, pgVector2 b);
-pgVector2 c_fcross(double a, pgVector2 b);
-pgVector2 c_crossf(pgVector2 a, double b);
-void c_rotate(pgVector2* a, double seta);
-int c_equal(pgVector2* a, pgVector2* b);
-pgVector2 c_project(pgVector2 l,pgVector2 p);
+/**
+ * Python C API export hook
+ *
+ * @param c_api Pointer to the C API array.
+ */
+void PyMath_ExportCAPI (void **c_api);
 
 #endif
-
-

include/pgWorldObject.h

-#ifndef _PYGAME_PHYSICS_WORLD_
-#define _PYGAME_PHYSICS_WORLD_
+/*
+  pygame physics - Pygame physics module
 
+  Copyright (C) 2008 Zhang Fan
 
-#include <Python.h>
-#include "pgAABBBox.h"
-#include "pgDeclare.h"
+  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.
 
-struct _pgWorldObject 
-{
-	PyObject_HEAD
+  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.
 
-	PyObject*	bodyList;
-	PyObject*	jointList;
-	PyObject*	contactList;
+  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
+*/
 
-	Py_complex		vecGravity;
-	double			fDamping;
+#ifndef _PHYSICS_WORLD_H_
+#define _PHYSICS_WORLD_H_
 
-	double			fStepTime;
-	double			fTotalTime;
-	pgAABBBox		worldBox;
+/**
+ * Python C API export hook
+ *
+ * @param c_api Pointer to the C API array.
+ */
+void PyWorldObject_ExportCAPI (void **c_api);
 
-};
-
-pgWorldObject* PG_WorldNew();
-void	PG_WorldDestroy(pgWorldObject* world);
-
-void	PG_Update(pgWorldObject* world, double stepTime);
-int		PG_AddBodyToWorld(pgWorldObject* world, pgBodyObject* body);
-int		PG_RemoveBodyFromWorld(pgWorldObject* world, pgBodyObject* body);
-int		PG_AddJointToWorld(pgWorldObject* world, pgJointObject* joint);
-int		PG_RemoveJointFromWorld(pgWorldObject* world, pgJointObject* joint);
-
-
-#endif
+#endif /* _PHYSICS_JOINT_H_ */

include/pgphysics.h

+/*
+  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
+*/
+
+#ifndef _PHYSICS_H_
+#define _PHYSICS_H_
+
+#include <Python.h>
+
+#ifndef ABS
+#define ABS(x) ( ((x) < 0) ?  -(x) : (x) )
+#endif
+#ifndef MAX
+#define MAX(x, y) ( ((x) > (y)) ? (x) : (y) )
+#endif
+#ifndef MIN
+#define MIN(x, y) ( ((x) < (y)) ? (x) : (y) )
+#endif
+
+/**
+ * Zero tolerance constants for vector calculations.
+ */
+#define ZERO_EPSILON 1e-6
+#define RELATIVE_ZERO 1e-6
+
+#ifndef M_PI
+#define M_PI 3.1415926535897932384626433832795
+#endif
+
+/**
+ * Checks whether the passed double/float value is near zero.
+ *
+ * @param num The value to check.
+ */
+#define IS_NEAR_ZERO(num) (fabs(num) <= ZERO_EPSILON)
+
+/**
+ * 2D vector definition.
+ */
+typedef Py_complex PyVector2;
+
+#define PyVector2_Check(x) (PyObject_TypeCheck(op, &PyComplex_Type))
+#define PyVector2_CheckExact(x) ((op)->ob_type == &PyComplex_Type)
+
+#define PyVector2_Set(vec, x, y) \
+    (vec).real = (x);          \
+    (vec).imag = (y);
+
+#define PyVector2_GetLengthSquare(x) ((x).real * (x).real + (x).imag * (x).imag)
+#define PyVector2_GetLength(x) (sqrt(PyVector2_GetLengthSquare(x)))
+#define PyVector2_Dot(x, y) ((x).real * (y).real + (x).imag * (y).imag)
+#define PyVector2_Cross(x, y) ((x).real * (y).imag - (x).imag * (y).real)
+
+#define PyVector2_Normalize(x)                  \
+{                                               \
+    double __pg_tmp = PyVector2_GetLength(*(x)); \
+    (x)->real /=  __pg_tmp;                      \
+    (x)->imag /=  __pg_tmp;                      \
+}
+
+#define PyVector2_Rotate(x, a)                      \
+{                                                   \
+    double __pg_x = (x)->real;                       \
+    double __pg_y = (x)->imag;                       \
+    (x)->real = __pg_x * cos(a) - __pg_y * sin(a);   \
+    (x)->imag = __pg_x * sin(a) + __pg_y * cos(a);   \
+}
+
+#define PHYSICS_MATH_FIRSTSLOT 0
+#define PHYSICS_MATH_NUMSLOTS 9
+#ifndef PHYSICS_MATH_INTERNAL
+#define PyMath_IsNearEqual                                              \
+    (*(int(*)(double,double))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+0])
+#define PyMath_LessEqual                                                \
+    (*(int(*)(double,double))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+1])
+#define PyMath_MoreEqual                                                \
+    (*(int(*)(double,double))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+2])
+#define PyVector2_Equal                                                 \
+    (*(int(*)(PyVector2,PyVector2))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+3])
+#define PyVector2_MultiplyWithReal                                                 \
+    (*(PyVector2(*)(PyVector2,double))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+4])
+#define PyVector2_DivideWithReal                                                 \
+    (*(PyVector2(*)(PyVector2,double))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+5])
+#define PyVector2_fCross                                                 \
+    (*(PyVector2(*)(double,PyVector2))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+6])
+#define PyVector2_Crossf                                                 \
+    (*(PyVector2(*)(PyVector2,double))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+7])
+#define PyVector2_Project                                                 \
+    (*(PyVector2(*)(PyVector2,PyVector2))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+8])
+
+#endif /* PYGAME_MATH_INTERNAL */
+
+/**
+ * TODO
+ */
+typedef struct
+{
+    double left;
+    double bottom;
+    double right;
+    double top;
+} AABBBox;
+
+/**
+ * TODO
+ */
+typedef struct
+{
+    PyObject_HEAD
+
+    double    fMass;
+    PyVector2 vecLinearVelocity;
+    double    fAngleVelocity;
+    int       bStatic;
+    
+    PyVector2 vecPosition;
+    double    fRotation;
+    PyVector2 vecImpulse;
+    PyVector2 vecForce;
+    double    fTorque;
+    
+    double    fRestitution;
+    double    fFriction;
+    
+    PyObject* shape;
+    
+    PyVector2 cBiasLV;
+    double    cBiasW;
+
+} PyBodyObject;
+
+#define PHYSICS_BODY_FIRSTSLOT (PHYSICS_MATH_FIRSTSLOT + PHYSICS_MATH_NUMSLOTS)
+#define PHYSICS_BODY_NUMSLOTS 2
+#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])
+#endif /* PYGAME_BODY_INTERNAL */
+
+/**
+ * TODO
+ */
+typedef struct _PyJointObject PyJointObject;
+struct _PyJointObject
+{
+    PyObject_HEAD
+
+    PyObject* body1;
+    PyObject* body2;
+    int       isCollideConnect;
+    void      (*SolveConstraintPosition)(PyJointObject* joint,double stepTime);
+    void      (*SolveConstraintVelocity)(PyJointObject* joint,double stepTime);
+    void      (*Destroy)(PyJointObject* joint);
+};
+
+/**
+ * TODO
+ */
+typedef struct
+{
+    PyJointObject joint;
+    double        distance;
+    PyVector2     anchor1;
+    PyVector2     anchor2;
+} PyDistanceJointObject;
+
+#define PHYSICS_JOINT_FIRSTSLOT \
+    (PHYSICS_BODY_FIRSTSLOT + PHYSICS_BODY_NUMSLOTS)
+#define PHYSICS_JOINT_NUMSLOTS 4
+#ifndef PHYSICS_JOINT_INTERNAL
+#define PyJoint_Check(x)                                                \
+    (PyObject_TypeCheck(x,                                              \
+        (PyTypeObject*)PyPhysics_C_API[PHYSICS_JOINT_FIRSTSLOT+0]))
+#define PyJoint_New \
+    (*(PyObject*(*)(void))PyPhysics_C_API[PHYSICS_JOINT_FIRSTSLOT+1])
+#define PyDistanceJoint_Check(x)                                        \
+    (PyObject_TypeCheck(x,                                              \
+        (PyTypeObject*)PyPhysics_C_API[PHYSICS_JOINT_FIRSTSLOT+2]))
+#define PyDistanceJoint_New \
+    (*(PyObject*(*)(void))PyPhysics_C_API[PHYSICS_JOINT_FIRSTSLOT+3])
+#endif /* PYGAME_JOINT_INTERNAL */
+
+/**
+ * TODO
+ */
+typedef enum
+{
+    ST_RECT,
+    ST_CIRCLE
+} ShapeType;
+
+/**
+ * TODO
+ */
+typedef struct _PyShapeObject PyShapeObject;
+struct _PyShapeObject
+{
+    PyObject_HEAD
+
+    AABBBox   box;
+    ShapeType type;
+    double    rInertia; //Rotor inertia  
+
+    // virtual functions
+    int       (*Collision)(PyBodyObject* selfBody, PyBodyObject* incidBody,
+                           PyObject* contactList);
+    void      (*UpdateAABB)(PyBodyObject* body);
+};
+
+/**
+ * TODO
+ */
+typedef struct
+{
+    PyShapeObject shape;
+
+    PyVector2 bottomleft;
+    PyVector2 bottomright;
+    PyVector2 topright;
+    PyVector2 topleft;
+} PyRectShape;
+
+#define PHYSICS_SHAPE_FIRSTSLOT \
+    (PHYSICS_JOINT_FIRSTSLOT + PHYSICS_JOINT_NUMSLOTS)
+#define PHYSICS_SHAPE_NUMSLOTS 4
+#ifndef PHYSICS_SHAPE_INTERNAL
+#define PyShape_Check(x)                                                \
+    (PyObject_TypeCheck(x,                                              \
+        (PyTypeObject*)PyPhysics_C_API[PHYSICS_SHAPE_FIRSTSLOT+0]))
+#define PyShape_New \
+    (*(PyObject*(*)(void))PyPhysics_C_API[PHYSICS_SHAPE_FIRSTSLOT+1])
+#define PyRectShape_Check(x)                                            \
+    (PyObject_TypeCheck(x,                                              \
+        (PyTypeObject*)PyPhysics_C_API[PHYSICS_SHAPE_FIRSTSLOT+2]))
+#define PyRectShape_New \
+    (*(PyObject*(*)(double,double,double))PyPhysics_C_API[PHYSICS_SHAPE_FIRSTSLOT+3])
+#endif /* PYGAME_SHAPE_INTERNAL */
+
+
+/**
+ * TODO
+ */
+typedef struct
+{
+    PyObject_HEAD
+    
+    PyObject*  bodyList;
+    PyObject*  jointList;
+    PyObject*  contactList;
+
+    PyVector2  vecGravity;
+    double     fDamping;
+
+    double     fStepTime;
+    double     fTotalTime;
+    AABBBox    worldBox;
+
+} PyWorldObject;
+
+#define PHYSICS_WORLD_FIRSTSLOT \
+    (PHYSICS_SHAPE_FIRSTSLOT + PHYSICS_SHAPE_NUMSLOTS)
+#define PHYSICS_WORLD_NUMSLOTS 6
+#ifndef PHYSICS_WORLD_INTERNAL
+#define PyWorld_Check(x)                                                \
+    (PyObject_TypeCheck(x,                                              \
+        (PyTypeObject*)PyPhysics_C_API[PHYSICS_WORLD_FIRSTSLOT+0])
+#define PyWorld_New \
+    (*(PyObject*(*)(void))PyPhysics_C_API[PHYSICS_WORLD_FIRSTSLOT+1])
+#define PyWorld_AddBody \
+    (*(PyObject*(*)(PyObject*,PyObject*))PyPhysics_C_API[PHYSICS_WORLD_FIRSTSLOT+2])
+#define PyWorld_RemoveBody \
+    (*(PyObject*(*)(PyObject*,PyObject*))PyPhysics_C_API[PHYSICS_WORLD_FIRSTSLOT+3])
+#define PyWorld_AddJoint \
+    (*(PyObject*(*)(PyObject*,PyObject*))PyPhysics_C_API[PHYSICS_WORLD_FIRSTSLOT+4])
+#define PyWorld_RemoveJoint \
+    (*(PyObject*(*)(PyObject*,PyObject*))PyPhysics_C_API[PHYSICS_WORLD_FIRSTSLOT+5])
+#define PyWorld_Update \
+    (*(void(*)(PyObject*,double))PyPhysics_C_API[PHYSICS_WORLD_FIRSTSLOT+6])
+    
+#endif /* PYGAME_WORLD_INTERNAL */
+
+/**
+ * C API slots.
+ */
+static void **PyPhysics_C_API;
+#define PHYSICS_API_SLOTS (PHYSICS_WORLD_FIRSTSLOT + PHYSICS_WORLD_NUMSLOTS)
+#define PHYSICS_CAPI_ENTRY "_PHYSICS_C_API"
+
+static int import_physics (void)
+{
+    PyObject *_module = PyImport_ImportModule ("physics");
+    if (_module != NULL)
+    {
+        PyObject *_capi = PyObject_GetAttrString(_module, PHYSICS_CAPI_ENTRY);
+        if (!PyCObject_Check (_capi))
+        {
+            Py_DECREF (_module);
+            return -1;
+        }
+        PyPhysics_C_API = (void**) PyCObject_AsVoidPtr (_capi);
+        Py_DECREF (_capi);
+        return 0;
+    }
+    return -1;
+}
+
+#ifndef PyMODINIT_FUNC	/* declarations for DLL import/export */
+#define PyMODINIT_FUNC void
+#endif
+PyMODINIT_FUNC initphysics (void);
+
+#endif //_PYGAME_PHYSICS_H_
 #!/usr/bin/env python
 
 from distutils.core import setup, Extension
-import os, glob
+import os, glob, sys
 
 
 def get_c_files ():
 
 if __name__ == "__main__":
 
+    warn_flags = ["-W", "-Wall", "-Wpointer-arith", "-Wcast-qual",
+                  "-Winline", "-Wcast-align", "-Wconversion",
+                  "-Wstrict-prototypes", "-Wmissing-prototypes",
+                  "-Wmissing-declarations", "-Wnested-externs",
+                  "-Wshadow", "-Wredundant-decls"
+                  ]
+    compile_args = [ "-std=c99", "-g"]
+    if True:
+        compile_args += warn_flags
+
     extphysics = Extension ("physics", sources = get_c_files (),
                             include_dirs = [ "include" ],
-                            extra_compile_args=["-g", "-W", "-Wall"])
+                            define_macros = [("PHYSICS_INTERNAL", "1")],
+                            extra_compile_args = compile_args)
 
     setupdata = {
         "name" : "physics",
         "version" : "0.0.1",
-        "description" : "blabla",
+        "author" : "Zhang Fan",
+        "url" : "http://www.pygame.org",
+        "description" : "2D physics module",
         "license": "LGPL",
         "ext_modules" : [ extphysics ],
+        "headers" : [ "include/pgphysics.h" ]
         }
     setup (**setupdata)
+/*
+  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
+*/
+
+#include <float.h>
 #include "pgAABBBox.h"
-#include <float.h>
 
-
-pgAABBBox PG_GenAABB(double left, double right, double bottom, double top)
+AABBBox AABB_Gen(double left, double right, double bottom, double top)
 {
-	pgAABBBox box;
-	box.left = left;
-	box.right = right;
-	box.bottom = bottom;
-	box.top = top;
-	return box;
+    AABBBox box;
+    box.left = left;
+    box.right = right;
+    box.bottom = bottom;
+    box.top = top;
+    return box;
 }
 
-void PG_AABBClear(pgAABBBox* box)
+void AABB_Clear(AABBBox* box)
 {
-	box->left = DBL_MAX;
-	box->bottom = DBL_MAX;
-	box->top = -DBL_MAX;
-	box->right = -DBL_MAX;
+    box->left = DBL_MAX;
+    box->bottom = DBL_MAX;
+    box->top = -DBL_MAX;
+    box->right = -DBL_MAX;
 }
 
-void PG_AABBExpandTo(pgAABBBox* box, pgVector2* p)
+void AABB_ExpandTo(AABBBox* box, PyVector2* p)
 {
-	box->left = MIN(box->left, p->real);
-	box->right = MAX(box->right, p->real);
-	box->bottom = MIN(box->bottom, p->imag);
-	box->top = MAX(box->top, p->imag);
+    box->left = MIN(box->left, p->real);
+    box->right = MAX(box->right, p->real);
+    box->bottom = MIN(box->bottom, p->imag);
+    box->top = MAX(box->top, p->imag);
 }
 
-int PG_IsOverlap(pgAABBBox* boxA, pgAABBBox* boxB, double eps)
+int AABB_IsOverlap(AABBBox* boxA, AABBBox* boxB, double eps)
 {
-	double from_x, from_y, to_x, to_y;
-	from_x = MAX(boxA->left, boxB->left);
-	from_y = MAX(boxA->bottom, boxB->bottom);
-	to_x = MIN(boxA->right, boxB->right);
-	to_y = MIN(boxA->top, boxB->top);
-	return from_x - eps <= to_x + eps && from_y - eps <= to_y + eps;
+    double from_x, from_y, to_x, to_y;
+    from_x = MAX(boxA->left, boxB->left);
+    from_y = MAX(boxA->bottom, boxB->bottom);
+    to_x = MIN(boxA->right, boxB->right);
+    to_y = MIN(boxA->top, boxB->top);
+    return from_x - eps <= to_x + eps && from_y - eps <= to_y + eps;
 }
 
-int PG_IsIn(pgVector2* p, pgAABBBox* box, double eps)
+int AABB_IsIn(PyVector2* p, AABBBox* box, double eps)
 {
-	return box->left - eps < p->real && p->real < box->right + eps
-		&& box->bottom - eps < p->imag && p->imag < box->top + eps;
+    return box->left - eps < p->real && p->real < box->right + eps
+        && box->bottom - eps < p->imag && p->imag < box->top + eps;
 }

src/pgBodyObject.c

+/*
+  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_BODY_INTERNAL
+#include "pgDeclare.h"
+#include "pgphysics.h"
+#include "pgHelpFunctions.h"
+#include "pgVector2.h"
 #include "pgBodyObject.h"
-#include "pgWorldObject.h"
-#include "pgVector2.h"
-#include "pgShapeObject.h"
-#include "pgHelpFunctions.h"
-#include <structmember.h>
 
-extern PyTypeObject pgBodyType;
+static void _BodyInit(PyBodyObject* body);
+static PyObject* _BodyNew(PyTypeObject *type, PyObject *args, PyObject *kwds);
+static void _BodyDestroy(PyBodyObject* body);
 
-void PG_Bind_RectShape(pgBodyObject* body, double width, double height, double seta)
+static PyObject* _Body_getVelocity(PyBodyObject* body,void* closure);
+static int _Body_setVelocity(PyBodyObject* body,PyObject* value,void* closure);
+static PyObject* _Body_getPosition(PyBodyObject* body,void* closure);
+static int _Body_setPosition(PyBodyObject* body,PyObject* value,void* closure);
+static PyObject* _Body_getForce(PyBodyObject* body,void* closure);
+static int _Body_setForce(PyBodyObject* body,PyObject* value,void* closure);
+
+static PyObject* _Body_getMass (PyBodyObject* body,void* closure);
+static int _Body_setMass(PyBodyObject* body,PyObject* value,void* closure);
+static PyObject* _Body_getRotation (PyBodyObject* body,void* closure);
+static int _Body_setRotation (PyBodyObject* body,PyObject* value,void* closure);
+static PyObject* _Body_getTorque (PyBodyObject* body,void* closure);
+static int _Body_setTorque (PyBodyObject* body,PyObject* value,void* closure);
+static PyObject* _Body_getRestitution (PyBodyObject* body,void* closure);
+static int _Body_setRestitution (PyBodyObject* body,PyObject* value,
+    void* closure);
+static PyObject* _Body_getFriction (PyBodyObject* body,void* closure);
+static int _Body_setFriction (PyBodyObject* body,PyObject* value,void* closure);
+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);
+
+
+static PyMethodDef _Body_methods[] = {
+    { "get_points",_Body_getPointList,METH_VARARGS,"" },
+    { NULL, NULL, 0, NULL }   /* Sentinel */
+};
+
+static PyGetSetDef _Body_getseters[] = {
+    { "mass", (getter) _Body_getMass, (setter) _Body_setMass, "Mass",
+      NULL },
+    { "shape",(getter)_Body_getShape,(setter)_Body_setShape,"Shape", NULL},
+    { "rotation", (getter) _Body_getRotation, (setter) _Body_setRotation,
+      "Rotation", NULL },
+    { "torque", (getter) _Body_getTorque, (setter) _Body_setTorque,
+      "Torque", NULL },
+    { "restitution", (getter) _Body_getRestitution,
+      (setter) _Body_setRestitution, "Restitution", NULL },
+    {"friction", (getter) _Body_getFriction, (setter) _Body_setFriction,
+     "Friction", NULL },
+
+    {"velocity",(getter)_Body_getVelocity,(setter)_Body_setVelocity,"velocity",NULL},
+    {"position",(getter)_Body_getPosition,(setter)_Body_setPosition,"position",NULL},
+    {"force",(getter)_Body_getForce,(setter)_Body_setForce,"force",NULL},
+    {"static",(getter)_Body_getBStatic,(setter)_Body_setBStatic,"whether static",NULL},
+    { NULL, NULL, NULL, NULL, NULL}
+};
+
+PyTypeObject PyBody_Type =
 {
-	if(body->shape == NULL)
-		body->shape = PG_RectShapeNew(body, width, height, seta);
-	else
-	{
-		Py_DECREF(body->shape);
-		body->shape = PG_RectShapeNew(body, width, height, seta);
-	}
+    PyObject_HEAD_INIT(NULL)
+    0,
+    "physics.Body",             /* tp_name */
+    sizeof(PyBodyObject),       /* tp_basicsize */
+    0,                          /* tp_itemsize */
+    (destructor)_BodyDestroy,   /* 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*/
+    "",                         /* tp_doc */
+    0,                          /* tp_traverse */
+    0,                          /* tp_clear */
+    0,                          /* tp_richcompare */
+    0,                          /* tp_weaklistoffset */
+    0,                          /* tp_iter */
+    0,                          /* tp_iternext */
+    _Body_methods,              /* tp_methods */
+    0,                          /* tp_members */
+    _Body_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 */
+    _BodyNew,                   /* 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 */
+};
+
+static void _BodyInit(PyBodyObject* body)
+{
+    body->fAngleVelocity = 0.0;
+    body->fFriction = 0.0;
+    body->fMass = 1.0;
+    body->fRestitution = 1.0;
+    body->fRotation = 0.0;
+    body->fTorque = 0.0;
+    body->shape = NULL;
+    body->bStatic = 0;
+    PyVector2_Set(body->vecForce,0.0,0.0);
+    PyVector2_Set(body->vecImpulse,0.0,0.0);
+    PyVector2_Set(body->vecLinearVelocity,0.0,0.0);
+    PyVector2_Set(body->vecPosition,0.0,0.0);
 }
 
-void PG_FreeUpdateBodyVel(pgWorldObject* world,pgBodyObject* body, double dt)
+static PyObject* _BodyNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-	pgVector2 totalF;
-	if(body->bStatic) return;
-
-	totalF = c_sum(body->vecForce, c_mul_complex_with_real(world->vecGravity,
-		body->fMass));
-	body->vecLinearVelocity = c_sum(body->vecLinearVelocity, 
-		c_mul_complex_with_real(totalF, dt/body->fMass));
+    //TODO: parse args later on
+    PyBodyObject* op = (PyBodyObject*)type->tp_alloc(type, 0);
+    _BodyInit(op);
+    return (PyObject*)op;
 }
 
-//void PG_FreeUpdateBodyPos(pgWorldObject* world,pgBodyObject* body,double dt)
-//{
-//	pgVector2 v;
-//	double w;
-//
-//	if(body->bStatic) return;
-//	
-//	v = c_sum(body->vecLinearVelocity, body->cBiasLV);
-//	w = body->fAngleVelocity + body->cBiasW;
-//	body->vecPosition = c_sum(body->vecPosition, 
-//		c_mul_complex_with_real(v, dt));
-//	body->fRotation += w*dt;
-//}
+static void _BodyDestroy(PyBodyObject* body)
+{
+    /*
+     * DECREF anything related to the Body, such as the lists and
+     * release any other memory hold by it.
+     */
 
-void PG_FreeUpdateBodyPos(pgBodyObject* body,double dt)
-{
-	if(body->bStatic) return;
-
-	body->vecPosition = c_sum(body->vecPosition, 
-		c_mul_complex_with_real(body->vecLinearVelocity, dt));
-	body->fRotation += body->fAngleVelocity*dt;
+    //delete shape
+    Py_XDECREF(body->shape);
+    body->ob_type->tp_free((PyObject*)body);
 }
 
-void PG_CorrectBodyPos(pgBodyObject* body, double dt)
-{
-	if(body->bStatic) return;
-
-	body->vecPosition = c_sum(body->vecPosition, 
-		c_mul_complex_with_real(body->cBiasLV, dt));
-	body->fRotation += body->cBiasW*dt;
-}
-
-//void PG_FreeUpdateBodyPos(pgWorldObject* world,pgBodyObject* body,double dt)
-//{
-//	pgVector2 v;
-//	double w;
-//
-//	if(body->bStatic) return;
-//
-//	v = c_sum(body->vecLinearVelocity, body->cBiasLV);
-//	w = body->fAngleVelocity + body->cBiasW;
-//	body->vecPosition = c_sum(body->vecPosition, 
-//		c_mul_complex_with_real(v, dt));
-//	body->fRotation += w*dt;
-//}
-
-void PG_BodyInit(pgBodyObject* body)
-{
-	body->fAngleVelocity = 0.0;
-	body->fFriction = 0.0;
-	body->fMass = 1.0;
-	body->fRestitution = 1.0;
-	body->fRotation = 0.0;
-	body->fTorque = 0.0;
-	body->shape = NULL;
-	body->bStatic = 0;
-	PG_Set_Vector2(body->vecForce,0.0,0.0);
-	PG_Set_Vector2(body->vecImpulse,0.0,0.0);
-	PG_Set_Vector2(body->vecLinearVelocity,0.0,0.0);
-	PG_Set_Vector2(body->vecPosition,0.0,0.0);
-}
-
-PyObject* _PG_BodyNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
-{
-	//TODO: parse args later on
-	pgBodyObject* op;
-	if(PyType_Ready(type)==-1) return NULL;
-	//op = (pgBodyObject*)type->tp_alloc(type, 0);
-	op = PyObject_New(pgBodyObject,type);
-	Py_INCREF(op);
-	PG_BodyInit(op);
-	return (PyObject*)op;
-}
-
-void PG_BodyDestroy(pgBodyObject* body)
-{
-	/*
-	* DECREF anything related to the Body, such as the lists and
-	* release any other memory hold by it.
-	*/
-
-	//delete shape
-	Py_DECREF(body->shape);
-	body->ob_type->tp_free((PyObject*)body);
-}
-
-
-
-pgBodyObject* PG_BodyNew()
-{
-	return (pgBodyObject*) _PG_BodyNew(&pgBodyType, NULL, NULL);
-}
-
-pgVector2 PG_GetGlobalPos(pgBodyObject* body, pgVector2* local_p)
-{
-	pgVector2 ans;
-	
-	ans = *local_p;
-	c_rotate(&ans, body->fRotation);
-	ans = c_sum(ans, body->vecPosition);
-
-	return ans;
-}
-
-
-pgVector2 PG_GetRelativePos(pgBodyObject* bodyA, pgBodyObject* bodyB, pgVector2* p_in_B)
-{
-	pgVector2 trans, p_in_A;
-	double rotate;
-	
-	trans = c_diff(bodyB->vecPosition, bodyA->vecPosition);
-	c_rotate(&trans, -bodyA->fRotation);
-	rotate = bodyA->fRotation - bodyB->fRotation;
-	p_in_A = *p_in_B;
-	c_rotate(&p_in_A, -rotate);
-	p_in_A = c_sum(p_in_A, trans);
-	
-	return p_in_A;
-}
-
-pgVector2 PG_GetLocalPointVelocity(pgBodyObject* body,pgVector2 localPoint)
-{
-	pgVector2 vel = c_fcross(body->fAngleVelocity,localPoint);
-	return c_sum(vel,body->vecLinearVelocity);
-}
 
 //============================================================
 //getter and setter functions
 
 //velocity
-static PyObject* _pgBody_getVelocity(pgBodyObject* body,void* closure)
+static PyObject* _Body_getVelocity(PyBodyObject* body,void* closure)
 {
     return Py_BuildValue ("(ff)", body->vecLinearVelocity.real,
         body->vecLinearVelocity.imag);
 }
 
-static int _pgBody_setVelocity(pgBodyObject* body,PyObject* value,void* closure)
+static int _Body_setVelocity(PyBodyObject* body,PyObject* value,void* closure)
 {
     PyObject *item;
     double real, imag;
 }
 
 //position
-static PyObject* _pgBody_getPosition(pgBodyObject* body,void* closure)
+static PyObject* _Body_getPosition(PyBodyObject* body,void* closure)
 {
     return Py_BuildValue ("(ff)", body->vecPosition.real,
         body->vecPosition.imag);
 }
 
-static int _pgBody_setPosition(pgBodyObject* body,PyObject* value,void* closure)
+static int _Body_setPosition(PyBodyObject* body,PyObject* value,void* closure)
 {
     PyObject *item;
     double real, imag;
 }
 
 //force
-static PyObject* _pgBody_getForce(pgBodyObject* body,void* closure)
+static PyObject* _Body_getForce(PyBodyObject* body,void* closure)
 {
     return Py_BuildValue ("(ff)", body->vecForce.real, body->vecForce.imag);
 }
 
-
-static int _pgBody_setForce(pgBodyObject* body,PyObject* value,void* closure)
+static int _Body_setForce(PyBodyObject* body,PyObject* value,void* closure)
 {
     PyObject *item;
     double real, imag;
 /**
  * Getter for retrieving the mass of the passed body.
  */
-static PyObject* _pgBody_getMass (pgBodyObject* body,void* closure)
+static PyObject* _Body_getMass (PyBodyObject* body,void* closure)
 {
     return PyFloat_FromDouble (body->fMass);
 }
 /**
  * Sets the mass of the passed body.
  */
-static int _pgBody_setMass(pgBodyObject* body,PyObject* value,void* closure)
+static int _Body_setMass(PyBodyObject* body,PyObject* value,void* closure)
 {
     if (PyNumber_Check (value))
     {
 /**
  * Getter for retrieving the rotation of the passed body.
  */
-static PyObject* _pgBody_getRotation (pgBodyObject* body,void* closure)
+static PyObject* _Body_getRotation (PyBodyObject* body,void* closure)
 {
     return PyFloat_FromDouble (body->fRotation);
 }
 /**
  * Sets the rotation of the passed body.
  */
-static int _pgBody_setRotation(pgBodyObject* body,PyObject* value,void* closure)
+static int _Body_setRotation(PyBodyObject* body,PyObject* value,void* closure)
 {
     if (PyNumber_Check (value))
     {
 /**
  * Getter for retrieving the torque of the passed body.
  */
-static PyObject* _pgBody_getTorque (pgBodyObject* body,void* closure)
+static PyObject* _Body_getTorque (PyBodyObject* body,void* closure)
 {
     return PyFloat_FromDouble (body->fTorque);
 }
 /**
  * Sets the torque of the passed body.
  */
-static int _pgBody_setTorque (pgBodyObject* body,PyObject* value,void* closure)
+static int _Body_setTorque (PyBodyObject* body,PyObject* value,void* closure)
 {
     if (PyNumber_Check (value))
     {
 /**
  * Getter for retrieving the restitution of the passed body.
  */
-static PyObject* _pgBody_getRestitution (pgBodyObject* body,void* closure)
+static PyObject* _Body_getRestitution (PyBodyObject* body,void* closure)
 {
     return PyFloat_FromDouble (body->fRestitution);
 }
 /**
  * Sets the restitution of the passed body.
  */
-static int _pgBody_setRestitution (pgBodyObject* body,PyObject* value,void* closure)
+static int _Body_setRestitution (PyBodyObject* body,PyObject* value,
+    void* closure)
 {
     if (PyNumber_Check (value))
     {
 /**
  * Getter for retrieving the friction of the passed body.
  */
-static PyObject* _pgBody_getFriction (pgBodyObject* body,void* closure)
+static PyObject* _Body_getFriction (PyBodyObject* body,void* closure)
 {
     return PyFloat_FromDouble (body->fFriction);
 }
 /**
  * Sets the friction of the passed body.
  */
-static int _pgBody_setFriction (pgBodyObject* body,PyObject* value,void* closure)
+static int _Body_setFriction (PyBodyObject* body,PyObject* value,void* closure)
 {
     if (PyNumber_Check (value))
     {
 }
 
 /**
-* Getter for retrieving the bStatic of the passed body.
-*/
-static PyObject* _pgBody_getBStatic (pgBodyObject* body,void* closure)
+ * Getter for retrieving the bStatic of the passed body.
+ */
+static PyObject* _Body_getBStatic (PyBodyObject* body,void* closure)
 {
-	return PyInt_FromLong (body->bStatic);
+    return PyInt_FromLong (body->bStatic);
 }
 
 /**
-* Sets the bStatic of the passed body.
-*/
-static int _pgBody_setBStatic (pgBodyObject* body,PyObject* value,void* closure)
+ * Sets the bStatic of the passed body.
+ */
+static int _Body_setBStatic (PyBodyObject* body,PyObject* value,void* closure)
 {
-	if (PyBool_Check (value))
-	{
-            body->bStatic = (value == Py_True) ? 1 : 0;
-            return 0;
+    if (PyBool_Check (value))
+    {
+        body->bStatic = (value == Py_True) ? 1 : 0;
+        return 0;
 
-	}
-	PyErr_SetString (PyExc_TypeError, "static must be a bool");
-	return -1;
+    }
+    PyErr_SetString (PyExc_TypeError, "static must be a bool");
+    return -1;
 }
 
-static PyObject* _pgBody_bindRectShape(PyObject* body,PyObject* args)
+/**
+ * Getter for retrieving the shape of the passed body.
+ */
+static PyObject* _Body_getShape (PyBodyObject* body,void* closure)
 {
-	double width,height,seta;
-	if (!PyArg_ParseTuple(args,"ddd",&width,&height,&seta))
-	{
-		PyErr_SetString(PyExc_ValueError,"parameters are wrong");
-		return NULL;
-	}
-	else
-	{
-		PG_Bind_RectShape((pgBodyObject*)body,width,height,seta);
-		if (((pgBodyObject*)body)->shape == NULL)
-		{
-			PyErr_SetString(PyExc_ValueError,"shape binding is failed");
-			return NULL;
-		}
-		else
-		{
-			Py_RETURN_NONE;
-			//return ((pgBodyObject*)body)->shape;
-		}
-	}
+    if (!body->shape)
+        Py_RETURN_NONE;
+
+    Py_INCREF (body->shape);
+    return body->shape;
 }
 
+/**
+ * Sets the shape of the passed body.
+ */
+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;
 
-
-static PyObject * _pg_getPointListFromBody(PyObject *self, PyObject *args)
-{
-	pgBodyObject* body = (pgBodyObject*)self;
-	int i;
-	PyObject* list;
-
-	/*if (!PyArg_ParseTuple(args,"O",&body))
-	{
-		PyErr_SetString(PyExc_ValueError,"arg is not body type");
-		return NULL;
-	}
-	else*/
-	{
-		if (body->shape == NULL)
-		{
-			PyErr_SetString(PyExc_ValueError,"Shape is NULL");
-			return NULL;
-		}
-		list = PyList_New(4);
-		for (i = 0;i < 4;i++)
-		{
-			pgVector2* pVertex = &(((pgRectShape*)(body->shape))->point[i]);
-			pgVector2 golVertex = PG_GetGlobalPos(body,pVertex);
-			PyObject* tuple = FromPhysicsVector2ToPoint(golVertex);
-			PyList_SetItem(list,i,tuple);
-		}
-		return (PyObject*)list;
-	}
+    // 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)
+    {
+        PyRectShape* rsh = (PyRectShape*) 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;
 }
 
-//===============================================================
+static PyObject *_Body_getPointList(PyObject *self, PyObject *args)
+{
+    PyBodyObject* body = (PyBodyObject*)self;
+    PyObject* list;
+    PyVector2* pVertex;
+    PyVector2 golVertex;
+    PyObject* tuple;
 
-static PyMethodDef _pgBody_methods[] = {
-	{"bind_rect_shape",_pgBody_bindRectShape,METH_VARARGS,""},
-	{"get_point_list",_pg_getPointListFromBody,METH_VARARGS,""	},
-    {NULL, NULL, 0, NULL}   /* Sentinel */
-};
+    if (!body->shape)
+    {
+        Py_RETURN_NONE;
+    }
 
-static PyGetSetDef _pgBody_getseters[] = {
-    { "mass", (getter) _pgBody_getMass, (setter) _pgBody_setMass, "Mass",
-      NULL },
-    { "rotation", (getter) _pgBody_getRotation, (setter) _pgBody_setRotation,
-      "Rotation", NULL },
-    { "torque", (getter) _pgBody_getTorque, (setter) _pgBody_setTorque,
-      "Torque", NULL },
-    { "restitution", (getter) _pgBody_getRestitution,
-      (setter) _pgBody_setRestitution, "Restitution", NULL },
-    {"friction", (getter) _pgBody_getFriction, (setter) _pgBody_setFriction,
-     "Friction", NULL },
+    /* TODO: shapes */
+    list = PyList_New (4);
 
-    {"velocity",(getter)_pgBody_getVelocity,(setter)_pgBody_setVelocity,"velocity",NULL},
-    {"position",(getter)_pgBody_getPosition,(setter)_pgBody_setPosition,"position",NULL},
-    {"force",(getter)_pgBody_getForce,(setter)_pgBody_setForce,"force",NULL},
-    {"static",(getter)_pgBody_getBStatic,(setter)_pgBody_setBStatic,"whether static",NULL},
-    { NULL, NULL, NULL, NULL, NULL}
-};
+    pVertex = &(((PyRectShape*)(body->shape))->bottomleft);
+    golVertex = PyBodyObject_GetGlobalPos(body,pVertex);
+    tuple = FromPhysicsVector2ToPoint(golVertex);
+    PyList_SetItem(list,0,tuple);
 
-PyTypeObject pgBodyType =
+    pVertex = &(((PyRectShape*)(body->shape))->bottomright);
+    golVertex = PyBodyObject_GetGlobalPos(body,pVertex);
+    tuple = FromPhysicsVector2ToPoint(golVertex);
+    PyList_SetItem(list,1,tuple);
+
+    pVertex = &(((PyRectShape*)(body->shape))->topright);
+    golVertex = PyBodyObject_GetGlobalPos(body,pVertex);
+    tuple = FromPhysicsVector2ToPoint(golVertex);
+    PyList_SetItem(list,2,tuple);
+
+    pVertex = &(((PyRectShape*)(body->shape))->topleft);
+    golVertex = PyBodyObject_GetGlobalPos(body,pVertex);
+    tuple = FromPhysicsVector2ToPoint(golVertex);
+    PyList_SetItem(list,3,tuple);
+
+    return list;
+}
+
+void PyBodyObject_FreeUpdateVel(PyBodyObject* body, PyVector2 gravity,
+    double dt)
 {
-	PyObject_HEAD_INIT(NULL)
-	0,
-	"physics.Body",            /* tp_name */
-	sizeof(pgBodyObject),      /* tp_basicsize */
-	0,                          /* tp_itemsize */
-	(destructor)PG_BodyDestroy,/* 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*/
-	"",                         /* tp_doc */
-	0,                          /* tp_traverse */
-	0,                          /* tp_clear */
-	0,                          /* tp_richcompare */
-	0,                          /* tp_weaklistoffset */
-	0,                          /* tp_iter */
-	0,                          /* tp_iternext */
-	_pgBody_methods,		   	/* tp_methods */
-	0,                          /* tp_members */
-	_pgBody_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 */
-	_PG_BodyNew,                /* 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 */
-};
+    PyVector2 totalF;
+    if (body->bStatic)
+        return;
 
+    totalF = c_sum(body->vecForce,
+        PyVector2_MultiplyWithReal(gravity, body->fMass));
+    body->vecLinearVelocity = c_sum(body->vecLinearVelocity, 
+        PyVector2_MultiplyWithReal(totalF, dt/body->fMass));
+}
 
+void PyBodyObject_FreeUpdatePos(PyBodyObject* body,double dt)
+{
+    if (body->bStatic)
+        return;
+
+    body->vecPosition = c_sum(body->vecPosition, 
+        PyVector2_MultiplyWithReal(body->vecLinearVelocity, dt));
+    body->fRotation += body->fAngleVelocity*dt;
+}
+
+void PyBodyObject_CorrectPos(PyBodyObject* body, double dt)
+{
+    if (body->bStatic)
+        return;
+
+    body->vecPosition = c_sum(body->vecPosition, 
+        PyVector2_MultiplyWithReal(body->cBiasLV, dt));
+    body->fRotation += body->cBiasW*dt;
+}
+
+PyVector2 PyBodyObject_GetGlobalPos(PyBodyObject* body, PyVector2* local_p)
+{
+    PyVector2 ans;
+	
+    ans = *local_p;
+    PyVector2_Rotate(&ans, body->fRotation);
+    ans = c_sum(ans, body->vecPosition);
+
+    return ans;
+}
+
+PyVector2 PyBodyObject_GetRelativePos(PyBodyObject* bodyA, PyBodyObject* bodyB,
+    PyVector2* p_in_B)
+{
+    PyVector2 trans, p_in_A;
+    double rotate;
+	
+    trans = c_diff(bodyB->vecPosition, bodyA->vecPosition);
+    PyVector2_Rotate(&trans, -bodyA->fRotation);
+    rotate = bodyA->fRotation - bodyB->fRotation;
+    p_in_A = *p_in_B;
+    PyVector2_Rotate(&p_in_A, -rotate);
+    p_in_A = c_sum(p_in_A, trans);
+	
+    return p_in_A;
+}
+
+PyVector2 PyBodyObject_GetLocalPointVelocity(PyBodyObject* body,
+    PyVector2 localPoint)
+{
+    PyVector2 vel = PyVector2_fCross(body->fAngleVelocity,localPoint);
+    return c_sum(vel,body->vecLinearVelocity);
+}
+
+
+/* C API */
+static PyObject* PyBody_New(void)
+{
+    return _BodyNew(&PyBody_Type, NULL, NULL);
+}
+
+void PyBodyObject_ExportCAPI (void **c_api)
+{
+    c_api[PHYSICS_BODY_FIRSTSLOT] = &PyBody_Type;
+    c_api[PHYSICS_BODY_FIRSTSLOT + 1] = &PyBody_New;
+}

src/pgCollision.c

+/*
+  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
+*/
+
+#include <assert.h>
+#include "pgDeclare.h"
+#include "pgphysics.h"
 #include "pgCollision.h"
-#include "pgAABBBox.h"
-#include "pgShapeObject.h"
-#include "pgBodyObject.h"
-#include <assert.h>
+#include "pgVector2.h"
 
-extern PyTypeObject pgContactType;
+static int _LiangBarskey_Internal(double p, double q, double* u1, double* u2);
+static void _UpdateV(PyJointObject* joint, double step);
+static void _UpdateP(PyJointObject* joint, double step);
+static void _ContactDestroy(PyJointObject* contact);
+static PyObject* _ContactNewInternal(PyTypeObject *type, PyObject *args,
+    PyObject *kwds);
+static PyJointObject* _ContactNew(PyBodyObject* refBody,
+    PyBodyObject* incidBody);
 
 // We borrow this graph from Box2DLite
 // Box vertex and edge numbering:
 // (p1, p2) is the input line segment to be clipped (note: it's a 2d vector)
 // (ans_p1, ans_p2) is the output line segment
 //TEST: Liang-Barskey clip has been tested.
-int _LiangBarskey_Internal(double p, double q, double* u1, double* u2)
+static int _LiangBarskey_Internal(double p, double q, double* u1, double* u2)
 {
-	double val;
+    double val;
 
-	if(is_zero(p))
-	{
-		if(q < 0)
-			return 0;
-		
-		return 1;
-	}
-	
-	val = q/p;
-	
-	if(p < 0)
-		*u1 = MAX(*u1, val);
-	else
-		*u2 = MIN(*u2, val);
+    if(IS_NEAR_ZERO(p))
+    {
+        if(q < 0)
+            return 0;
+        return 1;
+    }
+    
+    val = q/p;
+    
+    if(p < 0)
+        *u1 = MAX(*u1, val);
+    else
+        *u2 = MIN(*u2, val);
 
-	return 1;