ELEC2645 (2018/19) / Mbed 2 deprecated el17ebs

Dependencies:   mbed FATFileSystem

Revision:
10:9f54a6366e94
Parent:
9:bc34f2243e43
Child:
11:6d2027253aa9
--- a/Ball/Ball.cpp	Sat Apr 20 10:42:17 2019 +0000
+++ b/Ball/Ball.cpp	Tue Apr 23 12:03:38 2019 +0000
@@ -40,8 +40,8 @@
 
 void Ball::drawPower(N5110 &lcd, float mag)
 {     
-    lcd.drawRect(0,10,36,6,FILL_TRANSPARENT);
-    lcd.drawRect(0,10,36*mag,6,FILL_BLACK);
+    lcd.drawRect(0,1,36,6,FILL_TRANSPARENT);
+    lcd.drawRect(0,1,36*mag,6,FILL_BLACK);
 }
 
 
@@ -53,12 +53,12 @@
 
 }
 
-void Ball::move_ball(int frame_rate)
+void Ball::move_ball()
 {
-    _x_pos = _x_pos + _x_vel*10.0f/frame_rate; //move ball position at rate proportional to velocity in each direction  
-    _y_pos = _y_pos + _y_vel*10.0f/frame_rate;   
-    _x_vel = _x_vel*(1.0f-(0.6f/frame_rate)); //ball slows down each loop caled by time between frames to ensure same movement at different frame rates
-    _y_vel = _y_vel*(1.0f-(0.6f/frame_rate));
+    _x_pos = _x_pos + _x_vel*10.0f/_frame_rate; //move ball position at rate proportional to velocity in each direction  
+    _y_pos = _y_pos + _y_vel*10.0f/_frame_rate;   
+    _x_vel = _x_vel*(1.0f-(0.6f/_frame_rate)); //ball slows down each loop caled by time between frames to ensure same movement at different frame rates
+    _y_vel = _y_vel*(1.0f-(0.6f/_frame_rate));
     if(_x_vel != 0 && _y_vel != 0 && abs(_x_vel) < 0.2f && abs(_y_vel) < 0.2f) { //to make ball come to complete stop once velocity is nearly 0
         _x_vel = 0;
         _y_vel = 0;
@@ -116,7 +116,7 @@
 {
     for(int i = 0; i < size; i ++) {
     
-        if(map[i].wall == LEFT) {
+        if(map[i].wall == LEFT) { //each bounce check algorithm uses wall type and start/end coordinates then bounces if the next frame ball position is past the wall
             left_bounce(map[i].start,map[i].end);
         }
         else if(map[i].wall == RIGHT) {
@@ -128,21 +128,33 @@
         else if(map[i].wall == BOTTOM) {
             bottom_bounce(map[i].start,map[i].end);
         }
+        else if(map[i].wall == BOTTOMLEFT) {
+            bottom_left_bounce(map[i].start,map[i].end);
+        }
+        else if(map[i].wall == BOTTOMRIGHT) {
+            bottom_right_bounce(map[i].start,map[i].end);
+        }
+        else if(map[i].wall == TOPLEFT) {
+            top_left_bounce(map[i].start,map[i].end);
+        }
+        else if(map[i].wall == TOPRIGHT) {
+            top_right_bounce(map[i].start,map[i].end);
+        }
     
     }
 }
 
 void Ball::left_bounce(Coord start, Coord end) //top check for left wall collision
 {
-    if(_x_pos+_x_vel - 1 < start.x && _y_pos >= start.y && _y_pos <= end.y && _x_vel < 0){ // left wall (x=9 ,26<=y<=40)
-        _x_pos = start.x + 1;
-        _x_vel = -_x_vel; //5% velocity lost on impact and bounce
+    if(_x_pos + _x_vel*10.0f/_frame_rate - 1 < start.x && _x_pos + _x_vel*10.0f/_frame_rate - 1 > start.x - 5 && _y_pos >= start.y && _y_pos + 1 <= end.y && _x_vel < 0){ // left wall 
+        _x_pos = start.x; 
+        _x_vel = -_x_vel; 
     }     
 }
 
 void Ball::right_bounce(Coord start, Coord end)
 {
-    if(_x_pos+_x_vel + 1 > start.x && _y_pos >= start.y && _y_pos <= end.y && _x_vel > 0){ //right wall x + 1
+    if(_x_pos + _x_vel*10.0f/_frame_rate + 1 > start.x && _x_pos + _x_vel*10.0f/_frame_rate + 1 < start.x + 5 && _y_pos >= start.y && _y_pos + 1 <= end.y && _x_vel > 0){ //right wall x + 1
         _x_pos = start.x - 1;
         _x_vel = -_x_vel;
     }
@@ -150,18 +162,58 @@
 
 void Ball::top_bounce(Coord start, Coord end)
 {
-    if(_y_pos+_y_vel - 1 < start.y && _x_pos >= start.x && _x_pos <= end.x && _y_vel < 0){ //top wall y -1
-        _y_pos = start.y + 1;
+    if(_y_pos + _y_vel*10.0f/_frame_rate - 1 < start.y && _y_pos + _y_vel*10.0f/_frame_rate - 1 > start.y - 5 && _x_pos >= start.x && _x_pos + 1 <= end.x && _y_vel < 0){ //top wall y -1
+        _y_pos = start.y;
         _y_vel = -_y_vel;
     }
 }
 
 void Ball::bottom_bounce(Coord start, Coord end)
 {
-    if(_y_pos+_y_vel + 1 > start.y && _x_pos >= start.x && _x_pos <= end.x && _y_vel > 0){ //bottom wall y  + 2
+    if(_y_pos + _y_vel*10.0f/_frame_rate + 2 > start.y && _y_pos + _y_vel*10.0f/_frame_rate + 2 < start.y + 5 && _x_pos >= start.x && _x_pos + 1 <= end.x && _y_vel > 0){ //bottom wall 
         _y_pos = start.y - 1;
         _y_vel = -_y_vel;
     }  
 }
+
+void Ball::bottom_left_bounce(Coord start, Coord end) 
+{
+    if((_y_pos + _y_vel*10.0f/_frame_rate + 1) > (_x_pos + _x_vel*10.0f/_frame_rate - 1) + (start.y-start.x) && _x_pos >= start.x && _x_pos <= end.x && _y_pos >= start.y  && _y_pos <= end.y ) { 
+        swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped
+    }     
+}
+
+void Ball::bottom_right_bounce(Coord start, Coord end) //start to end = left to right sides of line
+{
+    if((_x_pos + _x_vel*10.0f/_frame_rate + 1) > -(_y_pos + _y_vel*10.0f/_frame_rate + 1) + (start.x+start.y) && _x_pos >= start.x &&  _x_pos <= end.x &&_y_pos >= end.y && _y_pos <= start.y){ 
+ 
+        _x_vel = -_x_vel;
+        _y_vel = -_y_vel;
+        swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped
+    }
+}
+
+void Ball::top_left_bounce(Coord start, Coord end) 
+{
+    if((_x_pos + _x_vel*10.0f/_frame_rate - 1) < -(_y_pos + _y_vel*10.0f/_frame_rate + 1) + (start.x+start.y) && _x_pos >= start.x &&  _x_pos <= end.x &&_y_pos >= end.y && _y_pos <= start.y){ 
+        _x_vel = -_x_vel; //as is negative wall
+        _y_vel = -_y_vel;
+        
+        swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped
+    }
+}
+
+void Ball::top_right_bounce(Coord start, Coord end)
+{
+    if((_y_pos + _y_vel*10.0f/_frame_rate - 1) < (_x_pos + _x_vel*10.0f/_frame_rate + 1) + (start.y-start.x) && _x_pos >= start.x && _x_pos <= end.x && _y_pos >= start.y && _y_pos <= end.y){ 
+
+        swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped
+    }  
+}
+
+void Ball::set_frame_rate(int frame_rate)
+{
+    _frame_rate = frame_rate;
+}
 //private methods