]> code.delx.au - pong/commitdiff
Speed is affected by where on the paddle the ball hits
authorJames Bunton <jamesbunton@fastmail.fm>
Sat, 23 Jun 2007 11:58:10 +0000 (21:58 +1000)
committerJames Bunton <jamesbunton@fastmail.fm>
Sat, 23 Jun 2007 11:58:10 +0000 (21:58 +1000)
pong.c

diff --git a/pong.c b/pong.c
index af2dce8a97dde7a4a165ee08bc035e9090be2aa6..32514c105d5c3b4792504a7932ab6a43d9726c9f 100644 (file)
--- a/pong.c
+++ b/pong.c
@@ -9,9 +9,13 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <math.h>
 
-#define SPEEDINC    1.15
-#define PADDLESPEED 2.5
+// Amount to increase speed by on x, y axis, and when bouncing
+#define SPEEDXINC   1.1
+#define SPEEDYINC   1.1
+#define SPEEDBINC   0.2
+#define PADDLESPEED 3.0
 #define FRAME       40
 #define PADDLESIZE  10
 #define HEIGHT      100
@@ -65,14 +69,16 @@ static void run(void) {
        // Check for collisions with paddles
        if(ballVecX < 0 && ballX <= -HEIGHT + -ballVecX * 1.5) {
                if(ballY >= paddle1 - PADDLESIZE && ballY <= paddle1 + PADDLESIZE) {
-                       ballVecX *= -SPEEDINC;
-                       ballVecY *= SPEEDINC;
+                       GLdouble speedChange = fmax(1.0, fabs(paddle1 - ballY) * SPEEDBINC);
+                       ballVecX *= -speedChange * SPEEDXINC;
+                       ballVecY *= speedChange * SPEEDYINC;
                }
        }
        if(ballVecX > 0 && ballX >=  HEIGHT - ballVecX * 1.5) {
                if(ballY >= paddle2 - PADDLESIZE && ballY <= paddle2 + PADDLESIZE) {
-                       ballVecX *= -SPEEDINC;
-                       ballVecY *= SPEEDINC;
+                       GLdouble speedChange = fmax(1.0, fabs(paddle2 - ballY) * SPEEDBINC);
+                       ballVecX *= -speedChange * SPEEDXINC;
+                       ballVecY *= speedChange * SPEEDYINC;
                }
        }