Anonymous avatar Anonymous committed 3b05caa

Added radians<->degrees conversion methods.
Body.rotation and Body.angular_velocity now take degrees as values.
Fixed shape inertia calculation on setting a new body mass.

Comments (0)

Files changed (4)

     body1.position = 100, 100
     body1.velocity = 2,0
     body1.restitution = 1.0
-    body1.rotation = 2
+    body1.rotation = 115
+    body1.angular_velocity = -23
     body1.mass = 80*33
     w.add_body(body1)
     body2 = physics.Body(physics.RectShape (20,20,0))
     body2.position = 200, 100
     body2.velocity = -2, 0
-    body1.restitution = 1.0
-    body1.rotation = 3
-    body1.angular_velocity = 1
-    body1.mass = 20*20
+    body2.restitution = 1.0
+    body2.rotation = 200
+    body2.angular_velocity = 57
+    body2.mass = 20*20
     w.add_body(body2)
     return w
 
     for i in range(1, 30):
         body1 = physics.Body(physics.RectShape(30, 28, 0))
         body1.position = 400, -900 + 40*i
-        body1.rotation = i
+        body1.rotation = 33*i
         body1.restitution = 0.0
         body1.mass = 20
         w.add_body(body1)
     body2.restitution = 0.0
     body2.mass = 1e100
     body2.static = True
-    body2.rotation = 0.2
+    body2.rotation = 15
     w.add_body(body2)
     
     return w

include/pgphysics.h

 #ifndef M_PI
 #define M_PI 3.1415926535897932384626433832795
 #endif
+#ifndef DEG2RAD
+#define DEG2RAD(x) ((x) * M_PI / 180.0f)
+#endif
+#ifndef RAD2DEG
+#define RAD2DEG(x) (180.0f / M_PI * (x))
+#endif
 
 #define IS_NEAR_ZERO(num) (fabs(num) <= ZERO_EPSILON)
 

src/pgBodyObject.c

  * Getter/Setter definitions used by the Body
  */
 static PyGetSetDef _Body_getseters[] = {
-    { "mass", (getter) _Body_getMass, (setter) _Body_setMass, "Mass",
-      NULL },
-    { "shape",(getter)_Body_getShape, NULL,"Shape", NULL},
+    { "mass", (getter) _Body_getMass, (setter) _Body_setMass,
+      "The mass of the Body.", NULL },
+    { "shape",(getter)_Body_getShape, NULL,
+      "The shape of the Body. (Read-only)", NULL},
     { "rotation", (getter) _Body_getRotation, (setter) _Body_setRotation,
       "Rotation", NULL },
     { "torque", (getter) _Body_getTorque, (setter) _Body_setTorque,
         }
     }
     PyErr_SetString (PyExc_TypeError, "mass must be a float");
+
+    // I = M(a^2 + b^2)/12
+    // TODO:
+    // This should be automatically be done by the shape.
+    if (((PyShapeObject*)body->shape)->type == ST_RECT)
+    {
+        PyRectShapeObject* rsh = (PyRectShapeObject*) body->shape;
+        double width = ABS (rsh->bottomright.real - rsh->bottomleft.real);
+        double height = ABS (rsh->bottomright.imag - rsh->topright.imag);
+        ((PyShapeObject*)body->shape)->rInertia = body->fMass *
+            (width * width + height * height) / 12;
+    }
+
     return -1;
 }
 
  */
 static PyObject* _Body_getRotation (PyBodyObject* body,void* closure)
 {
-    return PyFloat_FromDouble (body->fRotation);
+    return PyFloat_FromDouble (RAD2DEG(body->fRotation));
 }
 
 /**
             Py_DECREF (tmp);
             if (PyErr_Occurred ())
                 return -1;
-            body->fRotation = rotation;
+            body->fRotation = DEG2RAD(rotation);
             return 0;
         }
     }
 */
 static PyObject* _Body_getAngularVel (PyBodyObject* body,void* closure)
 {
-	return PyFloat_FromDouble (body->fAngleVelocity);
+    return PyFloat_FromDouble (RAD2DEG(body->fAngleVelocity));
 }
 
 /**
             Py_DECREF (tmp);
             if (PyErr_Occurred ())
                 return -1;
-            body->fAngleVelocity = fAngleVelocity;
+            body->fAngleVelocity = DEG2RAD(fAngleVelocity);
             return 0;
         }
     }
 static PyObject* PyBody_New(PyObject *shape)
 {
     PyBodyObject* body;
-    PyShapeObject *sh;
     
     if (!PyShape_Check (shape))
     {
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.