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.
Diff: Ball/Ball.cpp
- Revision:
- 12:b3ec47d606a5
- Parent:
- 10:da5743dfb137
- Child:
- 13:3585d2ea4ff4
--- a/Ball/Ball.cpp Thu May 09 11:24:12 2019 +0000 +++ b/Ball/Ball.cpp Thu May 09 11:41:58 2019 +0000 @@ -5,7 +5,7 @@ /** Constructor */ Ball::Ball() { - reset(); /** initial parameters of ball */ + reset(); // initial parameters of ball } /** Destructor - nothing happens here */ @@ -18,22 +18,22 @@ { GameObject::move(); - /** Right edge */ + // Right edge if (pos.x > WIDTH-1) { velocity.x = -velocity.x; pos.x = WIDTH-1; } - /** Left edge */ + // Left edge else if(pos.x < 1) { velocity.x = -velocity.x; pos.x = 1; } - /** Top edge */ + // Top edge if (pos.y < 1) { velocity.y = -velocity.y; pos.y = 1; } - /** Bottom edge */ + // Bottom edge else if (pos.y > HEIGHT-1) { velocity.y = -velocity.y; pos.y = HEIGHT-1; @@ -45,23 +45,23 @@ float Deg2Rad = PI / 180; float Rad2Deg = 180 / PI; -/** NOTE: This is how our coordinate system is setup. */ -/** angles go clock-wise, right is zero, up is -pi/2, down is pi/2 */ -/** printf("angle of down: %.02f\n", atan2(1.f, 0)); */ -/** printf("angle of right: %.02f\n", atan2(0.f, 1)); */ -/** printf("angle of up: %.02f\n", atan2(-1.f, 0)); */ +// NOTE: This is how our coordinate system is setup. +// angles go clock-wise, right is zero, up is -pi/2, down is pi/2 +// printf("angle of down: %.02f\n", atan2(1.f, 0)); +// printf("angle of right: %.02f\n", atan2(0.f, 1)); +// printf("angle of up: %.02f\n", atan2(-1.f, 0)); -/** Standard rotation of a vector by given degrees */ -/** Example of this function to play around with: https://repl.it/repls/HopefulTrimWordprocessing */ +// Standard rotation of a vector by given degrees */ +// Example of this function to play around with: https://repl.it/repls/HopefulTrimWordprocessing */ void Rotate(Vector2D& v, float degrees) { - float s = (float)sin(degrees * Deg2Rad); /** stores sin value betweem -1 and 1 */ - float c = (float)cos(degrees * Deg2Rad); /** stores cos value betweem -1 and 1 */ + float s = (float)sin(degrees * Deg2Rad); // stores sin value betweem -1 and 1 + float c = (float)cos(degrees * Deg2Rad); // stores cos value betweem -1 and 1 - float tx = v.x; /** 2D Vector (tx, ty) for the velocity */ + float tx = v.x; // 2D Vector (tx, ty) for the velocity float ty = v.y; - /** Rotation: Multiply vector by rotation matrix */ + // Rotation: Multiply vector by rotation matrix v.x = (c * tx) - (s * ty); v.y = (s * tx) + (c * ty); } @@ -81,28 +81,28 @@ { const Vector2D& posPad = paddle.getPos(); if (pos.y >= posPad.y - 1 && (pos.x >= posPad.x && pos.x <= posPad.x + paddle.getW())) { - /** We hit the pad */ + // We hit the pad - /** First: rotate about paddle's surface normal (flip upward) */ + // First: rotate about paddle's surface normal (flip upward) velocity.y = -velocity.y; - /** Change angle based on distance to center */ - float distanceSensitivity = 120; /** Add at most this many degrees */ - float maxDx = paddle.getW()/2; /** Maximum distance from centre */ - float cx = posPad.x + paddle.getW()/2.0f; /** Maximum distance from current paddle centre */ - float dx = pos.x - cx; /** Positive dx means right of center, 0 means at center */ + // Change angle based on distance to center + float distanceSensitivity = 120; // Add at most this many degrees + float maxDx = paddle.getW()/2; // Maximum distance from centre + float cx = posPad.x + paddle.getW()/2.0f; // Maximum distance from current paddle centre + float dx = pos.x - cx; // Positive dx means right of center, 0 means at center - float dangle = dx/maxDx * distanceSensitivity; /** Delta angle, meaning change in angle */ - float currentAngle = atan2(velocity.y, velocity.x)*Rad2Deg; /** Angle of our original outgoing velocity vector */ - float newAngle = currentAngle + dangle; /** The current angle + the change in the angle */ + float dangle = dx/maxDx * distanceSensitivity; // Delta angle, meaning change in angle + float currentAngle = atan2(velocity.y, velocity.x)*Rad2Deg; // Angle of our original outgoing velocity vector + float newAngle = currentAngle + dangle; // The current angle + the change in the angle //printf("angle: %.02f %.02f, %.02f\n", dx, currentAngle, newAngle); - /** Always clamp angle in sane boundaries */ - newAngle = clamp(newAngle, -160, -20); /** Clamp to the range of an upward facing cone (NOTE: -90 degrees is up) */ + // Always clamp angle in sane boundaries + newAngle = clamp(newAngle, -160, -20); // Clamp to the range of an upward facing cone (NOTE: -90 degrees is up) - /** Rotate the outgoing vector by the clamped dangle */ + // Rotate the outgoing vector by the clamped dangle dangle = newAngle - currentAngle; Rotate(velocity, dangle); @@ -113,10 +113,10 @@ /** Randomizes the initial x-direction of the ball */ int Ball::randomize() { - AnalogIn noisy(PTB0); /** Disconnected pin so will have random noise */ - srand(1000000*noisy.read()); /** Read the random noise and seed */ - int direction = rand() % 2; /** Randomise initial direction */ - int movement; /** Int to store the x-direction */ + AnalogIn noisy(PTB0); // Disconnected pin so will have random noise + srand(1000000*noisy.read()); // Read the random noise and seed + int direction = rand() % 2; // Randomise initial direction + int movement; // Int to store the x-direction if (direction == 0) { movement = -1; } else if (direction == 1) { @@ -128,10 +128,10 @@ /** Resets ball's initial parameters when game is over / lost */ void Ball::reset() { - pos.x = WIDTH/2; /** initial position of ball on x-axis */ - pos.y = HEIGHT - GAP - 2; /** initial position of ball on y-axis */ - velocity.x = randomize(); /** initial x-velocity of ball */ - velocity.y = -1; /** initial y-velocity of ball */ - w = 1; /** width of the ball */ - h = 1; /** height of the ball */ + pos.x = WIDTH/2; // initial position of ball on x-axis + pos.y = HEIGHT - GAP - 2; // initial position of ball on y-axis + velocity.x = randomize(); // initial x-velocity of ball + velocity.y = -1; // initial y-velocity of ball + w = 1; // width of the ball + h = 1; // height of the ball } \ No newline at end of file