-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.cpp
More file actions
60 lines (50 loc) · 1.32 KB
/
Ball.cpp
File metadata and controls
60 lines (50 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "stdafx.h"
#include "Ball.h"
Ball::Ball(){
};
Ball::Ball(int pointCount, int radius, float angle, sf::Color colour, sf::Vector2f position) {
ballAngle = 0;
ballSpeed = 1;
this->setPointCount(pointCount);
this->setRadius(radius);
this->setOrigin(radius, radius);
this->setFillColor(colour);
this->setPosition(position);
}
void Ball::MoveBall(const float &factor) {
this->move(std::cos(ballAngle)*factor*ballSpeed, std::sin(ballAngle)*factor*ballSpeed);
}
std::string Ball::BallPaddleCollision(const Paddle &playerPaddle, const int &playerFlag) {
float intersectPoint = playerPaddle.getPosition().y - this->getPosition().y;
float normalisedIntersectPoint = intersectPoint / 50;
float bounceModifier = (4 * 3.14159265359) / 15;
switch (playerFlag) {
case(1):
ballAngle = -normalisedIntersectPoint * (bounceModifier);
break;
case(2):
ballAngle = 3.14159265359 + normalisedIntersectPoint * (bounceModifier);
break;
}
if (fabsf(normalisedIntersectPoint) > 0.75) {
ballSpeed += 0.25;
return "Up";
}
else if (ballSpeed > 1) {
ballSpeed -= 0.25;
return "Down";
}
return "";
}
void Ball::InverseAngle() {
ballAngle = -ballAngle;
}
float Ball::GetAngle() {
return ballAngle;
}
void Ball::SetAngle(const float &angle) {
ballAngle = angle;
}
void Ball::SetBallSpeed(const int &speed) {
ballSpeed = 1;
}