-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.cpp
58 lines (45 loc) · 1.21 KB
/
Ball.cpp
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
#include "Ball.h"
void Ball::Update(float timeDelta){
position.x += velocity.x * timeDelta;
position.y += velocity.y * timeDelta;
shape.setOrigin(position);
}
sf::CircleShape Ball::GetShape(){
return shape;
}
void Ball::Bounce(sf::Vector2f a, sf::Vector2f b) {
if (position.y - radius < a.y) {
velocity.y = abs(velocity.y);
}
if (position.y > b.y) {
velocity.y = -abs(velocity.y);
}
}
sf::Vector2f Ball::GetPosition() {
return position;
}
void Ball::ResetPosition() {
this->position = sf::Vector2f(-400, -300);
shape.setOrigin(position);
float angle = ((float)rand() / (float)RAND_MAX) * 3.14159265 * 2;
this->velocity.x = cos(angle) * 350;
this->velocity.y = sin(angle) * 350;
if (abs(this->velocity.x) < 50) {
this->velocity.x *= 10;
}
}
void Ball::SetVelocitySign(bool positive) {
if (positive) {
this->velocity.x = abs(velocity.x);
}
else {
this->velocity.x = -abs(velocity.x);
}
}
void Ball::MultiplySpeed(float amount){
this->velocity.x *= amount;
this->velocity.y *= amount;
}
sf::Vector2f Ball::GetVelocity() {
return velocity;
}