def specialKeyEvent(key, x, y): #Left arrow to decrease x velocity if key == GLUT_KEY_LEFT: BouncingBallModel.changeVelocity( 'glutSolidSphere', BouncingBallModel.objects['glutSolidSphere']['velocity'][0] - .01, 0) #Right arrow to increase x velocity elif key == GLUT_KEY_RIGHT: BouncingBallModel.changeVelocity( 'glutSolidSphere', BouncingBallModel.objects['glutSolidSphere']['velocity'][0] + .01, 0) #Up arrow to increase y velocity elif key == GLUT_KEY_UP: BouncingBallModel.changeVelocity( 'glutSolidSphere', BouncingBallModel.objects['glutSolidSphere']['velocity'][1] + .01, 1) #Down arrow to decrease y velocity elif key == GLUT_KEY_DOWN: BouncingBallModel.changeVelocity( 'glutSolidSphere', BouncingBallModel.objects['glutSolidSphere']['velocity'][1] - .01, 1)
def reshape(w, h): global left,right,bottom,top glViewport(0, 0, w, h) glMatrixMode (GL_PROJECTION) glLoadIdentity() if w <= h: left = -2.5 right = 2.5 bottom = -2.5 * h/w top = 2.5 * h/w else: left = 2.5 * w/h right = 2.5 * w/h bottom = -2.5 top = 2.5 glOrtho(left,right,bottom,top, -10.0, 10.0) BouncingBallModel.setScreenBoundries(left + radius, right - radius, bottom + radius, top - radius) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def reshape(w, h): global left, right, bottom, top glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() if w <= h: left = -2.5 right = 2.5 bottom = -2.5 * h / w top = 2.5 * h / w else: left = 2.5 * w / h right = 2.5 * w / h bottom = -2.5 top = 2.5 glOrtho(left, right, bottom, top, -10.0, 10.0) BouncingBallModel.setScreenBoundries(left + radius, right - radius, bottom + radius, top - radius) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def display(): global time glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() spherePos = BouncingBallModel.updateObject('glutSolidSphere',time) glPushMatrix() glTranslatef(spherePos[0], spherePos[1], spherePos[2]) glutSolidSphere(radius, 40, 40) glPopMatrix() glutSwapBuffers() glFlush() time = time + .25
def display(): global time glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() spherePos = BouncingBallModel.updateObject('glutSolidSphere', time) glPushMatrix() glTranslatef(spherePos[0], spherePos[1], spherePos[2]) glutSolidSphere(radius, 40, 40) glPopMatrix() glutSwapBuffers() glFlush() time = time + .25
def specialKeyEvent(key, x, y): # Left arrow to decrease x velocity if key == GLUT_KEY_LEFT: BouncingBallModel.changeVelocity( "glutSolidSphere", BouncingBallModel.objects["glutSolidSphere"]["velocity"][0] - 0.01, 0 ) # Right arrow to increase x velocity elif key == GLUT_KEY_RIGHT: BouncingBallModel.changeVelocity( "glutSolidSphere", BouncingBallModel.objects["glutSolidSphere"]["velocity"][0] + 0.01, 0 ) # Up arrow to increase y velocity elif key == GLUT_KEY_UP: BouncingBallModel.changeVelocity( "glutSolidSphere", BouncingBallModel.objects["glutSolidSphere"]["velocity"][1] + 0.01, 1 ) # Down arrow to decrease y velocity elif key == GLUT_KEY_DOWN: BouncingBallModel.changeVelocity( "glutSolidSphere", BouncingBallModel.objects["glutSolidSphere"]["velocity"][1] - 0.01, 1 )
def keyEvent(key, x, y): #Press the space bar to pause or unpause the boucning ball if key == chr(32): BouncingBallModel.setPause(not BouncingBallModel.pause)
BouncingBallModel.setScreenBoundries(left + radius, right - radius, bottom + radius, top - radius) glMatrixMode(GL_MODELVIEW) glLoadIdentity() def Timer(value): glutPostRedisplay() glutTimerFunc(refresh, Timer, 0) if __name__ == '__main__': glutInit(sys.argv) glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) glutInitWindowSize (screenWidth, screenHeight) glutCreateWindow('Bouncing Ball') init() glutReshapeFunc(reshape) glutDisplayFunc(display) glutTimerFunc(0, Timer, 0) glutKeyboardFunc(BouncingBallController.keyEvent) glutSpecialFunc(BouncingBallController.specialKeyEvent) BouncingBallModel.addObjects('glutSolidSphere', [0,5,0], 0, [0.0,0.1,0]) print "Instructions:" print "~ To pause or restart the ball press the space bar" print "~ To decrease the speed in the x direction, press the left arrow" print "~ To increase the speed in the x direction, press the right arrow" print "~ To increase the speed in the y direction, press the up arrow" print "~ To decrease the speed in the x direction, press the down arrow" glutMainLoop()
def keyEvent(key, x, y): # Press the space bar to pause or unpause the boucning ball if key == chr(32): BouncingBallModel.setPause(not BouncingBallModel.pause)
glLoadIdentity() def Timer(value): glutPostRedisplay() glutTimerFunc(refresh, Timer, 0) if __name__ == '__main__': glutInit(sys.argv) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) glutInitWindowSize(screenWidth, screenHeight) glutCreateWindow('Bouncing Ball') init() glutReshapeFunc(reshape) glutDisplayFunc(display) glutTimerFunc(0, Timer, 0) glutKeyboardFunc(BouncingBallController.keyEvent) glutSpecialFunc(BouncingBallController.specialKeyEvent) BouncingBallModel.addObjects('glutSolidSphere', [0, 5, 0], 0, [0.0, 0.1, 0]) print "Instructions:" print "~ To pause or restart the ball press the space bar" print "~ To decrease the speed in the x direction, press the left arrow" print "~ To increase the speed in the x direction, press the right arrow" print "~ To increase the speed in the y direction, press the up arrow" print "~ To decrease the speed in the x direction, press the down arrow" glutMainLoop()