Index: pong.cpp
===================================================================
--- pong.cpp	(revision 237ec283524806ce8da95be9a601a3d79fcb0479)
+++ pong.cpp	(revision 9d22ee96401b020eba946d6488e674159862e65a)
@@ -140,6 +140,8 @@
    GLint location = glGetUniformLocation(shader_program, "model");
 
-   float speed = 1.0f;
-   float last_position = 0.0f;
+   float speedX = 1.0f;
+   float speedY = 0.0f;
+   float last_position_x = 0.0f;
+   float last_position_y = 0.0f;
    float last_paddle_pos = 0.0f;
 
@@ -150,15 +152,44 @@
       previous_seconds = current_seconds;
 
-      if (last_position > 1.0f-ballRadius) {
-         speed = -speed;
-      }
-
-      if (last_position < -1.0f+ballRadius+paddleThickness &&
-         fabs(last_paddle_pos) < 0.15f) {
-         speed = -speed;
-      }
-
-      // if the ball hits the paddle on a corner, I should make it bounce
-      // off at an angle
+      if (last_position_x > 1.0f-ballRadius) {
+         speedX = -speedX;
+      }
+      if (last_position_y+ballRadius>1.0f || last_position_y-ballRadius<-1.0f) {
+         speedY = -speedY;
+      }
+
+      if (last_position_x < -1.0f+ballRadius+paddleThickness &&
+          last_position_x > -1.0f) {
+         if (fabs(last_paddle_pos-last_position_y) < 0.15f) {
+            speedX = -speedX;
+         } else {
+            // if the ball hits the paddle on a corner, make it bounce off
+            // at an angle
+            float horDist = fabs(paddleThickness-1.0f-last_position_x);
+            float verDist1 = fabs(last_paddle_pos+0.15f-last_position_y);
+            float verDist2 = fabs(last_paddle_pos-0.15f-last_position_y);
+
+            float dist1 = pow(horDist, 2)+pow(verDist1, 2);
+            float dist2 = pow(horDist, 2)+pow(verDist2, 2);
+
+            if (dist1 < ballRadius) {
+               if (speedX == -1.0f || speedY == 0.7f) {
+                  speedX = 0.7f;
+                  speedY = 0.7f;
+               } else {
+                  speedX = 1.0f;
+                  speedY = 0.0f;
+               }
+            } else if (dist2 < ballRadius) {
+               if (speedX == -1.0f || speedY == -0.7f) {
+                  speedX = 0.7f;
+                  speedY = -0.7f;
+               } else {
+                  speedX = 1.0f;
+                  speedY = 0.0f;
+               }
+            }
+         }
+      }
 
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -174,7 +205,8 @@
       glDrawArrays(GL_TRIANGLES, 0, sizeof(points_paddle)/sizeof(GLfloat)/3);
 
-      model[12] = last_position + speed*elapsed_seconds;
-      last_position = model[12];
-      model[13] = 0.0f;
+      model[12] = last_position_x + speedX*elapsed_seconds;
+      last_position_x = model[12];
+      model[13] = last_position_y + speedY*elapsed_seconds;
+      last_position_y = model[13];
       glUniformMatrix4fv(location, 1, GL_FALSE, model);
 
