import sys, time, math, os from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * from Spring import * camera = PerspCamera() camera.pitch(-20) camera.moveBackward(30) camera.moveUp(5) GravityAccel = Vector([0, -9.8, 0]) AirDrag = 1 BounceCoeff = 0.8 windActive = False WindForce = Vector([2, 0, -2]) cubetex = Texture2D(os.getcwd() + os.sep + 'box.jpg') class Object: def __init__(self, position=Vector([0,0,0]), velocity=Vector([0,0,0]), mass=1.0, radius=1.0, material=Material()): self.point = PointMass(position,velocity,mass) self.radius = radius self.material = material def draw(self): self.material.apply() glPushMatrix() glTranslatef(self.point.position[0], self.point.position[1], self.point.position[2]) gluSphere(quadric, self.radius, 16, 8) glPopMatrix() self.material.disable() def calculateForce(self): self.point.clearForce() self.point.addForce(self.point.mass * GravityAccel) self.point.addForce(self.point.velocity * -AirDrag) if windActive: self.point.addForce(WindForce) def update(self,dt): self.point.update(dt) if self.point.position[1] < 0: self.point.position[1] = -self.point.position[1] self.point.velocity[1] = self.point.velocity[1] * -0.8 num = 10 objects = [] objects.append(Object(position=Vector([-2, 14, -2]), radius=0.1)) objects.append(Object(position=Vector([2, 14, -2]), radius=0.1)) objects.append(Object(position=Vector([2, 14, 2]), radius=0.1)) objects.append(Object(position=Vector([-2, 14, 2]), radius=0.1)) objects.append(Object(position=Vector([-2, 10, -2]), radius=0.1)) objects.append(Object(position=Vector([2, 10, -2]), radius=0.1)) objects.append(Object(position=Vector([2, 10, 2]), radius=0.1)) objects.append(Object(position=Vector([-2, 10, 2]), radius=0.1)) objects[0].point.mobile = False springs = [] springs.append(Spring(objects[0].point,objects[1].point,k=100)) springs.append(Spring(objects[1].point,objects[2].point,k=100)) springs.append(Spring(objects[2].point,objects[3].point,k=100)) springs.append(Spring(objects[3].point,objects[0].point,k=100)) springs.append(Spring(objects[4].point,objects[5].point,k=100)) springs.append(Spring(objects[5].point,objects[6].point,k=100)) springs.append(Spring(objects[6].point,objects[7].point,k=100)) springs.append(Spring(objects[7].point,objects[4].point,k=100)) springs.append(Spring(objects[0].point,objects[4].point,k=100)) springs.append(Spring(objects[1].point,objects[5].point,k=100)) springs.append(Spring(objects[2].point,objects[6].point,k=100)) springs.append(Spring(objects[3].point,objects[7].point,k=100)) springs.append(Spring(objects[0].point,objects[6].point,k=200)) springs.append(Spring(objects[1].point,objects[7].point,k=200)) springs.append(Spring(objects[2].point,objects[4].point,k=200)) springs.append(Spring(objects[3].point,objects[5].point,k=200)) active = False def draw(): Color.X11['LightSkyBlue'].clear() camera.apply() Color.Green.apply() glBegin(GL_QUADS) glVertex3f(-25, 0, -25) glVertex3f(25, 0, -25) glVertex3f(25, 0, 25) glVertex3f(-25, 0, 25) glEnd() Color.White.apply() cubetex.apply() glBegin(GL_QUADS) glTexCoord2f(0, 0) glVertex3fv(objects[0].point.position) glTexCoord2f(1, 0) glVertex3fv(objects[1].point.position) glTexCoord2f(1, 1) glVertex3fv(objects[2].point.position) glTexCoord2f(0, 1) glVertex3fv(objects[3].point.position) glTexCoord2f(0, 0) glVertex3fv(objects[4].point.position) glTexCoord2f(1, 0) glVertex3fv(objects[5].point.position) glTexCoord2f(1, 1) glVertex3fv(objects[6].point.position) glTexCoord2f(0, 1) glVertex3fv(objects[7].point.position) glTexCoord2f(0, 0) glVertex3fv(objects[0].point.position) glTexCoord2f(1, 0) glVertex3fv(objects[1].point.position) glTexCoord2f(1, 1) glVertex3fv(objects[5].point.position) glTexCoord2f(0, 1) glVertex3fv(objects[4].point.position) glTexCoord2f(0, 0) glVertex3fv(objects[1].point.position) glTexCoord2f(1, 0) glVertex3fv(objects[2].point.position) glTexCoord2f(1, 1) glVertex3fv(objects[6].point.position) glTexCoord2f(0, 1) glVertex3fv(objects[5].point.position) glTexCoord2f(0, 0) glVertex3fv(objects[2].point.position) glTexCoord2f(1, 0) glVertex3fv(objects[3].point.position) glTexCoord2f(1, 1) glVertex3fv(objects[7].point.position) glTexCoord2f(0, 1) glVertex3fv(objects[6].point.position) glTexCoord2f(0, 0) glVertex3fv(objects[3].point.position) glTexCoord2f(1, 0) glVertex3fv(objects[0].point.position) glTexCoord2f(1, 1) glVertex3fv(objects[4].point.position) glTexCoord2f(0, 1) glVertex3fv(objects[7].point.position) glEnd() cubetex.disable() glutSwapBuffers() def keyboard(key, x, y): global active, windActive if key == chr(27): sys.exit(0) elif key == ' ': active = not active elif key == 'w': windActive = not windActive elif key == 'r': objects[0].point.mobile = True elif key == 't': objects[1].point.mobile = True prevTime = time.time() def update(): global prevTime, active t = time.time() dt = t - prevTime prevTime = t dt = 0.01 if active: for o in objects: o.calculateForce() for s in springs: s.calculateForce() for o in objects: o.update(dt) glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(400,400) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutIdleFunc(update) glEnable(GL_DEPTH_TEST) quadric = gluNewQuadric() glutMainLoop()