Commits

Anonymous committed 8cc23d4

Fixed tuple assignments for bodies. world.gravity now takes a tuple. Fixed output list for points.

  • Participants
  • Parent commits 0900459
  • Branches physics

Comments (0)

Files changed (7)

 
 def init_world():
     w = physics.World()
-    w.gravity = 0+1j
+    w.gravity = 0, 1
     body1 = physics.Body()
-    body1.bind_rect_shape(2,2,0)
-    body1.position = 10, 20
-    body1.velocity = 1,0
+    body1.bind_rect_shape(20,20,0)
+    body1.position = 100, 100
+    body1.velocity = 10,0
     body1.restitution = 3.0
     w.add_body(body1)
     body2 = physics.Body()
-    body2.bind_rect_shape(2,2,0)
-    body2.position = 20, 20
-    body2.velocity = -1, 0
+    body2.bind_rect_shape(20,20,0)
+    body2.position = 200, 100
+    body2.velocity = -2, 0
     body1.restitution = 3.0
     w.add_body(body2)
     return w
     pygame.init()
     screen = pygame.display.set_mode((800, 800))
     pygame.display.set_caption('physics test')
-    pygame.mouse.set_visible(0)
 
 #Create The Backgound
     background = pygame.Surface(screen.get_size())
     for joint in world.joint_list:
         render_joint(joint,surface,joint_color)        
 
-
-    
-
 def init_world():
     w = physics.World()
-    w.gravity = 0+1j
+    w.gravity = 0, 1
     
     body = physics.Body()
-    body.bind_rect_shape(2,2,0)
-    body.position = 20, 0
+    body.bind_rect_shape(20,20,0)
+    body.position = 200, 100
     body.restitution = 3.0
     body.static = 1
     w.add_body(body)
     body1 = physics.Body()
-    body1.bind_rect_shape(2,2,0)
-    body1.position = 20, 10
+    body1.bind_rect_shape(20,20,0)
+    body1.position = 200, 200
     body1.restitution = 3.0
     w.add_body(body1)
     body2 = physics.Body()
-    body2.bind_rect_shape(2,2,0)
-    body2.position = 30, 20
+    body2.bind_rect_shape(20,20,0)
+    body2.position = 300, 300
     body1.restitution = 3.0
     w.add_body(body2)
     
     joint1 = physics.DistanceJoint(body1,body,1)
+    joint1.distance = 100.
     joint1.anchor1 = 0+0j
     joint1.anchor2 = 0+0j
     w.add_joint(joint1)
     joint2 = physics.DistanceJoint(body1,body2,1)
+    joint2.distance = 100.
     joint2.anchor1 = 0+0j
     joint2.anchor2 = 0+0j
     w.add_joint(joint2)
 
 
 #this calls the 'main' function when this script is executed
-if __name__ == '__main__': main()
+if __name__ == '__main__': main()

include/pgHelpFunctions.h

 #ifndef _PG_HELPFUNCTIONS_H
 #define _PG_HELPFUNCTIONS_H
 
+#include <Python.h>
+#include "pgVector2.h"
+
 /**
  * Tries to retrieve the double value from a python object.
  * Taken from the pygame codebase.
 int
 DoubleFromObj (PyObject* obj, double* val);
 
-
-PyObject* FromPhysicsVector2ToPygamePoint(pgVector2 v2);
+PyObject*
+FromPhysicsVector2ToPoint (pgVector2 v2);
 
 #endif /* _PG_HELPFUNCTIONS_H */

src/pgBodyObject.c

 		body->fMass));
 	body->vecLinearVelocity = c_sum(body->vecLinearVelocity, 
 		c_mul_complex_with_real(totalF, dt/body->fMass));
-	
 }
 
 //void PG_FreeUpdateBodyPos(pgWorldObject* world,pgBodyObject* body,double dt)
     item = PySequence_GetItem (value, 0);
     if (!DoubleFromObj (item, &real))
         return -1;
-    item = PySequence_GetItem (value, 0);
+    item = PySequence_GetItem (value, 1);
     if (!DoubleFromObj (item, &imag))
         return -1;
     
     item = PySequence_GetItem (value, 0);
     if (!DoubleFromObj (item, &real))
         return -1;
-    item = PySequence_GetItem (value, 0);
+    item = PySequence_GetItem (value, 1);
     if (!DoubleFromObj (item, &imag))
         return -1;
     
     item = PySequence_GetItem (value, 0);
     if (!DoubleFromObj (item, &real))
         return -1;
-    item = PySequence_GetItem (value, 0);
+    item = PySequence_GetItem (value, 1);
     if (!DoubleFromObj (item, &imag))
         return -1;
     
 		{
 			pgVector2* pVertex = &(((pgRectShape*)(body->shape))->point[i]);
 			pgVector2 golVertex = PG_GetGlobalPos(body,pVertex);
-			PyObject* tuple = FromPhysicsVector2ToPygamePoint(golVertex);
+			PyObject* tuple = FromPhysicsVector2ToPoint(golVertex);
 			PyList_SetItem(list,i,tuple);
 		}
 		return (PyObject*)list;

src/pgHelpFunctions.c

     return 0;
 }
 
-PyObject* FromPhysicsVector2ToPygamePoint(pgVector2 v2)
+PyObject* FromPhysicsVector2ToPoint(pgVector2 v2)
 {
 	PyObject* tuple = PyTuple_New(2);
 
-	long ix = v2.real * FLOAT_TO_INT_MUL;
-	long iy = v2.imag * FLOAT_TO_INT_MUL;
-	PyObject* xnum = PyInt_FromLong(ix);
-	PyObject* ynum = PyInt_FromLong(iy);
+	PyObject* xnum = PyFloat_FromDouble (v2.real);
+	PyObject* ynum = PyFloat_FromDouble (v2.imag);
 	PyTuple_SetItem(tuple,0,xnum);
 	PyTuple_SetItem(tuple,1,ynum);
 	return tuple;

src/pgJointObject.c

-#include "pgJointObject.h"
+include "pgJointObject.h"
 #include "pgShapeObject.h"
+#include "pgHelpFunctions.h"
 #include <structmember.h>
 
 extern PyTypeObject pgDistanceJointType;
 	PyObject* list  = PyList_New(2);
 
 	pgVector2 p = PG_GetGlobalPos(joint->joint.body1,&joint->anchor1);
-	PyObject* tuple = FromPhysicsVector2ToPygamePoint(p);
+	PyObject* tuple = FromPhysicsVector2ToPoint(p);
 	PyList_SetItem(list,0,tuple);
 
 	if(joint->joint.body2)
 		p = joint->anchor2;
 	}
 
-	tuple = FromPhysicsVector2ToPygamePoint(p);
+	tuple = FromPhysicsVector2ToPoint(p);
 	PyList_SetItem(list,1,tuple);
 	return list;
 }

src/pgWorldObject.c

 #include "pgJointObject.h"
 #include "pgCollision.h"
 #include "pgShapeObject.h"
+#include "pgHelpFunctions.h"
+
 #include <structmember.h>
 
 #define MAX_ITERATION 5
 
 static PyObject* _pgWorld_getGravity(pgWorldObject* world,void* closure)
 {
-	return PyComplex_FromCComplex(world->vecGravity);
+    return Py_BuildValue ("(ff)", world->vecGravity.real,
+        world->vecGravity.imag);
 }
 
 static int _pgWorld_setGravity(pgWorldObject* world,PyObject* value,void* closure)
 {
-	if (value == NULL || (!PyComplex_Check(value))) {
-		PyErr_SetString(PyExc_TypeError, "Cannot set the gravity attribute");
-		return -1;
-	}
-	else
-	{
-		world->vecGravity = PyComplex_AsCComplex(value);
-		return 0;
-	}
+    PyObject *item;
+    double real, imag;
+
+    if (!PySequence_Check(value) || PySequence_Size (value) != 2)
+    {
+        PyErr_SetString (PyExc_TypeError, "gravity must be a x, y sequence");
+        return -1;
+    }
+
+    item = PySequence_GetItem (value, 0);
+    if (!DoubleFromObj (item, &real))
+        return -1;
+    item = PySequence_GetItem (value, 1);
+    if (!DoubleFromObj (item, &imag))
+        return -1;
+    
+    world->vecGravity.real = real;
+    world->vecGravity.imag = imag;
+    return 0;
 }
 
 static PyObject* _pgWorld_getBodyList(pgWorldObject* world,void* closure)