| | 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]) |
| 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() |
| 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() |