Skip to content

Commit

Permalink
Collision with paddle and loss condition
Browse files Browse the repository at this point in the history
  • Loading branch information
mel-mouk committed Jul 16, 2021
1 parent b2d7b13 commit 6f99f4f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/objects/entities/ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ Ball::Ball(sf::Rect<float> constraints) : VisibleObject("../assets/ball.png") {
_speed = 500.0f;
_angle = rand() % 2 == 0 ? 0 : 180;
_constraints = constraints;
isOut = false;
}

void Ball::update(float timeElapsed) {
if (isOut) return;

float velocity = _speed * timeElapsed;
float angleInRadian = _angle * M_PI / 180.0f;
float velocityX = velocity * std::cos(angleInRadian) - 0 * std::sin(angleInRadian);
Expand All @@ -18,11 +21,31 @@ void Ball::update(float timeElapsed) {
velocityY *= -1;
}

// Temporary handling bounce with the side walls
if (getLeft() + velocityX <= _constraints.left || getRight() + velocityX >= _constraints.left + _constraints.width) {
// Temporary handling bounce with the right wall
if (getRight() + velocityX >= _constraints.left + _constraints.width) {
_angle = (180 - _angle) % 360;
velocityX *= -1;
}

// Handle loss condition on the players side
if (getLeft() + velocityX <= _constraints.left) {
isOut = true;
}

move(velocityX, velocityY);
}

void Ball::collideWith(VisibleObject *target) {
if (isOut) return;
if (!dynamic_cast<Paddle*>(target)) return;

float paddleCenterY = (target->getTop() + target->getBottom()) / 2;
float ballCenterY = (getTop() + getBottom()) / 2;
float distDiff = ballCenterY - paddleCenterY;
float maxDiff = target->getBoundingRect().height;

float normalizedDiff = distDiff / maxDiff;
_angle = (int)(normalizedDiff * 90); // Between -45 and 45 degree
_speed += 100;
if (_speed > _maxSpeed) _speed = _maxSpeed;
}
5 changes: 5 additions & 0 deletions src/objects/entities/ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
#include <cmath>

#include "../visible-object.h"
#include "paddle.h"

class Ball : public VisibleObject {
public:
bool isOut;

Ball(sf::Rect<float> constraints);
void update(float timeElapsed);
void collideWith(VisibleObject *target);

private:
int _angle;
float _speed;
float _maxSpeed = 1800.0f;
sf::Rect<float> _constraints;
};

Expand Down
2 changes: 2 additions & 0 deletions src/pang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ void Pang::gameLoop() {
currentState->draw(&_window);

_window.display();

currentState->endLoopLogic();
}
}

Expand Down
1 change: 1 addition & 0 deletions src/states/game-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class GameState {
virtual void handleInput(sf::Event *event) = 0;
virtual void update(float timeElapsed) = 0;
virtual void draw(sf::RenderWindow *window) = 0;
virtual void endLoopLogic() {};

protected:
VisibleObjectManager _visibleObjectManager;
Expand Down
17 changes: 17 additions & 0 deletions src/states/playing-state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@ void PlayingState::update(float timeElapsed) {

void PlayingState::draw(sf::RenderWindow *window) {
_visibleObjectManager.drawAll(window);
}

void PlayingState::endLoopLogic() {
Ball *ball = dynamic_cast<Ball*>(_visibleObjectManager.get("ball"));

if (ball->isOut) {
_visibleObjectManager.remove("ball");

Field *field = dynamic_cast<Field*>(_visibleObjectManager.get("field"));
sf::Rect<float> ballConstraint = sf::Rect(field->getLeft() + 10, field->getTop() + 10, field->getBoundingRect().width - 20, field->getBoundingRect().height - 20);
Ball *ball = new Ball(ballConstraint);
float centerX = field->getLeft() + field->getBoundingRect().width / 2;
float centerY = field->getTop() + field->getBoundingRect().height / 2;
sf::Rect<float> ballRect = ball->getBoundingRect();
ball->setPosition(centerX - ballRect.width / 2, centerY - ballRect.height / 2);
_visibleObjectManager.add("ball", ball);
}
}
1 change: 1 addition & 0 deletions src/states/playing-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class PlayingState: public GameState {
void handleInput(sf::Event *event) override;
void update(float timeElapsed) override;
void draw(sf::RenderWindow *window) override;
void endLoopLogic() override;
};

#endif //PANG_PLAYING_STATE_H

0 comments on commit 6f99f4f

Please sign in to comment.