James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Revision:
8:1ab6d90c4d60
Parent:
7:ef162a6232ca
Child:
9:f6f0f39538c7
--- a/PongEngine/PongEngine.cpp	Sun Apr 21 12:45:11 2019 +0000
+++ b/PongEngine/PongEngine.cpp	Sun Apr 21 19:40:05 2019 +0000
@@ -197,11 +197,30 @@
         (ball_pos.y <= _p1y + _paddle_height)  //top
     ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
         // if it has, fix position and reflect x velocity  
-        ball_pos.y = _p1y + _paddle_height;
-        ball_velocity.y = -ball_velocity.y;
-        // audio feedback
         pad.tone(1000.0,0.1);
+    
+        if (ball_pos.x == p1_pos.x + PADDLE_WIDTH/2) {
+            ball_pos.y = _p1y + _paddle_height;
+            ball_velocity.y = -ball_velocity.y;
+        }
+        else if (ball_pos.x <= p1_pos.x + PADDLE_WIDTH/2) {
+            float ang = 30*(((p1_pos.x + PADDLE_WIDTH/2)-ball_pos.x)/(PADDLE_WIDTH/2 - p1_pos.x)) + 30;  //converts the distance from the centre to an angle between 30 and 60
+            if (ball_velocity.x > 0) {
+                ball_velocity.x = -ball_velocity.x;
+            }
+            ball_velocity.y = -tan(ang);
+        }
+        else if (ball_pos.x >= p1_pos.x + PADDLE_WIDTH/2) {
+            float ang = 30*(((p1_pos.x + PADDLE_WIDTH/2)-ball_pos.x)/(PADDLE_WIDTH/2 - p1_pos.x)) + 30;  //converts the distance from the centre to an angle between 30 and 60
+            if (ball_velocity.x < 0) {
+                ball_velocity.x = -ball_velocity.x;
+            }
+            ball_velocity.y = -tan(ang);
+        }
     }
+    
+    // check ballxpos in relation to paddle xpos. translate the distance from the centre to an angle between 30 and 60 degrees in that direction
+    
 
     // write new attributes
     _ball.set_velocity(ball_velocity);