Skip to content

Commit 1821da8

Browse files
committed
Improve ball bounce
1 parent a03b0f9 commit 1821da8

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

Arcanoid/App/App.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void App::run() {
4747

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

50-
window.setFramerateLimit(60);
50+
window.setFramerateLimit(100);
5151

5252
sf::Clock clock;
5353

Arcanoid/Ball/Ball.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,26 @@ void Ball::draw(sf::RenderWindow *window) {
2525

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

3129
if (!res) {
3230
return false;
3331
}
3432

33+
if ((float)rand() / RAND_MAX >= 0.3) {
34+
float offsetX = (float)rand() / RAND_MAX * 400 - 200;
35+
float offsetY = (float)rand() / RAND_MAX * 400 - 200;
36+
float directionX = centerX - x + offsetX;
37+
float directionY = centerY - y + offsetY;
38+
float distance = sqrt(directionX * directionX + directionY * directionY);
39+
40+
speedX = (directionX / distance) * speed;
41+
speedY = (directionY / distance) * speed;
42+
43+
return true;
44+
}
45+
46+
float newAngle = float(rand()) / RAND_MAX * M_PI * 2 - M_PI;
47+
3548
if (abs(res[1]) > abs(res[0])) {
3649
if (res[1] > 0) {
3750
speedX = speed * cos(newAngle);
@@ -98,6 +111,11 @@ void Ball::setSpeedAngle(float angle) {
98111
speedY = speed * sin(angle);
99112
}
100113

114+
void Ball::setCenterPosition(float x, float y) {
115+
centerX = x;
116+
centerY = y;
117+
}
118+
101119
COLOR Ball::getColor() {
102120
return color;
103121
}

Arcanoid/Ball/Ball.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
class Ball : public Circle {
77
private:
88
COLOR color;
9+
910
float speed = 0.4;
11+
float centerX = 10, centerY = 10;
1012

1113
public:
1214
Ball();
@@ -18,6 +20,7 @@ class Ball : public Circle {
1820

1921
void setColor(COLOR newColor);
2022
void setSpeedAngle(float angle);
23+
void setCenterPosition(float x, float y);
2124

2225
COLOR getColor();
2326
};

Arcanoid/Game/Game.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Game::Game() {
2626
}
2727

2828
ball = new Ball;
29+
ball->setCenterPosition(SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f);
2930
setInitBallPosition();
3031

3132
heartTexture = new sf::Texture;

Arcanoid/Home/Home.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Home::Home() {
1414
eventManager.subscribe(EVENT::CLICK, aboutBtn, &Home::setAboutScene, this);
1515
eventManager.subscribe(EVENT::CLICK, quitBtn, &Home::quit, this);
1616

17-
1817
layout = new VerticalLayout(5, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
1918
layout->addObject(startBtn);
2019
layout->addObject(rulesBtn);

0 commit comments

Comments
 (0)