Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Ball.cpp
00001 #include "Ball.h" 00002 #include "Paddle.h" 00003 #include <math.h> 00004 00005 Ball::Ball() 00006 { 00007 reset(); // initial parameters of ball 00008 } 00009 00010 Ball::~Ball() 00011 { 00012 } 00013 00014 void Ball::move() 00015 { 00016 GameObject::move(); 00017 00018 // Right edge 00019 if (pos.x > WIDTH-1) { 00020 velocity.x = -velocity.x; 00021 pos.x = WIDTH-1; 00022 } 00023 // Left edge 00024 else if(pos.x < 1) { 00025 velocity.x = -velocity.x; 00026 pos.x = 1; 00027 } 00028 // Top edge 00029 if (pos.y < 1) { 00030 velocity.y = -velocity.y; 00031 pos.y = 1; 00032 } 00033 // Bottom edge 00034 else if (pos.y > HEIGHT-1) { 00035 velocity.y = -velocity.y; 00036 pos.y = HEIGHT-1; 00037 } 00038 } 00039 00040 00041 #define PI 3.14159265 00042 float Deg2Rad = PI / 180; 00043 float Rad2Deg = 180 / PI; 00044 00045 // NOTE: This is how our coordinate system is setup. 00046 // angles go clock-wise, right is zero, up is -pi/2, down is pi/2 00047 // printf("angle of down: %.02f\n", atan2(1.f, 0)); 00048 // printf("angle of right: %.02f\n", atan2(0.f, 1)); 00049 // printf("angle of up: %.02f\n", atan2(-1.f, 0)); 00050 00051 // Standard rotation of a vector by given degrees 00052 // Example of this function to play around with: https://repl.it/repls/HopefulTrimWordprocessing 00053 void Rotate(Vector2D& v, float degrees) 00054 { 00055 float s = (float)sin(degrees * Deg2Rad); // stores sin value betweem -1 and 1 00056 float c = (float)cos(degrees * Deg2Rad); // stores cos value betweem -1 and 1 00057 00058 float tx = v.x; // 2D Vector (tx, ty) for the velocity 00059 float ty = v.y; 00060 00061 // Rotation: Multiply vector by rotation matrix 00062 v.x = (c * tx) - (s * ty); 00063 v.y = (s * tx) + (c * ty); 00064 } 00065 00066 /** Sets upper and lower boundary for angle 00067 * @return clamped angle between sane boundaries 00068 */ 00069 float clamp(float x, float minn, float maxx) 00070 { 00071 return min(max(x, minn), maxx); 00072 } 00073 00074 00075 void Ball::hitPad(Paddle &paddle) 00076 { 00077 const Vector2D& posPad = paddle.getPos(); 00078 if (pos.y >= posPad.y - 1 && (pos.x >= posPad.x && pos.x <= posPad.x + paddle.getW())) { 00079 // We hit the pad 00080 00081 // First: rotate about paddle's surface normal (flip upward) 00082 velocity.y = -velocity.y; 00083 00084 // Change angle based on distance to center 00085 float distanceSensitivity = 120; // Add at most this many degrees 00086 float maxDx = paddle.getW()/2; // Maximum distance from centre 00087 float cx = posPad.x + paddle.getW()/2.0f; // Maximum distance from current paddle centre 00088 float dx = pos.x - cx; // Positive dx means right of center, 0 means at center 00089 00090 float dangle = dx/maxDx * distanceSensitivity; // Delta angle, meaning change in angle 00091 float currentAngle = atan2(velocity.y, velocity.x)*Rad2Deg; // Angle of our original outgoing velocity vector 00092 float newAngle = currentAngle + dangle; // The current angle + the change in the angle 00093 00094 //printf("angle: %.02f %.02f, %.02f\n", dx, currentAngle, newAngle); 00095 00096 00097 // Always clamp angle in sane boundaries 00098 newAngle = clamp(newAngle, -160, -20); // Clamp to the range of an upward facing cone (NOTE: -90 degrees is up) 00099 00100 // Rotate the outgoing vector by the clamped dangle 00101 dangle = newAngle - currentAngle; 00102 Rotate(velocity, dangle); 00103 00104 } 00105 00106 } 00107 00108 int Ball::randomize() 00109 { 00110 AnalogIn noisy(PTB0); // Disconnected pin so will have random noise 00111 srand(1000000*noisy.read()); // Read the random noise and seed 00112 int direction = rand() % 2; // Randomise initial direction 00113 int movement; // Int to store the x-direction 00114 if (direction == 0) { 00115 movement = -1; 00116 } else if (direction == 1) { 00117 movement = 1; 00118 } 00119 return movement; 00120 } 00121 00122 void Ball::reset() 00123 { 00124 pos.x = WIDTH/2; // initial position of ball on x-axis 00125 pos.y = HEIGHT - GAP - 2; // initial position of ball on y-axis 00126 velocity.x = randomize(); // initial x-velocity of ball 00127 velocity.y = -1; // initial y-velocity of ball 00128 w = 1; // width of the ball 00129 h = 1; // height of the ball 00130 }
Generated on Thu Jul 14 2022 02:02:53 by
1.7.2