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

Changeset 32

Show
Ignore:
Timestamp:
09/03/08 06:53:10 (10 months ago)
Author:
gak
Message:
 
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • muckaround/pyode/test.py

    r31 r32  
    44from pyglet import window 
    55from pyglet.gl import * 
     6from OpenGL.GLUT import * 
    67 
    78import ode 
     
    1415 
    1516    world.initGL() 
     17    glutInit(0) 
     18 
     19    def drop_object(): 
     20        """Drop an object into the scene.""" 
     21 
     22        global bodies, counter, objcount 
     23 
     24        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)) ) 
     26        theta = random.uniform(0,2 * math.pi) 
     27        ct = math.cos(theta) 
     28        st = math.sin(theta) 
     29        body.setRotation([ct, 0., -st, 0., 1., 0., st, 0., ct]) 
    1630 
    1731    running = True 
    1832    a = 1 
    1933    while not win.has_exit: 
     34        drop_object() 
     35 
    2036        world.think(1. / 30) 
    2137        win.dispatch_events() 
     
    2844        self.world = ode.World() 
    2945        self.space = ode.Space() 
     46        self.bodies = [] 
    3047 
    3148    def think(self, dt): 
     
    5067        geom.setBody(body) 
    5168 
     69        self.bodies.append(body) 
     70 
    5271        return body 
    5372 
     
    6786        glMatrixMode(GL_PROJECTION) 
    6887        glLoadIdentity() 
    69         gluPerspective (45,1.3333,0.2,20) 
    70         # glTranslatef(0, -100, 0); 
     88        gluPerspective (45, 1.3333, 0.2, 200) 
    7189        glMatrixMode(GL_MODELVIEW) 
    7290 
     
    7593        glClear(GL_COLOR_BUFFER_BIT) 
    7694        glLoadIdentity() 
    77         gluLookAt(0, -10, 0, 0, 0, 0, 0, 0, 1) 
     95        dist = 10 
     96        gluLookAt(dist/2, -dist, dist/2, 0, 0, 0, 0, 0, 1) 
     97 
     98        glBegin(GL_LINES) 
     99        glColor3f(1, 0, 0) 
     100        glVertex3f(0, 0, 0) 
     101        glVertex3f(1, 0, 0) 
     102        glColor3f(0, 1, 0) 
     103        glVertex3f(0, 0, 0) 
     104        glVertex3f(0, 1, 0) 
     105        glColor3f(0, 0, 1) 
     106        glVertex3f(0, 0, 0) 
     107        glVertex3f(0, 0, 1) 
     108        glEnd() 
    78109 
    79110        glColor3f(0.5,.5,.5) 
    80         q = gluNewQuadric() 
    81         gluSphere(q, 1, 10, 10) 
    82111 
    83         glBegin(GL_LINES) 
    84         glVertex3f(0, 0, 0) 
    85         glVertex3f(100, 0, 0) 
    86         glEnd() 
     112        for body in self.bodies: 
     113            self.draw_body(body) 
    87114 
    88     def drawCircle(self, x, y, r, a): 
    89         segs = 15 
    90         coef = 2.0*math.pi/segs; 
     115    def draw_box(self, body): 
     116        sx, sy, sz = body.boxsize 
     117        glScalef(sx, sy, sz) 
     118        glutSolidCube(1) 
    91119 
    92         glBegin(GL_LINE_LOOP) 
    93         for n in range(segs): 
    94             rads = n*coef 
    95             glVertex2f(r*math.cos(rads + a) + x, r*math.sin(rads + a) + y) 
    96         glVertex2f(x,y) 
    97         glEnd() 
    98  
    99     def drawCircleShape(self, circle): 
    100         body = circle.body 
    101         c = body.position + circle.center.cpvrotate(body.rotation_vector) 
    102         self.drawCircle(c.x, c.y, circle.radius, body.angle) 
    103  
    104     def drawSegmentShape(self, seg): 
    105         body = seg.body 
    106         a = body.position + seg.a.cpvrotate(body.rotation_vector) 
    107         b = body.position + seg.b.cpvrotate(body.rotation_vector) 
    108  
    109         glBegin(GL_LINES) 
    110         glVertex2f(a.x, a.y) 
    111         glVertex2f(b.x, b.y) 
    112         glEnd() 
    113  
    114     def drawPolyShape(self, poly): 
    115         body = poly.body 
    116  
    117         glBegin(GL_LINE_LOOP) 
    118         for vert in poly.verts: 
    119             v = body.position + vert.cpvrotate(body.rotation_vector) 
    120             glVertex2f(v.x, v.y) 
    121         glEnd() 
    122  
    123     def drawJoint(self, joint): 
    124         b1pos = joint._a.position 
    125         b2pos = joint._b.position 
    126         glBegin(GL_LINES) 
    127         glVertex2f(b1pos.x, b1pos.y) 
    128         glVertex2f(b2pos.x, b2pos.y) 
    129         glEnd() 
    130  
    131     def drawObject(self, shape): 
    132         if isinstance(shape, Circle): 
    133             self.drawCircleShape(shape) 
    134         elif isinstance(shape, Segment): 
    135             self.drawSegmentShape(shape) 
    136         elif isinstance(shape, Poly): 
    137             self.drawPolyShape(shape) 
    138         elif isinstance(shape, Joint): 
    139             self.drawJoint(shape) 
    140  
    141     def drawCollisions(self, arb): 
    142         for contact in arb.contacts: 
    143             glVertex2f(contact.position.x, contact.position.y) 
     120    def draw_body(self, body): 
     121        x, y, z = body.getPosition() 
     122        R = body.getRotation() 
     123        rot = [R[0], R[3], R[6], 0., 
     124               R[1], R[4], R[7], 0., 
     125               R[2], R[5], R[8], 0., 
     126               x, y, z, 1.0] 
     127        glPushMatrix() 
     128#        rot = [ ctypes.c_double(v) for v in rot ] 
     129#        rot = [ ctypes.pointer(ctypes.c_float(v)) for v in rot ] 
     130#        rot = ctypes.POINTER(rot) 
     131        rot = (c_float * 16)(*rot) 
     132        glMultMatrixf(rot) 
     133        if body.shape == "box": 
     134            self.draw_box(body) 
     135        glPopMatrix() 
    144136 
    145137if __name__ == '__main__':