Sections
Timeline
Sub-Sections
Download
Unified Diff
Zip Archive
Metanav
Preferences
About Trac
Links
Slowchop Studios
Gerald Kaszuba
Advertisement

Changeset 33

Show
Ignore:
Timestamp:
09/03/08 07:22:02 (10 months ago)
Author:
gak
Message:
 
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • muckaround/pyode/test.py

    r32 r33  
     1import time 
    12import math, sys 
    23import random 
     
    1314    world = WorldRenderable() 
    1415    world.set_gravity((0, 0, -100)) 
     16    world.add_floor() 
    1517 
    1618    world.initGL() 
     
    2325 
    2426        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) ) 
    2628        theta = random.uniform(0,2 * math.pi) 
    2729        ct = math.cos(theta) 
     
    3133    running = True 
    3234    a = 1 
     35    frame_time = 1. / 30 
     36 
     37    done_steps = 0 
     38    start_time = time.time() 
    3339    while not win.has_exit: 
    34         drop_object() 
    3540 
    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 
    3752        win.dispatch_events() 
    3853        win.clear() 
    3954        world.render() 
    4055        win.flip() 
     56        print 'render' 
    4157 
    4258class World: 
     
    4561        self.space = ode.Space() 
    4662        self.bodies = [] 
     63        self.contact_group = ode.JointGroup() 
    4764 
    4865    def think(self, dt): 
     66        self.space.collide(None, self.near_callback) 
    4967        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()) 
    5080 
    5181    def set_gravity(self, v): 
    5282        self.world.setGravity(v) 
     83 
     84    def add_floor(self): 
     85        self.floor = ode.GeomPlane(self.space, (0, 0, 1), 0) 
    5386 
    5487    def add_sphere(self, pos, mass, rad): 
     
    75108    def initGL(self): 
    76109        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) 
    78121 
    79122    def set_ortho(self): 
     
    138181    sys.exit(main()) 
    139182 
     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() 
    140188