-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ball.cpp
113 lines (93 loc) · 2.36 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "Ball.h"
Ball::Ball(
Vector2 position,
float radius,
float speed,
Vector2 velocity,
Vector2 acceleration,
Vector4 color
)
:position(position),
radius(radius),
speed(speed),
velocity(velocity),
acceleration(acceleration),
color(color)
{
topLeft = Vector2(
position.x,
position.y
);
bottomRight = Vector2(
position.x + radius,
position.y + radius
);
}
void Ball::Draw(SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(
renderer,
color.x,
color.y,
color.w,
color.z
);
SDL_Rect ball{
static_cast<int>(position.x),
static_cast<int>(position.y),
radius,
radius
};
SDL_RenderFillRect(renderer, &ball);
}
bool Ball::DidCollideWithPaddle(Paddle* paddle)
{
const bool hasCollided = position.x <= paddle->position.x + paddle->width &&
position.x + radius >= paddle->position.x &&
position.y <= paddle->position.y + paddle->height &&
position.y + radius >= paddle->position.y;
if (hasCollided)
SDL_Log("Colidiu com raquete");
return hasCollided;
}
void Ball::InvertVelocityOnPaddleCollide(Paddle* paddle, bool hasToUpdateSpeed)
{
if (hasToUpdateSpeed)
{
speed += 0.01f;
}
velocity.x *= -1;
#pragma region vertical collision
float topPositionBeforeCollide = (position.y + radius) - velocity.y;
float bottomPositionBeforeCollide = position.y - velocity.y;
bool isTopCollision = topPositionBeforeCollide < paddle->position.y;
bool isBottomCollision = bottomPositionBeforeCollide > paddle->position.y + paddle->height;
if (isTopCollision || isBottomCollision)
{
velocity.y *= -1;
}
# pragma endregion
}
void Ball::CheckCollisionWithAnotherBall(Ball* ball)
{
if (ball == this) return; // prevents compare a ball with itself
if (position.x <= ball->position.x + ball->radius &&
position.x + radius >= ball->position.x &&
position.y <= ball->position.y + ball->radius &&
position.y + ball->radius >= ball->position.y
)
{
velocity.x *= -1;
ball->velocity.x *= -1;
#pragma region vertical collision
float topPositionBeforeCollide = (position.y + radius) - velocity.y;
float bottomPositionBeforeCollide = position.y - velocity.y;
bool isTopCollision = topPositionBeforeCollide < ball->position.y;
bool isBottomCollision = bottomPositionBeforeCollide > ball->position.y + ball->radius;
if (isTopCollision || isBottomCollision)
{
velocity.y *= -1;
ball->velocity.y *= -1;
}
# pragma endregion
}
}