import sys, time, math 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(40) camera.moveUp(5) light = Light() GravityAccel = Vector([0, -9.8, 0]) AirDrag = 1 BounceCoeff = 0.8 windActive = False WindForce = Vector([2, 0, -2]) 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.Black.clear() camera.apply() light.apply() for o in objects: o.draw() for s in springs: s.draw() 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 prevTime = time.time() def update(): global prevTime, active t = time.time() dt = t - prevTime prevTime = t 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()