import sys, time, math from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * from Spring import * camera = OrthoCamera(-25,25, 0,50, -25,25) light = Light() GravityAccel = Vector([0, -9.8, 0]) AirDrag = 1 BounceCoeff = 0.8 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) def update(self,dt): self.point.update(dt) num = 10 objects = [] for i in range(0,num): objects.append(Object(position=Vector([i*10.0/num, 30, 0]), mass=5.0, radius=0.1, material=Material(diffuse=Color.Green))) objects[0].point.mobile = False springs = [] for i in range(0,num-1): springs.append(Spring(objects[i].point,objects[i+1].point,k=2000)) 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 if key == chr(27): sys.exit(0) elif key == ' ': active = not active 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()