Anonymous avatar Anonymous committed 50dbef7

add damping coefficient python interface
add test5.py and Readme_Test_List.txt

Comments (0)

Files changed (3)

PYD/Readme_Test_List.txt

+test1.py	two falling objects with collision
+test2.py	there bodys connected by distance joints
+test3.py	a lot of falling objects (demo of collision test and response)
+test4.py	chain simulation
+test5.py	chain simulation with damping of velocity (compare with test4.py)
+# The python file is under test
+
+import pygame
+import physics
+from pygame.locals import *
+
+def render_body(body,surface,color):
+    l = body.get_points()
+    pygame.draw.polygon(surface,color,l)
+
+def render_joint(joint,surface,color):
+	l = joint.get_points()
+	pygame.draw.lines(surface,color,False,l)
+
+def render_world(world,surface,body_color,joint_color):
+    for body in world.bodies:
+        render_body(body,surface,body_color)
+    for joint in world.joints:
+        render_joint(joint,surface,joint_color)        
+
+def init_world():
+    w = physics.World()
+    w.gravity = 0, 5
+    body = physics.Body(physics.RectShape(1, 1, 0))
+    body.position = 400, 20
+    body.restitution = 0.0
+    body.static = True
+    w.add_body(body)
+    pre_body = body
+    for i in range(1, 30):
+        body = physics.Body(physics.RectShape(5, 5, 0))
+        body.position = 400+10*i, 10*i+20
+        body.restitution = 0.0
+	body.linear_vel_damping = 0.05
+        body.mass = 20
+        w.add_body(body)
+        joint = physics.DistanceJoint(pre_body,body,0)
+        joint.anchor1 = 0, 0
+        joint.anchor2 = 0, 0
+        w.add_joint(joint)
+        pre_body = body
+    
+    return w
+
+def main():
+    """this function is called when the program starts.
+       it initializes everything it needs, then runs in
+       a loop until the function returns."""
+#Initialize Everything
+    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())
+    background = background.convert()
+    background.fill((0,0,0))
+
+
+#Display The Background
+    screen.blit(background, (0, 0))
+    pygame.display.flip()
+
+#Prepare Game Objects
+    clock = pygame.time.Clock()
+        
+    white = 250, 250, 250
+    red = 255,0,0
+    world = init_world()
+    
+    
+#Main Loop
+    while 1:
+        t = clock.tick(60)
+        t = t/1000.0;
+        world.update(0.1)
+    #Handle Input Events
+        for event in pygame.event.get():
+            if event.type == QUIT:
+                quit()
+            elif event.type == KEYDOWN and event.key == K_ESCAPE:
+                return
+            elif event.type == MOUSEBUTTONDOWN:
+               return
+            elif event.type is MOUSEBUTTONUP:
+                return
+        
+
+    #Draw Everything
+        background.fill((0,0,0))
+        render_world(world,background,white,red)
+        screen.blit(background, (0, 0))
+        pygame.display.flip()
+        
+
+#Game Over
+
+
+#this calls the 'main' function when this script is executed
+if __name__ == '__main__': main()

src/pgBodyObject.c

 static PyObject* _Body_getRestitution (PyBodyObject* body,void* closure);
 static int _Body_setRestitution (PyBodyObject* body,PyObject* value,
     void* closure);
+static PyObject* _Body_getLinearVelDamping (PyBodyObject* body,void* closure);
+static int _Body_setLinearVelDamping (PyBodyObject* body,PyObject* value,void* closure);
+static PyObject* _Body_getAngleVelDamping (PyBodyObject* body,void* closure);
+static int _Body_setAngleVelDamping (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);
       "The restitution of the Body. This influences the collision behavior\n"
       "of the collision (inelastic to elastic).",
       NULL },
-    { "friction", (getter) _Body_getFriction, (setter) _Body_setFriction,
-      "The friction of the body surface. Currently not supported.", NULL },
+    { "linear_vel_damping", (getter) _Body_getLinearVelDamping, (setter) _Body_setLinearVelDamping,
+      "the damping coefficient of linear velocity,must be in [0.0,1.0].", NULL },
+	{ "angle_vel_damping", (getter) _Body_getAngleVelDamping, (setter) _Body_setAngleVelDamping,
+	"the damping coefficient of linear velocity,must be in [0.0,1.0].", NULL },
+	{ "friction", (getter) _Body_getFriction, (setter) _Body_setFriction,
+	"The friction of the body surface. Currently not supported.", NULL },
     { "velocity",(getter)_Body_getVelocity,(setter)_Body_setVelocity,
       "The velocity of the body as a (x, y) tuple. This describes the\n"
       "velocity in x and y direction", NULL },
 }
 
 /**
+* Getter for Body.linear_vel_damping
+*/
+static PyObject* _Body_getLinearVelDamping (PyBodyObject* body,void* closure)
+{
+	return PyFloat_FromDouble (body->fLinearVelDamping);
+}
+
+/**
+* Setter for Body.linear_vel_damping = x
+*/
+static int _Body_setLinearVelDamping (PyBodyObject* body,PyObject* value,void* closure)
+{
+	if (PyNumber_Check (value))
+	{
+		PyObject *tmp = PyNumber_Float (value);
+
+		if (tmp)
+		{
+			double damping = PyFloat_AsDouble (tmp);
+			Py_DECREF (tmp);
+			if (PyErr_Occurred ())
+				return -1;
+			if (damping < 0.0 || damping > 1.0)
+			{
+				PyErr_SetString(PyExc_ValueError,
+					"linear_vel_damping must not be in [0.0,1.0]");
+				return -1;
+			}
+			body->fLinearVelDamping = damping;
+			return 0;
+		}
+	}
+	PyErr_SetString (PyExc_TypeError, "linear_vel_damping must be a float");
+	return -1;
+}
+
+/**
+* Getter for Body.angle_vel_damping
+*/
+static PyObject* _Body_getAngleVelDamping (PyBodyObject* body,void* closure)
+{
+	return PyFloat_FromDouble (body->fAngleVelDamping);
+}
+
+/**
+* Setter for Body.angle_vel_damping = x
+*/
+static int _Body_setAngleVelDamping (PyBodyObject* body,PyObject* value,void* closure)
+{
+	if (PyNumber_Check (value))
+	{
+		PyObject *tmp = PyNumber_Float (value);
+
+		if (tmp)
+		{
+			double damping = PyFloat_AsDouble (tmp);
+			Py_DECREF (tmp);
+			if (PyErr_Occurred ())
+				return -1;
+			if (damping < 0.0 || damping > 1.0)
+			{
+				PyErr_SetString(PyExc_ValueError,
+					"angle_vel_damping must not be in [0.0,1.0]");
+				return -1;
+			}
+			body->fAngleVelDamping = damping;
+			return 0;
+		}
+	}
+	PyErr_SetString (PyExc_TypeError, "angle_vel_damping must be a float");
+	return -1;
+}
+
+/**
  * Setter for Body.static = x
  */
 static PyObject* _Body_getBStatic (PyBodyObject* body,void* closure)
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.