Changeset 33
- Timestamp:
- 09/03/08 07:22:02 (10 months ago)
- Files:
-
- 1 modified
-
muckaround/pyode/test.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
muckaround/pyode/test.py
r32 r33 1 import time 1 2 import math, sys 2 3 import random … … 13 14 world = WorldRenderable() 14 15 world.set_gravity((0, 0, -100)) 16 world.add_floor() 15 17 16 18 world.initGL() … … 23 25 24 26 body = world.add_box(1000, 1.0, 0.2, 0.2) 25 body.setPosition( (random.gauss(0,0.1), 3.0,random.gauss(0,0.1)) )27 body.setPosition( (random.gauss(0,0.1), random.gauss(0,0.1), 5) ) 26 28 theta = random.uniform(0,2 * math.pi) 27 29 ct = math.cos(theta) … … 31 33 running = True 32 34 a = 1 35 frame_time = 1. / 30 36 37 done_steps = 0 38 start_time = time.time() 33 39 while not win.has_exit: 34 drop_object()35 40 36 world.think(1. / 30) 41 if random.random() < 0.1: 42 drop_object() 43 44 time_since_start = (time.time() - start_time) 45 required_steps = time_since_start / frame_time 46 47 while required_steps > done_steps: 48 world.think(frame_time) 49 done_steps += 1 50 print 'step', random.random() 51 37 52 win.dispatch_events() 38 53 win.clear() 39 54 world.render() 40 55 win.flip() 56 print 'render' 41 57 42 58 class World: … … 45 61 self.space = ode.Space() 46 62 self.bodies = [] 63 self.contact_group = ode.JointGroup() 47 64 48 65 def think(self, dt): 66 self.space.collide(None, self.near_callback) 49 67 self.world.step(dt) 68 self.contact_group.empty() 69 70 def near_callback(self, args, geom1, geom2): 71 # Check if the objects do collide 72 contacts = ode.collide(geom1, geom2) 73 74 # Create contact joints 75 for c in contacts: 76 c.setBounce(0.2) 77 c.setMu(5000) 78 j = ode.ContactJoint(self.world, self.contact_group, c) 79 j.attach(geom1.getBody(), geom2.getBody()) 50 80 51 81 def set_gravity(self, v): 52 82 self.world.setGravity(v) 83 84 def add_floor(self): 85 self.floor = ode.GeomPlane(self.space, (0, 0, 1), 0) 53 86 54 87 def add_sphere(self, pos, mass, rad): … … 75 108 def initGL(self): 76 109 glClearColor(0.9, 0.9, 0.9, 0.0) 77 glPointSize(3.0) 110 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 111 glEnable(GL_DEPTH_TEST) 112 glDisable(GL_LIGHTING) 113 glEnable(GL_LIGHTING) 114 glEnable(GL_NORMALIZE) 115 glShadeModel(GL_FLAT) 116 117 glLightfv(GL_LIGHT0,GL_POSITION, (ctypes.c_float * 4)(0,0,1,0)) 118 glLightfv(GL_LIGHT0,GL_DIFFUSE, (ctypes.c_float * 4)(1,1,1,1)) 119 glLightfv(GL_LIGHT0,GL_SPECULAR, (ctypes.c_float * 4)(1,1,1,1)) 120 glEnable(GL_LIGHT0) 78 121 79 122 def set_ortho(self): … … 138 181 sys.exit(main()) 139 182 183 import cProfile as profile 184 import pstats 185 profile.run('main()', 'profile.dat') 186 p = pstats.Stats('profile.dat') 187 p.strip_dirs().sort_stats(-1).print_stats() 140 188