Skip to content

Commit

Permalink
Improve ball bounce
Browse files Browse the repository at this point in the history
  • Loading branch information
lleballex committed Dec 4, 2023
1 parent a03b0f9 commit 1821da8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Arcanoid/App/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void App::run() {

sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "ARCANOID by lleballex");

window.setFramerateLimit(60);
window.setFramerateLimit(100);

sf::Clock clock;

Expand Down
22 changes: 20 additions & 2 deletions Arcanoid/Ball/Ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,26 @@ void Ball::draw(sf::RenderWindow *window) {

bool Ball::handlePlatformCollide(float plX, float plY, float plWidth, float plHeight) {
float *res = getRectCollision(plX, plY, plWidth, plHeight);
float newAngle = float(rand()) / RAND_MAX * M_PI / 2 - M_PI / 4;
//float newAngle = float(rand()) / RAND_MAX * M_PI / 1 - M_PI / 2;

if (!res) {
return false;
}

if ((float)rand() / RAND_MAX >= 0.3) {
float offsetX = (float)rand() / RAND_MAX * 400 - 200;
float offsetY = (float)rand() / RAND_MAX * 400 - 200;
float directionX = centerX - x + offsetX;
float directionY = centerY - y + offsetY;
float distance = sqrt(directionX * directionX + directionY * directionY);

speedX = (directionX / distance) * speed;
speedY = (directionY / distance) * speed;

return true;
}

float newAngle = float(rand()) / RAND_MAX * M_PI * 2 - M_PI;

if (abs(res[1]) > abs(res[0])) {
if (res[1] > 0) {
speedX = speed * cos(newAngle);
Expand Down Expand Up @@ -98,6 +111,11 @@ void Ball::setSpeedAngle(float angle) {
speedY = speed * sin(angle);
}

void Ball::setCenterPosition(float x, float y) {
centerX = x;
centerY = y;
}

COLOR Ball::getColor() {
return color;
}
Expand Down
3 changes: 3 additions & 0 deletions Arcanoid/Ball/Ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
class Ball : public Circle {
private:
COLOR color;

float speed = 0.4;
float centerX = 10, centerY = 10;

public:
Ball();
Expand All @@ -18,6 +20,7 @@ class Ball : public Circle {

void setColor(COLOR newColor);
void setSpeedAngle(float angle);
void setCenterPosition(float x, float y);

COLOR getColor();
};
1 change: 1 addition & 0 deletions Arcanoid/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Game::Game() {
}

ball = new Ball;
ball->setCenterPosition(SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f);
setInitBallPosition();

heartTexture = new sf::Texture;
Expand Down
1 change: 0 additions & 1 deletion Arcanoid/Home/Home.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Home::Home() {
eventManager.subscribe(EVENT::CLICK, aboutBtn, &Home::setAboutScene, this);
eventManager.subscribe(EVENT::CLICK, quitBtn, &Home::quit, this);


layout = new VerticalLayout(5, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
layout->addObject(startBtn);
layout->addObject(rulesBtn);
Expand Down

0 comments on commit 1821da8

Please sign in to comment.