import sys, time, math from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * camera = OrthoCamera(-10,10, -10,10, -10,10) light = Light() material = Material(diffuse=Color.Blue) ballXform = SimpleTransform() ballPos = Vector([0, 0, 0]) ballVel = Vector([3, 0, 0]) ballAcc = Vector([0, 0, 0]) gravity = Vector([0, -9.8, 0]) active = False def draw(): Color.Black.clear() camera.apply() light.apply() material.apply() ballXform.pushApply() gluSphere(quadric, 1.0, 32, 16) ballXform.pop() material.disable() glutSwapBuffers() def keyboard(key, x, y): global active if key == chr(27): sys.exit(0) elif key == ' ': active = not active prevTime = time.time() def checkForBounce(): if ballPos[0] > 10.0: ballVel[0] = -ballVel[0] ballPos[0] = 10.0 - (ballPos[0] - 10.0) elif ballPos[0] < -10.0: ballVel[0] = -ballVel[0] ballPos[0] = -10.0 + (-10.0 - ballPos[0]) if ballPos[1] > 10.0: ballVel[1] = -ballVel[1] ballPos[1] = 10.0 - (ballPos[1] - 10.0) elif ballPos[1] < -10.0: ballVel[1] = -ballVel[1] ballPos[1] = -10.0 + (-10.0 - ballPos[1]) def update(): global prevTime, ballPos, ballVel, ballXform, active t = time.time() dt = t - prevTime prevTime = t if active: ballAcc = gravity ballVel += ballAcc * dt ballPos += ballVel * dt checkForBounce() ballXform.translate = ballPos 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()