import sys, time, math, os, random from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * from collide import * startdir = os.getcwd() camera = PerspCamera(far=1000) camera.pitch(-80) camera.moveBackward(200) light1 = Light() light2 = Light() light2.position = [-1, 1, -1, 0] material = Material(diffuse=Color.Blue) cow = WFObject('cow2.obj') cowBounds = WFObjectBoundingSphere(cow) cowpos = Vector([0, 0, 0]) cowvel = Vector([-1, 0, -1]) showBounds = True buildings = [ AABB([-10, 0, -10], [-5, 40, -5]), AABB([10, 0, -30], [15, 50, -25]), AABB([-5, 0, 20], [5, 20, 35]) ] buildings = [] for i in range(0,10): cx = random.uniform(-50,50) cz = random.uniform(-50,50) sx = random.uniform(5, 15) sy = random.uniform(10,60) buildings.append(AABB([cx-sx/2, 0, cz-sx/2], [cx+sx/2, sy, cz+sx/2])) def draw(): Color.X11['DeepSkyBlue'].clear() camera.apply() light1.apply() light2.apply() glPushMatrix() glTranslatef(cowpos[0], cowpos[1], cowpos[2]) cow.draw() if showBounds: cowBounds.drawWireframe() glPopMatrix() for b in buildings: b.drawWireframe() glutSwapBuffers() def keyboard(key, x, y): global cameraVelDist, showBounds if key == chr(27): sys.exit(0) elif key == '.': cameraVelDist += 4 elif key == ',': cameraVelDist += -4 elif key == 'w': accelerateCow(Vector([0, 0, -100])) elif key == 'a': accelerateCow(Vector([-100, 0, 0])) elif key == 's': accelerateCow(Vector([0, 0, 100])) elif key == 'd': accelerateCow(Vector([100, 0, 0])) elif key == ' ': showBounds = not showBounds def keyboardUp(key, x, y): global cameraVelDist if key == chr(27): sys.exit(0) elif key == '.': cameraVelDist -= 4 elif key == ',': cameraVelDist -= -4 def specialkey(key,x,y): global cameraVelPitch, cameraVelTurn if key == GLUT_KEY_LEFT: cameraVelTurn += 90 elif key == GLUT_KEY_RIGHT: cameraVelTurn -= 90 elif key == GLUT_KEY_UP: cameraVelPitch += 90 elif key == GLUT_KEY_DOWN: cameraVelPitch -= 90 def specialkeyUp(key,x,y): global cameraVelPitch, cameraVelTurn if key == GLUT_KEY_LEFT: cameraVelTurn -= 90 elif key == GLUT_KEY_RIGHT: cameraVelTurn += 90 elif key == GLUT_KEY_UP: cameraVelPitch -= 90 elif key == GLUT_KEY_DOWN: cameraVelPitch += 90 cameraVelPitch = 0.0 cameraVelTurn = 0.0 cameraVelDist = 0.0 prevTime = time.time() startTime = time.time() dt = 0 def update(): global cameraVelPitch, cameraVelTurn, cameraVelDist, startTime, cowpos, cowvel global prevTime, dt t = time.time() dt = t - prevTime prevTime = t camera.pitch(cameraVelPitch * dt) camera.turn(cameraVelTurn * dt) camera.moveForward(cameraVelDist * dt) cowpos += cowvel * dt s = Sphere(cowBounds.center + cowpos, cowBounds.radius) for b in buildings: if SphereCollidesAABB(s, b): print 'boing', t cowpos -= cowvel * dt cowvel = Vector([0,0,0]) # center = (Vector(b.min) + Vector(b.max)) / 2 # force = s.center - center # force[1] = 0 # force.normalize() # accelerateCow(force*100) glutPostRedisplay() def accelerateCow(f): global cowvel, dt cowvel += f * dt glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(400,400) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutKeyboardUpFunc(keyboardUp) glutSpecialFunc(specialkey) glutSpecialUpFunc(specialkeyUp) glutIgnoreKeyRepeat(1) glutIdleFunc(update) os.chdir(startdir) glEnable(GL_DEPTH_TEST) glEnable(GL_CULL_FACE) glutMainLoop()