Commits

Anonymous committed f5142b2

Added body shape setter and GetGlobalPos C API.
Added type checks to C API functions.
Added PyDistanceJoint_SetAnchors C API.
Removed distance setter from DistanceJoint class.
Renamed PyRectShape to PyRectShapeObject.
Fixed internal PyJoint_Check and PyDistanceJoint_Check defines.
Fixed some ref count issues.
Fixed C tests to use the Python C API.
Added Makefile for C tests.

Comments (0)

Files changed (17)

+#include <stdio.h>
+#ifdef WIN32
+#include <windows.h>
+#endif
 #include <GL/glut.h>
 #include <math.h>
 
 
 /*-------------------------------˛âĘÔš¤žß----------------------------*/
 
+#include <physics/pgphysics.h>
 #include "pgPhysicsRenderer.h"
-#include "pgBodyObject.h"
-//#include "glTestSuite.h"
 
-static pgWorldObject* s_world = NULL;
+static PyObject* s_world = NULL;
 
 /*-------------------------------˛âĘÔşŻĘý----------------------------*/
 
-pgBodyObject* body, * body1;
+/*PyObject* body, * body1;*/
 
 //äÖČžşŻĘý
 void do_render()
 {
-	glColor3f(1.f, 1.f, 1.f);
-	PG_Update(s_world, 0.02);
-	PGT_RenderWorld(s_world);
-	//glprintf(0, 0, "Velocity of body: (%.2f, %.2f)", body->vecLinearVelocity.real, 
-	//	body->vecLinearVelocity.imag);
-	//glprintf(0, 20, "w of body: %d", body->fAngleVelocity);
+    glColor3f(1.f, 1.f, 1.f);
+    PyWorld_Update(s_world, 0.2);
+    PGT_RenderWorld((PyWorldObject*)s_world);
+    //glprintf(0, 0, "Velocity of body: (%.2f, %.2f)", body->vecLinearVelocity.real, 
+    //	body->vecLinearVelocity.imag);
+    //glprintf(0, 20, "w of body: %d", body->fAngleVelocity);
 }
 
-
-
 //keyboardĘäČëĎěÓŚ
 void keyboard (unsigned char key, int x, int y)
 {
-	switch(key)
-	{
-	case 27:
-		PG_WorldDestroy(s_world);
-		exit(0);
-		break;
-	}
+    switch(key)
+    {
+    case 27:
+        Py_DECREF(s_world);
+		Py_Finalize();
+        exit(0);
+        break;
+    }
 }
 
 
 
 void InitGL()
 {
-	glShadeModel(GL_SMOOTH);	
-	glPointSize(3.f);
-	glEnable(GL_LINE_SMOOTH);
-	glEnable(GL_POINT_SMOOTH);
-	glEnable(GL_BLEND);
-	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-	glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
-	glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
-	glLineWidth(2.5f);
+    glShadeModel(GL_SMOOTH);	
+    glPointSize(3.f);
+    glEnable(GL_LINE_SMOOTH);
+    glEnable(GL_POINT_SMOOTH);
+    glEnable(GL_BLEND);
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+    glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
+    glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
+    glLineWidth(2.5f);
 
-	glMatrixMode(GL_PROJECTION);
-	glLoadIdentity();
-	glOrtho(-WIDTH/2, WIDTH/2, -HEIGHT/2, HEIGHT/2, -1.f, 1.f);
-	glMatrixMode(GL_MODELVIEW);
-	glLoadIdentity();
+    glMatrixMode(GL_PROJECTION);
+    glLoadIdentity();
+    glOrtho(-WIDTH/2, WIDTH/2, -HEIGHT/2, HEIGHT/2, -1.f, 1.f);
+    glMatrixMode(GL_MODELVIEW);
+    glLoadIdentity();
 }
 
 
 void display(void)
 {
-	glClear(GL_COLOR_BUFFER_BIT);
-	glLoadIdentity();
-	do_render();
-	glutSwapBuffers();
+    glClear(GL_COLOR_BUFFER_BIT);
+    glLoadIdentity();
+    do_render();
+    glutSwapBuffers();
 }
 
 //Őâ¸öşŻĘýŇťżŞĘźžÍťáąťľ÷ÓĂŁŹšĘgluPerspectiveşŻĘýĂťąŘŇŞÔÚinitGLťňŐßdisplayşŻĘýŔďľ÷ÓĂ
 void reshape (int width , int height)
 {
-	if(height == 0)										
-		height = 1;										
+    if(height == 0)										
+        height = 1;										
 
-	glViewport(0,0,width,height);						
-	glMatrixMode(GL_PROJECTION);
-	glLoadIdentity();
-	glOrtho(-width/2, width/2, -height/2, height/2, -1.f, 1.f);
-	glMatrixMode(GL_MODELVIEW);
-	glLoadIdentity();									
+    glViewport(0,0,width,height);						
+    glMatrixMode(GL_PROJECTION);
+    glLoadIdentity();
+    glOrtho(-width/2, width/2, -height/2, height/2, -1.f, 1.f);
+    glMatrixMode(GL_MODELVIEW);
+    glLoadIdentity();									
 }
 
 //============================================
 
 void TestBasic1Init()
 {
-	pgBodyObject* body;
-	pgJointObject* joint;
-	pgVector2 a1,a2;
-	PG_Set_Vector2(a1,0,0);
-	PG_Set_Vector2(a2,0,100);
+    PyObject* body;
+    PyObject* joint;
+    PyVector2 a1,a2;
+    PyVector2_Set(a1,0,0);
+    PyVector2_Set(a2,0,100);
 
-	s_world = PG_WorldNew();
-	s_world->fStepTime = 0.03;
-	body = PG_BodyNew();
-	PG_Bind_RectShape(body, 20, 20, 0);
-	PG_Set_Vector2(body->vecPosition,0,0)
-	PG_Set_Vector2(body->vecLinearVelocity, 40, 0)
-	PG_AddBodyToWorld(s_world,body);
-	
-	
-	joint = PG_DistanceJointNew(body,NULL,0,100,a1,a2);
-	PG_AddJointToWorld(s_world,joint);
+    s_world = PyWorld_New();
+    ((PyWorldObject*)s_world)->fStepTime = 0.03;
+    body = PyBody_New();
+    PyBody_SetShape (body, PyRectShape_New(20, 20, 0));
+    PyVector2_Set(((PyBodyObject*)body)->vecPosition,0,0);
+    PyVector2_Set(((PyBodyObject*)body)->vecLinearVelocity, 40, 0);
+    PyWorld_AddBody(s_world,body);
+    
+    joint = PyDistanceJoint_New(body,NULL,0);
+    PyDistanceJoint_SetAnchors (joint,a1,a2);
+    PyWorld_AddJoint(s_world,joint);
 }
 //test collision
 void TestBasic2Init()
 {
-	int i = 0;
-	s_world = PG_WorldNew();
-	s_world->fStepTime = 0.03;
-	PG_Set_Vector2(s_world->vecGravity, 0, -2000.f);
+    int i = 0;
+    PyObject* body, *body1;
+    s_world = PyWorld_New();
+    ((PyWorldObject*)s_world)->fStepTime = 0.03;
+    PyVector2_Set(((PyWorldObject*)s_world)->vecGravity, 0, -2000.f);
 
-	for(i = 0; i < 10; ++i)
-	{
-		body = PG_BodyNew();
-		//PG_Set_Vector2(body->vecPosition, -100+100*i, 200);
-		//PG_Set_Vector2(body->vecLinearVelocity, (100 - 100*i)/2, 0.f);
-		PG_Set_Vector2(body->vecPosition, 0, 400 - 40*i);
-		
-		body->fRotation = 0.f;
-		body->fAngleVelocity = 0.f;
-		body->fRestitution = 0.0f;
-		body->fMass = 10;
-		PG_Bind_RectShape(body, 30, 30, 0);
-		PG_AddBodyToWorld(s_world, body);
-	}
+    for(i = 0; i < 10; ++i)
+    {
+        body = PyBody_New();
+        PyVector2_Set(((PyBodyObject*)body)->vecPosition, 0, 400 - 40*i);
+        
+        ((PyBodyObject*)body)->fRotation = 0.f;
+        ((PyBodyObject*)body)->fAngleVelocity = 0.f;
+        ((PyBodyObject*)body)->fRestitution = 0.0f;
+        ((PyBodyObject*)body)->fMass = 10;
+        PyBody_SetShape (body, PyRectShape_New(30, 30, 0));
+        PyWorld_AddBody(s_world, body);
+    }
 
-	body1 = PG_BodyNew();
-	PG_Set_Vector2(body1->vecPosition,0, -100);
-	body1->bStatic = 1;
-	body1->fRestitution = 1.f;//for test
-	body1->fMass = 1e32;
-	body1->fRotation = 0.f;
-	PG_Bind_RectShape(body1, 1000, 20, 0);
-	PG_AddBodyToWorld(s_world, body1);
-
+    body1 = PyBody_New();
+    PyVector2_Set(((PyBodyObject*)body1)->vecPosition,0, -100);
+    ((PyBodyObject*)body1)->bStatic = 1;
+    ((PyBodyObject*)body1)->fRestitution = 1.f;//for test
+    ((PyBodyObject*)body1)->fMass = 1e32;
+    ((PyBodyObject*)body1)->fRotation = 0.f;
+    PyBody_SetShape(body1, PyRectShape_New (1000, 20, 0));
+    PyWorld_AddBody(s_world, body1);
 }
 
 void TestBasic3Init()
 {
-	pgBodyObject* body1,*body2;
-	pgJointObject* joint;
-	pgVector2 a1,a2;
-	PG_Set_Vector2(a1,0,0);
-	PG_Set_Vector2(a2,0,0);
+    PyObject* body1,*body2;
+    PyObject* joint;
+    PyVector2 a1,a2;
+    PyVector2_Set(a1,0,0);
+    PyVector2_Set(a2,0,0);
 
-	s_world = PG_WorldNew();
-	s_world->fStepTime = 0.03;
-	body1 = PG_BodyNew();
-	PG_Bind_RectShape(body1, 20, 20, 0);
-	PG_Set_Vector2(body1->vecPosition,0,0)
-	PG_Set_Vector2(body1->vecLinearVelocity,10,0)
-	PG_AddBodyToWorld(s_world,body1);
+    s_world = PyWorld_New();
+    ((PyWorldObject*)s_world)->fStepTime = 0.03;
+    body1 = PyBody_New();
+    PyBody_SetShape(body1, PyRectShape_New (20, 20, 0));
+    PyVector2_Set(((PyBodyObject*)body1)->vecPosition,0,0);
+    PyVector2_Set(((PyBodyObject*)body1)->vecLinearVelocity,10,0);
+    PyWorld_AddBody(s_world,body1);
 
-	body2 = PG_BodyNew();
-	PG_Bind_RectShape(body2, 20, 20, 0);
-	PG_Set_Vector2(body2->vecPosition,0,100)
-	PG_Set_Vector2(body2->vecLinearVelocity,0,0)
-	PG_AddBodyToWorld(s_world,body2);
+    body2 = PyBody_New();
+    PyBody_SetShape(body2, PyRectShape_New (20, 20, 0));
+    PyVector2_Set(((PyBodyObject*)body2)->vecPosition,0,100);
+    PyVector2_Set(((PyBodyObject*)body2)->vecLinearVelocity,0,0);
+    PyWorld_AddBody(s_world,body2);
 
-
-	joint = PG_DistanceJointNew(body1,body2,0,100,a1,a2);
-	PG_AddJointToWorld(s_world,joint);
+    joint = PyDistanceJoint_New(body1,body2,0);
+    PyDistanceJoint_SetAnchors (joint,a1,a2);
+    PyWorld_AddJoint(s_world,joint);
 }
 
 void TestBasic4Init()
 {
 #define  BODY_NUM  1
 
-	int i;
-	pgBodyObject* body[BODY_NUM + 1];
-	pgJointObject* joint[BODY_NUM];
-	pgVector2 a1,a2;
-	PG_Set_Vector2(a1,5,0);
-	PG_Set_Vector2(a2,0,100);
+    int i;
+    PyObject* body[BODY_NUM + 1];
+    PyObject* joint[BODY_NUM];
+    PyVector2 a1,a2;
+    PyVector2_Set(a1,5,0);
+    PyVector2_Set(a2,0,100);
 
-	s_world = PG_WorldNew();
-	s_world->fStepTime = 0.03;
-	PG_Set_Vector2(s_world->vecGravity,0,0)
+    s_world = PyWorld_New();
+    ((PyWorldObject*)s_world)->fStepTime = 0.03;
+    PyVector2_Set(((PyWorldObject*)s_world)->vecGravity,0,0);
 
 	body[0] = NULL;
-	for (i = 1;i < BODY_NUM + 1;i++)
-	{
-		body[i] = PG_BodyNew();
-		PG_Bind_RectShape(body[i], 20, 20, 0);
-		PG_Set_Vector2(body[i]->vecPosition,0,(-i*60 + 100))
-		//PG_Set_Vector2(body[i]->vecLinearVelocity,20,0)
-		PG_AddBodyToWorld(s_world,body[i]);
-	}
+    for (i = 1;i < BODY_NUM + 1;i++)
+    {
+        body[i] = PyBody_New();
+        PyBody_SetShape (body[i], PyRectShape_New (20, 20, 0));
+        PyVector2_Set(((PyBodyObject*)body[i])->vecPosition,0,(-i*60 + 100));
+        //PG_Set_Vector2(body[i]->vecLinearVelocity,20,0);
+        PyWorld_AddBody(s_world,body[i]);
+    }
 
-	/*body1 = PG_BodyNew();
-	PG_Set_Vector2(body1->vecPosition,50, 0);
-	body1->bStatic = 1;
-	PG_Bind_RectShape(body1, 20, 300, 0);
-	PG_AddBodyToWorld(s_world, body1);*/
+    /*body1 = PG_BodyNew();
+      PyVector2_Set(body1->vecPosition,50, 0);
+      body1->bStatic = 1;
+      PG_Bind_RectShape(body1, 20, 300, 0);
+      PG_AddBodyToWorld(s_world, body1);*/
 
-	PG_Set_Vector2(body[BODY_NUM]->vecLinearVelocity,40,0)
+    PyVector2_Set(((PyBodyObject*)body[BODY_NUM])->vecLinearVelocity,40,0)
 
 	i = 0;
-	joint[i] = PG_DistanceJointNew(body[i+1],body[i],0,50,a1,a2);
-	PG_AddJointToWorld(s_world,joint[i]);
-	for (i = 1;i < BODY_NUM;i++)
-	{
-		joint[i] = PG_DistanceJointNew(body[i],body[i+1],0,50,a1,a1);
-		PG_AddJointToWorld(s_world,joint[i]);
-	}
+    joint[i] = PyDistanceJoint_New(body[i+1],body[i],0);
+    PyDistanceJoint_SetAnchors (joint[i], a1, a2);
+    PyWorld_AddJoint(s_world,joint[i]);
+    for (i = 1;i < BODY_NUM;i++)
+    {
+        joint[i] = PyDistanceJoint_New(body[i+1],body[i],0);
+        PyDistanceJoint_SetAnchors (joint[i], a1, a1);
+        PyWorld_AddJoint(s_world,joint[i]);
+    }
 #undef BODY_NUM
 
 }
 {
 #define  BODY_NUM  4
 
-	int i;
-	pgBodyObject* body[BODY_NUM + 1];
-	pgJointObject* joint[BODY_NUM];
-	pgVector2 a1,a2;
-	PG_Set_Vector2(a1,0,0);
-	PG_Set_Vector2(a2,0,0);
+    int i;
+    PyObject* body[BODY_NUM + 1];
+    PyObject* joint[BODY_NUM];
+    PyVector2 a1,a2;
+    PyVector2_Set(a1,0,0);
+    PyVector2_Set(a2,0,0);
 
-	s_world = PG_WorldNew();
-	s_world->fStepTime = 0.03;
-	PG_Set_Vector2(s_world->vecGravity,0,10)
-
+    s_world = PyWorld_New();
+    ((PyWorldObject*)s_world)->fStepTime = 0.03;
+    PyVector2_Set(((PyWorldObject*)s_world)->vecGravity,0,10);
 
 	for (i = 0;i < BODY_NUM;i++)
 	{
-		body[i] = PG_BodyNew();
-		if (i != BODY_NUM - 1)
-		{
-			PG_Bind_RectShape(body[i], 20, 20, 0);
-		}
-		else
-		{
-			PG_Bind_RectShape(body[i], 20, 100, 0);
-		}
-		
-		
-			//PG_Set_Vector2(body[i]->vecLinearVelocity,50,0)
-			PG_AddBodyToWorld(s_world,body[i]);
+        body[i] = PyBody_New();
+        if (i != BODY_NUM - 1)
+        {
+            PyBody_SetShape(body[i], PyRectShape_New (20, 20, 0));
+        }
+        else
+        {
+            PyBody_SetShape(body[i], PyRectShape_New (20, 100, 0));
+        }
+        //PyVector2_Set(((PyBodyObject*)body[i])->vecLinearVelocity,50,0)
+        PyWorld_AddBody(s_world,body[i]);
 	}
-	PG_Set_Vector2(body[0]->vecPosition,200,0)
-	PG_Set_Vector2(body[1]->vecPosition,200,100)
-	PG_Set_Vector2(body[2]->vecPosition,300,200)
-	PG_Set_Vector2(body[3]->vecPosition,300,200)
-	body[0]->bStatic = 1;
-	body[3]->bStatic = 1;
-	
+    PyVector2_Set(((PyBodyObject*)body[0])->vecPosition,200,0);
+	PyVector2_Set(((PyBodyObject*)body[1])->vecPosition,200,100);
+	PyVector2_Set(((PyBodyObject*)body[2])->vecPosition,300,200);
+	PyVector2_Set(((PyBodyObject*)body[3])->vecPosition,300,200);
+	((PyBodyObject*)body[0])->bStatic = 1;
+    ((PyBodyObject*)body[3])->bStatic = 1;
 
-	//PG_Set_Vector2(body[BODY_NUM]->vecLinearVelocity,20,60)
+    //PyVector2_Set(((PyBodyObject*)body[BODY_NUM])->vecLinearVelocity,20,60);
 
 
-	for (i = 0;i < BODY_NUM - 2;i++)
-	{
-		joint[i] = PG_DistanceJointNew(body[i],body[i+1],0,50,a1,a2);
-		PG_AddJointToWorld(s_world,joint[i]);
-	}
+    for (i = 0;i < BODY_NUM - 2;i++)
+    {
+        joint[i] = PyDistanceJoint_New(body[i],body[i+1],0);
+        PyDistanceJoint_SetAnchors(joint[i], a1, a2);
+        PyWorld_AddJoint(s_world,joint[i]);
+    }
 
-	#undef BODY_NUM
+#undef BODY_NUM
 }
 
 void TestBasic6Init()
 {
-	pgBodyObject* body[2];
-	pgJointObject* joint;
-	pgVector2 a1,a2;
-	PG_Set_Vector2(a1,0,0);
-	PG_Set_Vector2(a2,0,0);
-	s_world = PG_WorldNew();
-	s_world->fStepTime = 0.03;
-	PG_Set_Vector2(s_world->vecGravity,0,0)
+    PyObject* body[2];
+    PyObject* joint;
+    PyVector2 a1,a2;
+    PyVector2_Set(a1,0,0);
+    PyVector2_Set(a2,0,0);
+    s_world = PyWorld_New();
+    ((PyWorldObject*)s_world)->fStepTime = 0.03;
+    PyVector2_Set(((PyWorldObject*)s_world)->vecGravity,0,0);
 
-	body[0] = PG_BodyNew();
-	PG_Bind_RectShape(body[0], 20, 20, 0);
-	PG_Set_Vector2(body[0]->vecPosition,-50,0)
-	PG_AddBodyToWorld(s_world,body[0]);
+	body[0] = PyBody_New();
+    PyBody_SetShape (body[0], PyRectShape_New (20, 20, 0));
+    PyVector2_Set(((PyBodyObject*)body[0])->vecPosition,-50,0)
+	PyWorld_AddBody(s_world,body[0]);
 
-	body[1] = PG_BodyNew();
-	PG_Bind_RectShape(body[1], 20, 20, 0);
-	PG_Set_Vector2(body[1]->vecPosition,50,0)
-	PG_AddBodyToWorld(s_world,body[1]);
+    body[1] = PyBody_New();
+    PyBody_SetShape (body[1], PyRectShape_New (20, 20, 0));
+    PyVector2_Set(((PyBodyObject*)body[1])->vecPosition,50,0);
+	PyWorld_AddBody(s_world,body[1]);
 
-	PG_Set_Vector2(body[0]->vecLinearVelocity,0,0)
-	PG_Set_Vector2(body[1]->vecLinearVelocity,0,0)
+    PyVector2_Set(((PyBodyObject*)body[0])->vecLinearVelocity,0,0);
+	PyVector2_Set(((PyBodyObject*)body[1])->vecLinearVelocity,0,0);
 
-	joint = PG_DistanceJointNew(body[0],body[1],0,100,a1,a2);
-	PG_AddJointToWorld(s_world,joint);
+	joint = PyDistanceJoint_New(body[0],body[1],0);
+    PyDistanceJoint_SetAnchors (joint,a1, a2);
+    PyWorld_AddJoint(s_world,joint);
 }
 
+void TestBasic7Init()
+{
+    PyObject* body[3];
+    PyObject* joint;
+    
+    PyVector2 a1,a2;
+    PyVector2_Set(a1,0,0);
+    PyVector2_Set(a2,0,0);
+    
+    s_world = PyWorld_New();
+    ((PyWorldObject*)s_world)->fStepTime = 0.03;
+    PyVector2_Set(((PyWorldObject*)s_world)->vecGravity,0,-.1);
+
+	body[0] = PyBody_New();
+    //((PyBodyObject*)body[0])->bStatic = 1;
+    PyBody_SetShape (body[0], PyRectShape_New (20, 20, 0));
+    PyVector2_Set(((PyBodyObject*)body[0])->vecPosition,10,200)
+	PyWorld_AddBody(s_world,body[0]);
+
+    body[1] = PyBody_New();
+    ((PyBodyObject*)body[1])->bStatic = 1;
+    PyBody_SetShape (body[1], PyRectShape_New (20, 20, 0));
+    PyVector2_Set(((PyBodyObject*)body[1])->vecPosition,0,100);
+	PyWorld_AddBody(s_world,body[1]);
+
+    body[2] = PyBody_New();
+    PyBody_SetShape (body[2], PyRectShape_New (20, 20, 0));
+    PyVector2_Set(((PyBodyObject*)body[2])->vecPosition,0,0);
+	PyWorld_AddBody(s_world,body[2]);
+
+    PyVector2_Set(((PyBodyObject*)body[0])->vecLinearVelocity,0,0);
+	PyVector2_Set(((PyBodyObject*)body[1])->vecLinearVelocity,0,0);
+    PyVector2_Set(((PyBodyObject*)body[2])->vecLinearVelocity,0,0);
+
+	joint = PyDistanceJoint_New(body[0],body[1],0);
+    PyDistanceJoint_SetAnchors (joint,a1, a2);
+    PyWorld_AddJoint(s_world,joint);
+    
+	joint = PyDistanceJoint_New(body[1],body[2],0);
+    PyDistanceJoint_SetAnchors (joint,a1, a2);
+    PyWorld_AddJoint(s_world,joint);
+}
 
 
 //===============================================
 
 void InitWorld()
 {
-	TestBasic6Init();
+    TestBasic7Init();
 }
 
 int main (int argc, char** argv)
 {
-	glutInit(&argc, argv);
-	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
-	glutInitWindowSize(WIDTH, HEIGHT);
-	glutCreateWindow("test physics");
+    glutInit(&argc, argv);
+    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
+    glutInitWindowSize(WIDTH, HEIGHT);
+    glutCreateWindow("test physics");
+    
+	Py_Initialize();
+    if (import_physics () == -1)
+	{
+		// Needed for the glut threading.
+		Py_Finalize();
+        return 0;
+	}
 	InitWorld();
-	InitGL();
-	glutDisplayFunc(display);
-	glutKeyboardFunc(keyboard);
-	glutIdleFunc(display);
-	glutMainLoop();
-	return 0;
+    InitGL();
+    glutDisplayFunc(display);
+    glutKeyboardFunc(keyboard);
+    glutIdleFunc(display);
+    glutMainLoop();
+    return 0;
 }
+SOURCES = Main.c pgPhysicsRenderer.c
+OBJECTS = $(SOURCES:%.c=%.o)
+TARGET = physics_test
+
+CC ?= gcc
+CFLAGS ?= -W -Wall -g 
+
+# Windows includes (MinGW)
+#INCLUDES = -DWIN32 -I. -Ic:\\Python25\\include
+#LIBS = -Lc:\\Python25\\libs -mwindows -lm -lpython25 -lopengl32 -lglu32 -lglut32win 
+
+# Linux/Unix includes
+INCLUDES = -I. -I/usr/local/include/ -I/usr/local/include/python2.5
+LIBS = -L/usr/local/lib -lGL -lGLU -lglut -lpython2.5 -lm
+
+CFLAGS += $(INCLUDES)
+LDFLAGS += $(LIBS)
+
+all: $(OBJECTS) $(TARGET)
+
+$(OBJECTS): %.o:%.c
+	$(CC) $(CFLAGS) -c -o $@ $*.c
+
+$(TARGET): $(OBJECTS)
+	$(CC) $(OBJECTS) -o $(TARGET) $(LDFLAGS) 
+
+clean:
+	rm -f *.o $(TARGET)

Test/pgPhysicsRenderer.c

+#ifdef WIN32
+#include <windows.h>
+#endif
+#include <GL/glut.h>
 #include "pgPhysicsRenderer.h"
-#include "pgShapeObject.h"
-#include "pgAABBBox.h"
-#include <GL/glut.h>
 
 int RENDER_AABB = 0;
 
-void PGT_RenderWorld(pgWorldObject* world)
+void PGT_RenderWorld(PyWorldObject* world)
 {
-	Py_ssize_t size = PyList_Size((PyObject*)(world->bodyList));
+	Py_ssize_t size = PyList_Size(world->bodyList);
 	Py_ssize_t i;
 	for (i = 0;i < size;i++)
 	{
-		pgBodyObject* body = (pgBodyObject*)(PyList_GetItem((PyObject*)(world->bodyList),i));
+		PyBodyObject* body = (PyBodyObject*)(PyList_GetItem(world->bodyList,i));
 		PGT_RenderBody(body);
 	}
 
-	size = PyList_Size((PyObject*)(world->jointList));
+	size = PyList_Size(world->jointList);
 	for (i = 0;i < size;i++)
 	{
-		pgJointObject* joint = (pgJointObject*)(PyList_GetItem((PyObject*)(world->jointList),i));
+		PyJointObject* joint = (PyJointObject*)(PyList_GetItem(world->jointList,i));
 		PGT_RenderJoint(joint);
 	}
 }
 
-void PGT_RenderAABB(pgBodyObject* body)
+void PGT_RenderAABB(PyBodyObject* body)
 {
-	pgVector2 p[4];
-	pgAABBBox* box;
+	PyVector2 p[4];
+	AABBBox* box;
 
-	box = &(body->shape->box);
+	box = &(((PyShapeObject*)body->shape)->box);
 	
-	PG_Set_Vector2(p[0], box->left, box->bottom);
-	PG_Set_Vector2(p[1], box->right, box->bottom);
-	PG_Set_Vector2(p[2], box->right, box->top);
-	PG_Set_Vector2(p[3], box->left, box->top);
+	PyVector2_Set(p[0], box->left, box->bottom);
+	PyVector2_Set(p[1], box->right, box->bottom);
+	PyVector2_Set(p[2], box->right, box->top);
+	PyVector2_Set(p[3], box->left, box->top);
 
 	glColor3f(0.f, 1.f, 1.f);
 	glEnable(GL_LINE_STIPPLE);
 	glDisable(GL_LINE_STIPPLE);
 }
 
-void PGT_RenderBody(pgBodyObject* body)
+void PGT_RenderBody(PyBodyObject* body)
 {
-	pgVector2 gp[4];
+	PyVector2 gp[4];
 	int i;
-	pgRectShape* rect = (pgRectShape*)body->shape;
+	PyRectShapeObject* rect = (PyRectShapeObject*)body->shape;
 
-	for(i = 0; i < 4; ++i)
-		gp[i] = PG_GetGlobalPos(body, &(rect->point[i]));
+	// Evil hack for the threads.
+	import_physics ();
+	
+	gp[0] = PyBody_GetGlobalPos((PyObject*)body, rect->bottomleft);
+	gp[1] = PyBody_GetGlobalPos((PyObject*)body, rect->bottomright);
+	gp[2] = PyBody_GetGlobalPos((PyObject*)body, rect->topright);
+	gp[3] = PyBody_GetGlobalPos((PyObject*)body, rect->topleft);
 
 	glColor3f(1.f, 1.f, 0.f);
 	glLineWidth(1.f);
 		PGT_RenderAABB(body);
 }
 
-void PGT_RenderJoint(pgJointObject* joint)
+void PGT_RenderJoint(PyJointObject* joint)
 {
-	pgDistanceJointObject* pj = (pgDistanceJointObject*)joint;
+	PyDistanceJointObject* pj = (PyDistanceJointObject*)joint;
 	glColor3f(1.f, 0.f, 0.f);
 	glLineWidth(1.f);
 	glBegin(GL_LINES);
 	if (joint->body1 && (!joint->body2))
 	{
-		pgVector2 pos = PG_GetGlobalPos(joint->body1,&pj->anchor1);
+		PyVector2 pos = PyBody_GetGlobalPos(joint->body1, pj->anchor1);
 		glVertex2d(pos.real,pos.imag);
 		glVertex2d(pj->anchor2.real,pj->anchor2.imag);
 	}
 	else if(joint->body1 && joint->body2)
 	{
-		pgVector2 pos1 = PG_GetGlobalPos(joint->body1,&pj->anchor1);
-		pgVector2 pos2 = PG_GetGlobalPos(joint->body2,&pj->anchor2);
+		PyVector2 pos1 = PyBody_GetGlobalPos(joint->body1, pj->anchor1);
+		PyVector2 pos2 = PyBody_GetGlobalPos(joint->body2, pj->anchor2);
 		glVertex2d(pos1.real,pos1.imag);
 		glVertex2d(pos2.real,pos2.imag);
 	}

Test/pgPhysicsRenderer.h

 #ifndef _PYGAME_PHYSICS_RENDERER_
 #define _PYGAME_PHYSICS_RENDERER_
 
+#include <physics/pgphysics.h>
 
-#include "pgBodyObject.h"
-#include "pgWorldObject.h"
-#include "pgJointObject.h"
-
-void PGT_RenderWorld(pgWorldObject* world);
-void PGT_RenderBody(pgBodyObject* body);
-void PGT_RenderJoint(pgJointObject* joint);
+void PGT_RenderWorld(PyWorldObject* world);
+void PGT_RenderBody(PyBodyObject* body);
+void PGT_RenderJoint(PyJointObject* joint);
 
 extern int RENDER_AABB;
 

include/pgBodyObject.h

 
 /**
  * Transform point local_p's position from body's local coordinate to
- * the world's global one.
+ * the world's global.
  * TODO: is the local coordinate necessary?  anyway let it alone right
  * now.
  *
 PyVector2 PyBodyObject_GetGlobalPos(PyBodyObject* body, PyVector2* local_p);
 
 /**
- * Translate vector from coordinate B to coordinate A
+ * Translate vector from coordinate B to the body coordinates A
  *
  * @param bodyA
  * @param bodyB
     PyVector2* p_in_B);
 
 /**
- * Get velocity with a local point of a body,assume center is local (0,0)
+ * Get velocity with a local point of a body, assume center is local (0,0).
  *
  * @param body
  * @param localPoint

include/pgDeclare.h

 
 #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 PyJoint_Check(x) (PyObject_TypeCheck(x, &PyJoint_Type))
+#define PyDistanceJoint_Check(x) (PyObject_TypeCheck(x, &PyDistanceJoint_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))

include/pgVector2.h

 #define _PYGAME_VECTOR2_H_
 
 #define PHYSICS_MATH_INTERNAL
-
 #include "pgphysics.h"
 
 /*
  * Internal math function declarations. Used to circumvent the C API
- * array. See pgphysics.h for their descriptions.
+ * array.
+ */
+
+/**
+ * TODO
+ *
+ * @param a
+ * @param b
+ * return
  */
 int PyMath_IsNearEqual(double a, double b);
+
+/**
+ * TODO
+ *
+ * @param a
+ * @param b
+ * return
+ */
 int PyMath_LessEqual(double a, double b);
+
+/**
+ * TODO
+ *
+ * @param a
+ * @param b
+ * return
+ */
 int PyMath_MoreEqual(double a, double b);
+
+/**
+ * TODO
+ *
+ * @param a
+ * @param b
+ * return
+ */
 int PyVector2_Equal(PyVector2* a, PyVector2* b);
+
+/**
+ * TODO
+ *
+ * @param a
+ * @param f
+ * return
+ */
 PyVector2 PyVector2_MultiplyWithReal (PyVector2 a, double f);
+
+/**
+ * TODO
+ *
+ * @param a
+ * @param f
+ * return
+ */
 PyVector2 PyVector2_DivideWithReal (PyVector2 a, double f);
+
+/**
+ * TODO
+ *
+ * @param f
+ * @param a
+ * return
+ */
 PyVector2 PyVector2_fCross(double f, PyVector2 a);
+
+/**
+ * TODO
+ *
+ * @param a
+ * @param f
+ * return
+ */
 PyVector2 PyVector2_Crossf(PyVector2 a, double f);
+
+/**
+ * TODO
+ *
+ * @param a
+ * @param p
+ * return
+ */
 PyVector2 PyVector2_Project(PyVector2 a, PyVector2 p);
 
 /**

include/pgphysics.h

 #ifndef _PHYSICS_H_
 #define _PHYSICS_H_
 
+#include <math.h>
 #include <Python.h>
 
 #ifndef ABS
 #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)
 
 /**
 
 #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);          \
+#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 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
     (*(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                                                 \
+#define PyVector2_MultiplyWithReal                                      \
     (*(PyVector2(*)(PyVector2,double))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+4])
-#define PyVector2_DivideWithReal                                                 \
+#define PyVector2_DivideWithReal                                        \
     (*(PyVector2(*)(PyVector2,double))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+5])
-#define PyVector2_fCross                                                 \
+#define PyVector2_fCross                                                \
     (*(PyVector2(*)(double,PyVector2))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+6])
-#define PyVector2_Crossf                                                 \
+#define PyVector2_Crossf                                                \
     (*(PyVector2(*)(PyVector2,double))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+7])
-#define PyVector2_Project                                                 \
+#define PyVector2_Project                                               \
     (*(PyVector2(*)(PyVector2,PyVector2))PyPhysics_C_API[PHYSICS_MATH_FIRSTSLOT+8])
 
 #endif /* PYGAME_MATH_INTERNAL */
 } PyBodyObject;
 
 #define PHYSICS_BODY_FIRSTSLOT (PHYSICS_MATH_FIRSTSLOT + PHYSICS_MATH_NUMSLOTS)
-#define PHYSICS_BODY_NUMSLOTS 2
+#define PHYSICS_BODY_NUMSLOTS 4
 #ifndef PHYSICS_BODY_INTERNAL
 #define PyBody_Check(x)                                                 \
     (PyObject_TypeCheck(x,                                              \
         (PyTypeObject*)PyPhysics_C_API[PHYSICS_BODY_FIRSTSLOT+0]))
-#define PyBody_New \
+#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])
+#define PyBody_GetGlobalPos                                             \
+    (*(PyVector2(*)(PyObject*,PyVector2))PyPhysics_C_API[PHYSICS_BODY_FIRSTSLOT+3])
 #endif /* PYGAME_BODY_INTERNAL */
 
 /**
 
 #define PHYSICS_JOINT_FIRSTSLOT \
     (PHYSICS_BODY_FIRSTSLOT + PHYSICS_BODY_NUMSLOTS)
-#define PHYSICS_JOINT_NUMSLOTS 4
+#define PHYSICS_JOINT_NUMSLOTS 5
 #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 PyJoint_New                                                     \
+    (*(PyObject*(*)(PyObject*,PyObject*,int))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])
+#define PyDistanceJoint_New                                             \
+    (*(PyObject*(*)(PyObject*,PyObject*,int))PyPhysics_C_API[PHYSICS_JOINT_FIRSTSLOT+3])
+#define PyDistanceJoint_SetAnchors                                      \
+    (*(int(*)(PyObject*,PyVector2,PyVector2))PyPhysics_C_API[PHYSICS_JOINT_FIRSTSLOT+4])
 #endif /* PYGAME_JOINT_INTERNAL */
 
 /**
     PyVector2 bottomright;
     PyVector2 topright;
     PyVector2 topleft;
-} PyRectShape;
+} PyRectShapeObject;
 
 #define PHYSICS_SHAPE_FIRSTSLOT \
     (PHYSICS_JOINT_FIRSTSLOT + PHYSICS_JOINT_NUMSLOTS)
 #define PyShape_Check(x)                                                \
     (PyObject_TypeCheck(x,                                              \
         (PyTypeObject*)PyPhysics_C_API[PHYSICS_SHAPE_FIRSTSLOT+0]))
-#define PyShape_New \
+#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 \
+#define PyRectShape_New                                                 \
     (*(PyObject*(*)(double,double,double))PyPhysics_C_API[PHYSICS_SHAPE_FIRSTSLOT+3])
 #endif /* PYGAME_SHAPE_INTERNAL */
 
 
 #define PHYSICS_WORLD_FIRSTSLOT \
     (PHYSICS_SHAPE_FIRSTSLOT + PHYSICS_SHAPE_NUMSLOTS)
-#define PHYSICS_WORLD_NUMSLOTS 6
+#define PHYSICS_WORLD_NUMSLOTS 7
 #ifndef PHYSICS_WORLD_INTERNAL
 #define PyWorld_Check(x)                                                \
     (PyObject_TypeCheck(x,                                              \
         (PyTypeObject*)PyPhysics_C_API[PHYSICS_WORLD_FIRSTSLOT+0])
-#define PyWorld_New \
+#define PyWorld_New                                                     \
     (*(PyObject*(*)(void))PyPhysics_C_API[PHYSICS_WORLD_FIRSTSLOT+1])
-#define PyWorld_AddBody \
+#define PyWorld_AddBody                                                 \
     (*(PyObject*(*)(PyObject*,PyObject*))PyPhysics_C_API[PHYSICS_WORLD_FIRSTSLOT+2])
-#define PyWorld_RemoveBody \
+#define PyWorld_RemoveBody                                              \
     (*(PyObject*(*)(PyObject*,PyObject*))PyPhysics_C_API[PHYSICS_WORLD_FIRSTSLOT+3])
-#define PyWorld_AddJoint \
+#define PyWorld_AddJoint                                                \
     (*(PyObject*(*)(PyObject*,PyObject*))PyPhysics_C_API[PHYSICS_WORLD_FIRSTSLOT+4])
-#define PyWorld_RemoveJoint \
+#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])
-    
+#define PyWorld_Update                                                  \
+    (*(int(*)(PyObject*,double))PyPhysics_C_API[PHYSICS_WORLD_FIRSTSLOT+6])
+
 #endif /* PYGAME_WORLD_INTERNAL */
 
 /**
 #endif
 PyMODINIT_FUNC initphysics (void);
 
-#endif //_PYGAME_PHYSICS_H_
+#endif /*_PYGAME_PHYSICS_H_*/
                   "-Wshadow", "-Wredundant-decls"
                   ]
     compile_args = [ "-std=c99", "-g"]
-    if True:
-        compile_args += warn_flags
+    compile_args += warn_flags
 
     extphysics = Extension ("physics", sources = get_c_files (),
                             include_dirs = [ "include" ],

src/pgBodyObject.c

 
 #define PHYSICS_BODY_INTERNAL
 #include "pgDeclare.h"
-#include "pgphysics.h"
+#include "pgVector2.h"
 #include "pgHelpFunctions.h"
-#include "pgVector2.h"
 #include "pgBodyObject.h"
 
 static void _BodyInit(PyBodyObject* body);
 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 PyVector2 PyBody_GetGlobalPos (PyObject *body, PyVector2 point);
 
+/**
+ * Methods used by the Body object.
+ */
 static PyMethodDef _Body_methods[] = {
     { "get_points",_Body_getPointList,METH_VARARGS,"" },
     { NULL, NULL, 0, NULL }   /* Sentinel */
 };
 
+/**
+ * Getter/Setter definitions used by the Body
+ */
 static PyGetSetDef _Body_getseters[] = {
     { "mass", (getter) _Body_getMass, (setter) _Body_setMass, "Mass",
       NULL },
 static PyObject* _BodyNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
     //TODO: parse args later on
-    PyBodyObject* op = (PyBodyObject*)type->tp_alloc(type, 0);
-    _BodyInit(op);
-    return (PyObject*)op;
+    PyObject* op = type->tp_alloc(type, 0);
+    if (!op)
+        return NULL;
+
+    _BodyInit((PyBodyObject*)op);
+    return op;
 }
 
 static void _BodyDestroy(PyBodyObject* body)
 
     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);
+
     body->vecForce.real = real;
     body->vecForce.imag = imag;
     return 0;
     shape = (PyShapeObject*) value;
     if (shape->type == ST_RECT)
     {
-        PyRectShape* rsh = (PyRectShape*) shape;
+        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 *
     /* TODO: shapes */
     list = PyList_New (4);
 
-    pVertex = &(((PyRectShape*)(body->shape))->bottomleft);
+    pVertex = &(((PyRectShapeObject*)(body->shape))->bottomleft);
     golVertex = PyBodyObject_GetGlobalPos(body,pVertex);
     tuple = FromPhysicsVector2ToPoint(golVertex);
     PyList_SetItem(list,0,tuple);
 
-    pVertex = &(((PyRectShape*)(body->shape))->bottomright);
+    pVertex = &(((PyRectShapeObject*)(body->shape))->bottomright);
     golVertex = PyBodyObject_GetGlobalPos(body,pVertex);
     tuple = FromPhysicsVector2ToPoint(golVertex);
     PyList_SetItem(list,1,tuple);
 
-    pVertex = &(((PyRectShape*)(body->shape))->topright);
+    pVertex = &(((PyRectShapeObject*)(body->shape))->topright);
     golVertex = PyBodyObject_GetGlobalPos(body,pVertex);
     tuple = FromPhysicsVector2ToPoint(golVertex);
     PyList_SetItem(list,2,tuple);
 
-    pVertex = &(((PyRectShape*)(body->shape))->topleft);
+    pVertex = &(((PyRectShapeObject*)(body->shape))->topleft);
     golVertex = PyBodyObject_GetGlobalPos(body,pVertex);
     tuple = FromPhysicsVector2ToPoint(golVertex);
     PyList_SetItem(list,3,tuple);
     return c_sum(vel,body->vecLinearVelocity);
 }
 
-
 /* C API */
 static PyObject* PyBody_New(void)
 {
     return _BodyNew(&PyBody_Type, NULL, NULL);
 }
 
+static int PyBody_SetShape(PyObject *body, PyObject *shape)
+{
+    if (!PyBody_Check (body))
+    {
+        PyErr_SetString (PyExc_TypeError, "body must be a Body");
+        return 0;
+    }
+    if (_Body_setShape ((PyBodyObject*)body, shape, NULL) == -1)
+        return 0;
+    return 1;
+}
+
+static PyVector2 PyBody_GetGlobalPos (PyObject *body, PyVector2 point)
+{
+    return PyBodyObject_GetGlobalPos ((PyBodyObject*) body, &point);
+}
+
 void PyBodyObject_ExportCAPI (void **c_api)
 {
     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;
 }

src/pgCollision.c

 
 #include <assert.h>
 #include "pgDeclare.h"
-#include "pgphysics.h"
+#include "pgVector2.h"
 #include "pgCollision.h"
-#include "pgVector2.h"
 
 static int _LiangBarskey_Internal(double p, double q, double* u1, double* u2);
 static void _UpdateV(PyJointObject* joint, double step);
     PyVector2 refR, incidR;
     PyShapeObject *refShape, *incidShape;
 
-
     contact = (PyContact*)joint;
     refBody = (PyBodyObject *)joint->body1;
     incidBody = (PyBodyObject *)joint->body2;

src/pgHelpFunctions.c

   License along with this library; if not, write to the Free
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
+
 #include "pgVector2.h"
+#include "pgHelpFunctions.h"
 
 int
 DoubleFromObj (PyObject* obj, double* val)

src/pgJointObject.c

 
 #define PHYSICS_JOINT_INTERNAL
 #include "pgDeclare.h"
-#include "pgphysics.h"
 #include "pgVector2.h"
+#include "pgHelpFunctions.h"
 #include "pgBodyObject.h"
 #include "pgJointObject.h"
-#include "pgHelpFunctions.h"
 
-static int _JointBase_InitInternal(PyJointObject* joint,PyObject* b1,
+static void _JointBase_InitInternal(PyJointObject* joint,PyObject* b1,
     PyObject* b2,int bCollideConnect);
 static void _JointDestroy(PyJointObject* joint);
 static PyObject* _JointBaseNew(PyTypeObject *type, PyObject *args,
 
 static PyObject* _DistanceJoint_getDistance(PyDistanceJointObject* joint,
     void* closure);
-static int _DistanceJoint_setDistance(PyDistanceJointObject* joint,
-    PyObject* value,void* closure);
+/*static int _DistanceJoint_setDistance(PyDistanceJointObject* joint,
+  PyObject* value,void* closure);*/
 static PyObject* _DistanceJoint_getAnchor1(PyDistanceJointObject* joint,
     void* closure);
 static int _DistanceJoint_setAnchor1(PyDistanceJointObject* joint,
     PyObject* value,void* closure);
 static PyObject* _DistanceJoint_getPointList(PyObject *self, PyObject *args);
 
+/* C API */
+static PyObject* PyJoint_New(PyObject *body1, PyObject *body2, int collideConnect);
+static PyObject* PyDistanceJoint_New(PyObject *body1, PyObject *body2, int collideConnect);
+static int PyDistanceJoint_SetAnchors(PyObject *joint,PyVector2 anchor1,PyVector2 anchor2);
+
 
 static PyGetSetDef _JointBase_getseters[] = {
     { "body1",(getter)_Joint_getBody1,(setter)_Joint_setBody1,"",NULL },
     0                           /* tp_del */
 };
 
-static int _JointBase_InitInternal(PyJointObject* joint,PyObject* b1,
+static void _JointBase_InitInternal(PyJointObject* joint,PyObject* b1,
     PyObject* b2,int bCollideConnect)
 {
-    if (!PyBody_Check (b1))
-    {
-        PyErr_SetString (PyExc_TypeError, "body1 must be a Body");
-        return 0;
-    }
-    if (!PyBody_Check (b2))
-    {
-        PyErr_SetString (PyExc_TypeError, "body2 must be a Body");
-        return 0;
-    }
-    
     Py_INCREF (b1);
     Py_INCREF (b2);
-    
     joint->body1 = b1;
     joint->body2 = b2;
     joint->isCollideConnect = bCollideConnect;
-    return 1;
 }
 
 static int _JointBase_init(PyJointObject* joint,PyObject *args, PyObject *kwds)
 {
     PyObject* body1, *body2;
     int bCollide;
-    static char *kwlist[] = {"body1", "body2", "isCollideConnect", NULL};
+    static char *kwlist[] = {"body1", "body2", "collide_connect", NULL};
 
     if (!PyArg_ParseTupleAndKeywords(args,kwds,"OOi",kwlist,&body1,&body2,
             &bCollide))
     {
         return -1;
     }
-    
-    if (!_JointBase_InitInternal(joint, body1, body2, bCollide))
+ 
+    if (!PyBody_Check (body1))
+    {
+        PyErr_SetString (PyExc_TypeError, "body1 must be a Body");
         return -1;
+    }
+    if (!PyBody_Check (body2))
+    {
+        PyErr_SetString (PyExc_TypeError, "body2 must be a Body");
+        return -1;
+    }
+   
+    _JointBase_InitInternal(joint, body1, body2, bCollide);
     return 0;
 }
 
      * on.
      */
     PyJointObject* joint = (PyJointObject*)type->tp_alloc(type, 0);
+    if (!joint)
+        return NULL;
+
     joint->body1 = NULL;
     joint->body2 = NULL;
     joint->isCollideConnect = 0;
 
 static PyGetSetDef _DistanceJoint_getseters[] = {
     { "distance",(getter)_DistanceJoint_getDistance,
-      (setter)_DistanceJoint_setDistance,"",NULL, },
+      /*(setter)_DistanceJoint_setDistance*/NULL,"",NULL, },
     { "anchor1",(getter)_DistanceJoint_getAnchor1,
       (setter)_DistanceJoint_setAnchor1,"",NULL, },
     { "anchor2",(getter)_DistanceJoint_getAnchor2,
     
     joint->joint.SolveConstraintVelocity = _SolveDistanceJointVelocity;
     //joint->joint.SolveConstraintPosition = _SolveDistanceJointPosition;
-    joint->distance = 10.0;
+    joint->distance = 0.0;
     PyVector2_Set(joint->anchor1,0,0);
     PyVector2_Set(joint->anchor2,0,0);
 
     dAngleV /= k;
     body->fAngleVelocity += dAngleV;
 
-    // TODO: Position correction, badly needed, otherwise the python tests
-    // will cause the joints to grow in their size %-).
+    // Position correction.
     temp = -bb /a;
     dvBody = PyVector2_MultiplyWithReal(L,temp);
     dAngleV = PyVector2_Cross(localP,dvBody);
     return PyFloat_FromDouble(joint->distance);
 }
 
+/*
 static int _DistanceJoint_setDistance(PyDistanceJointObject* joint,
     PyObject* value,void* closure)
 {
     }
     PyErr_SetString (PyExc_TypeError, "distance must be a float");
     return -1;
-}
+    }
+*/
 
 static PyObject* _DistanceJoint_getAnchor1(PyDistanceJointObject* joint,
     void* closure)
 }
 
 /* C API */
-static PyObject* PyJoint_New(void)
+static PyObject* PyJoint_New(PyObject *body1, PyObject *body2, int collideConnect)
 {
-    return _JointBaseNew(&PyJoint_Type, NULL, NULL);
+    PyObject *joint;
+
+    if (!PyBody_Check (body1))
+    {
+        PyErr_SetString (PyExc_TypeError, "body1 must be a Body");
+        return 0;
+    }
+    if (!PyBody_Check (body2))
+    {
+        PyErr_SetString (PyExc_TypeError, "body2 must be a Body");
+        return 0;
+    }
+
+    joint = _JointBaseNew (&PyJoint_Type, NULL, NULL);
+    if (!joint)
+        return NULL;
+    _JointBase_InitInternal ((PyJointObject*)joint, body1, body2, collideConnect);
+    return joint;
 }
 
-static PyObject* PyDistanceJoint_New(void)
+static PyObject* PyDistanceJoint_New(PyObject *body1, PyObject *body2, int collideConnect)
 {
-    PyObject* op = PyDistanceJoint_Type.tp_alloc(&PyDistanceJoint_Type, 0);
-    return op;
+    PyObject* joint;
+
+    if (!PyBody_Check (body1))
+    {
+        PyErr_SetString (PyExc_TypeError, "body1 must be a Body");
+        return 0;
+    }
+    if (!PyBody_Check (body2))
+    {
+        PyErr_SetString (PyExc_TypeError, "body2 must be a Body");
+        return 0;
+    }
+
+    joint = _DistanceJointNew (&PyDistanceJoint_Type, NULL, NULL);
+    if (!joint)
+        return NULL;
+
+    _JointBase_InitInternal ((PyJointObject*)joint, body1, body2, collideConnect);
+    return joint;
+}
+
+static int PyDistanceJoint_SetAnchors(PyObject *joint,PyVector2 anchor1,PyVector2 anchor2)
+{
+    if (!PyDistanceJoint_Check (joint))
+    {
+        PyErr_SetString (PyExc_TypeError, "joint must be a DistanceJoint");
+        return 0;
+    }
+
+    ((PyDistanceJointObject*)joint)->anchor1.real = anchor1.real;
+    ((PyDistanceJointObject*)joint)->anchor1.imag = anchor1.imag;
+    ((PyDistanceJointObject*)joint)->anchor2.real = anchor2.real;
+    ((PyDistanceJointObject*)joint)->anchor2.imag = anchor2.imag;
+    _ReComputeDistance((PyDistanceJointObject*)joint);
+    return 1;
 }
 
 void PyJointObject_ExportCAPI (void **c_api)
     c_api[PHYSICS_JOINT_FIRSTSLOT + 1] = &PyJoint_New;
     c_api[PHYSICS_JOINT_FIRSTSLOT + 2] = &PyDistanceJoint_Type;
     c_api[PHYSICS_JOINT_FIRSTSLOT + 3] = &PyDistanceJoint_New;
+    c_api[PHYSICS_JOINT_FIRSTSLOT + 4] = &PyDistanceJoint_SetAnchors;
 }

src/pgModuleInit.c

 */
 
 #include "pgDeclare.h"
-#include "pgphysics.h"
+#include "pgVector2.h"
 #include "pgWorldObject.h"
 #include "pgBodyObject.h"
 #include "pgJointObject.h"
 #include "pgShapeObject.h"
-#include "pgVector2.h"
-
 
 PyMODINIT_FUNC
 initphysics(void) 

src/pgShapeObject.c

 
 #define PHYSICS_SHAPE_INTERNAL
 #include <float.h>
-
 #include "pgDeclare.h"
-#include "pgphysics.h"
 #include "pgAABBBox.h"
+#include "pgVector2.h"
+#include "pgCollision.h"
 #include "pgBodyObject.h"
 #include "pgShapeObject.h"
-#include "pgCollision.h"
-#include "pgVector2.h"
 
 static PyObject* _ShapeNew(PyTypeObject *type, PyObject *args, PyObject *kwds);
 static void _ShapeObjectDestroy(PyShapeObject* shape);
 
 static void _RectShapeUpdateAABB(PyBodyObject* body);
-static int _RectShape_init(PyRectShape* shape,PyObject *args, PyObject *kwds);
-static void _RectShape_InitInternal (PyRectShape *shape, double width,
+static int _RectShape_init(PyRectShapeObject* shape,PyObject *args, PyObject *kwds);
+static void _RectShape_InitInternal (PyRectShapeObject *shape, double width,
     double height, double seta);
 static PyObject* _RectShapeNew(PyTypeObject *type, PyObject *args, PyObject *kwds);
 
 static int _RectShapeCollision(PyBodyObject* selfBody,
     PyBodyObject* incidBody, PyObject* contactList);
 
+/* C API */
 static PyObject *PyShape_New (void);
 static PyObject *PyRectShape_New (double width, double height, double seta);
 
     PyObject_HEAD_INIT(NULL)
     0,
     "physics.RectShape",        /* tp_name */
-    sizeof(PyRectShape),        /* tp_basicsize */
+    sizeof(PyRectShapeObject),  /* tp_basicsize */
     0,                          /* tp_itemsize */
     (destructor) 0,				/* tp_dealloc */
     0,                          /* tp_print */
 
     if(((PyShapeObject*)body->shape)->type == ST_RECT)
     {
-        PyRectShape *p = (PyRectShape*)body->shape;
+        PyRectShapeObject *p = (PyRectShapeObject*)body->shape;
 		
         AABB_Clear(&(p->shape.box));
 
     }
 }
 
-static void _RectShape_InitInternal (PyRectShape *shape, double width,
+static void _RectShape_InitInternal (PyRectShapeObject *shape, double width,
     double height, double seta)
 {
     PyVector2_Set(shape->bottomleft, -width/2, -height/2);
     PyVector2_Rotate(&(shape->topleft), seta);
 }
 
-static int _RectShape_init(PyRectShape* shape,PyObject *args, PyObject *kwds)
+static int _RectShape_init(PyRectShapeObject* shape,PyObject *args, PyObject *kwds)
 {
     double width, height, seta = 0;
     if (PyShape_Type.tp_init((PyObject*)shape, args, kwds) < 0)
 static PyObject* _RectShapeNew(PyTypeObject *type, PyObject *args,
     PyObject *kwds)
 {
-    PyRectShape *shape = (PyRectShape*) _ShapeNew (type, args, kwds);
+    PyRectShapeObject *shape = (PyRectShapeObject*) _ShapeNew (type, args, kwds);
     if (!shape)
         return NULL;
     
 #define _EPS_DEPTH 1e-2
 
     int i, apart;
-    PyRectShape *ref, *inc;
+    PyRectShapeObject *ref, *inc;
     double deps[4];
 
-    ref = (PyRectShape*)refBody->shape;
-    inc = (PyRectShape*)incBody->shape;
+    ref = (PyRectShapeObject*)refBody->shape;
+    inc = (PyRectShapeObject*)incBody->shape;
     memset(gp_in_ref, 0, sizeof(gp_in_ref));
     memset(deps, 0, sizeof(deps));
     
 
 static PyObject *PyRectShape_New (double width, double height, double seta)
 {
-    PyRectShape *shape = (PyRectShape*)_RectShapeNew (&PyRectShape_Type, NULL, NULL);
+    PyRectShapeObject *shape = (PyRectShapeObject*)
+        _RectShapeNew (&PyRectShape_Type, NULL, NULL);
     _RectShape_InitInternal (shape, width, height, seta);
     return (PyObject*) shape;
 }
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
-#define PHYSICS_SHAPE_INTERNAL
-
 #include "pgVector2.h"
 
 int PyMath_IsNearEqual(double a, double b)

src/pgWorldObject.c

 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 void PyWorld_Update(PyObject* world, double dt);
+static int PyWorld_Update(PyObject* world, double dt);
 
 /**
  * Here we allow the Python object to do stuff like
     }
 
     contact_cnt = PyList_Size(world->contactList);
-    if (contact_cnt)
-    {
-        //printf("contact_cnt:%d\n",contact_cnt);
-    }
-	
+/*     if (contact_cnt) */
+/*     { */
+/*         printf("contact_cnt:%d\n",contact_cnt); */
+/*     } */
     for(j = 0; j < MAX_ITERATION; ++j)
     {
         //clear bias
         }
 
         //collision reaction
-        contact_cnt = PyList_Size(world->contactList);
         for(i = 0; i < contact_cnt; ++i)
         {
             contact = (PyJointObject*)(PyList_GetItem(world->contactList, i));
 static PyWorldObject* _WorldNewInternal(PyTypeObject *type)
 {
     PyWorldObject* op = (PyWorldObject*)type->tp_alloc(type, 0);
+    if (!op)
+        return NULL;
+
     _WorldInit(op);
     return op;
 }
 {
     PyObject *item;
     double real, imag;
+    
 
     if (!PySequence_Check(value) || PySequence_Size (value) != 2)
     {
 
     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;
 
 static int PyWorld_AddBody(PyObject* world, PyObject* body)
 {
-    return !PyList_Append(((PyWorldObject*)world)->bodyList,body);
+    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;
+    }
+        
+    if (PyList_Append(((PyWorldObject*)world)->bodyList, body) == 0)
+        return 1;
+    return 0;
 }
 
 static int PyWorld_RemoveBody(PyObject* world, PyObject* body)
 {
-    // TODO
+    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)
 {
-    return !PyList_Append(((PyWorldObject*)world)->jointList,joint);
+    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;
+    }
+    
+    if (PyList_Append(((PyWorldObject*)world)->jointList, joint) == 0)
+        return 1;
+    return 0;
 }
 
 static int PyWorld_RemoveJoint(PyObject* world, PyObject* joint)
 {
-    // TODO
+    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 void PyWorld_Update(PyObject* world, double dt)
+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)
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.