Added a randomly generated multiplier to the offset, when the ball collides with a paddle

master
Aadhavan Srinivasan 11 months ago
parent 2940c61314
commit b5a8fd599d

@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include <ctime>
#include <raylib-cpp.hpp> #include <raylib-cpp.hpp>
#include "paddle.hpp" #include "paddle.hpp"
#include "ball.hpp" #include "ball.hpp"
@ -12,20 +13,19 @@ const int RECT_H = HEIGHT / 3;
const int RECT_W = 30; const int RECT_W = 30;
const int PADDLE_SPEED = 8; const int PADDLE_SPEED = 8;
const int CIRC_RAD = 10; const int CIRC_RAD = 10;
const float BASE_BOUNCE_DEG = 45; const float BASE_BOUNCE_DEG = 60;
const float BASE_BOUNCE_RAD = (BASE_BOUNCE_DEG / 180.0) * M_PI; const float BASE_BOUNCE_RAD = (BASE_BOUNCE_DEG / 180.0) * M_PI;
const float BASE_SPEED_COMPONENTS = 15; const float BASE_SPEED_COMPONENTS = 15;
const float BASE_SPEED = sqrt(powf(BASE_SPEED_COMPONENTS, 2) * 2); const float BASE_SPEED = sqrt(powf(BASE_SPEED_COMPONENTS, 2) * 2);
raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) { raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
float paddle_mid_y = (paddle.getRect().y + paddle.getRect().GetHeight()) / 2.0; /* Middle y value of rectangle */ float paddle_mid_y = (paddle.getRect().y + paddle.getRect().GetHeight()) / 2.0; /* Middle y value of rectangle */
TraceLog(LOG_INFO, "\nMid y-value: %.2f", paddle_mid_y);
float ball_y = ball.pos.y; /* Y co-ordinate of ball */ float ball_y = ball.pos.y; /* Y co-ordinate of ball */
float offset = paddle_mid_y - ball_y; /* Subtracting the ball coordinate will give us a value between -paddle_mid_y (represents bottom of paddle) and +paddle_mid_y (represents top of paddle) */ float offset = paddle_mid_y - ball_y; /* Subtracting the ball coordinate will give us a value between -paddle_mid_y (represents bottom of paddle) and +paddle_mid_y (represents top of paddle) */
offset /= (paddle.getRect().GetHeight() / 2.0); /* Normalize the value, by dividing it by its maximum magnitude. It is now a value between -1 and 1. */ offset /= (paddle.getRect().GetHeight()); /* Normalize the value, by dividing it by its maximum magnitude. It is now a value between -1 and 1. */
TraceLog(LOG_INFO, "Offset: %.2f\n",offset);
// offset *= -1; /* Reverse the sign of the offset, so that -1 represents the top, and 1 represents the bottom */ offset *= 0.8 + (float)(std::rand()) / (float) (RAND_MAX / ( 1.2 - 0.8)); // Generate a random float from 0.8 to 1.2
float bounce_angle = offset * BASE_BOUNCE_RAD; /* Calculate the actual bounce angle from the base bounce angle. */ float bounce_angle = offset * BASE_BOUNCE_RAD; /* Calculate the actual bounce angle from the base bounce angle. */
@ -39,7 +39,7 @@ raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
} }
int main() { int main() {
/* Initialize window */ /* Initialize window and other variables */
raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong"); raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong");
SetTraceLogLevel(LOG_INFO); SetTraceLogLevel(LOG_INFO);
window.ClearBackground(BLACK); window.ClearBackground(BLACK);
@ -47,6 +47,7 @@ int main() {
SetExitKey(KEY_Q); SetExitKey(KEY_Q);
std::string points_str = std::string("0\t\t0"); std::string points_str = std::string("0\t\t0");
bool game_started = false; bool game_started = false;
srand(std::time(NULL));
/* Instantiate Paddle and Ball objects */ /* Instantiate Paddle and Ball objects */
Paddle pad1 = Paddle(10, (HEIGHT / 2) - (RECT_H / 2), RECT_W, RECT_H); Paddle pad1 = Paddle(10, (HEIGHT / 2) - (RECT_H / 2), RECT_W, RECT_H);

Loading…
Cancel
Save