Repository navigation
Expand file tree
/
Copy pathBall.cpp
More file actions
101 lines (84 loc) · 3.55 KB
/
Copy pathBall.cpp
File metadata and controls
101 lines (84 loc) · 3.55 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
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
#include "Ball.h"
void Ball::reflectOnPlayer(const Rectangle& player)
{
// it's this->m_pos as first point and this->m_pos + this->m_vel as
// second point
[[maybe_unused]]
Vector2 nextBallPos {
Vector2Add(this->m_pos, Vector2Scale(this->m_vel, GetFrameTime()))
};
DrawLineV(this->m_pos, nextBallPos, GREEN);
// Check if ball next pos touch player
if (!CheckCollisionCircleRec(nextBallPos, this->m_radius, player))
return;
// Get the line of Top face of player
Vector2 topLeftCorner {player.x, player.y};
Vector2 topRightCorner {player.x + player.width, player.y};
DrawLineV(topLeftCorner, topRightCorner, RED);
// Get the line of Bottom face of player
Vector2 bottomLeftCorner {player.x, player.y + player.height};
Vector2 bottomRightCorner {
player.x + player.width, player.y + player.height
};
DrawLineV(bottomLeftCorner, bottomRightCorner, RED);
// Get the line of Right face of player
DrawLineV(topRightCorner, bottomRightCorner, RED);
// Get the line of Left face of player
DrawLineV(topLeftCorner, bottomLeftCorner, RED);
Vector2 collisionPoint;
// Reflect ball on Right and Left faces of player
if ((this->m_pos.x > player.x + player.width
&& CheckCollisionLines(
topRightCorner, bottomRightCorner, this->m_pos, nextBallPos,
&collisionPoint
))
|| (this->m_pos.x < player.x
&& CheckCollisionLines(
topLeftCorner, bottomLeftCorner, this->m_pos, nextBallPos,
&collisionPoint
))) {
// // For debugging ball collision with players
// Ball ballHit{
// .pos = {collisionPoint.x, collisionPoint.y},
// .m_vel = {0, 0},
// .radius = this->m_radius,
// .color = RED
// };
//
// DrawCircleV(ballHit.pos, ballHit.radius, ballHit.color);
// Reflect ball on X depending on where the ball hit the player
float relativeYPos {this->m_pos.y - player.y};
float normRelativeYPos {2 * (relativeYPos / player.height) - 1};
float clampedRelativeYPos {Clamp(normRelativeYPos, -1, 1)};
float bounceAngleModifer {60.0f};
float maxBounceAngle {bounceAngleModifer * DEG2RAD};
float bounceAngle {clampedRelativeYPos * maxBounceAngle};
this->m_vel.x = -this->m_vel.x;
this->m_vel = Vector2Rotate(this->m_vel, bounceAngle);
// Prevent Weird endless steep ball bounce angle
float speed {Vector2Length(this->m_vel)};
float steepAnglePreventModifer {2.0f};
float maxY {std::abs(this->m_vel.x) * steepAnglePreventModifer};
this->m_vel.y = Clamp(this->m_vel.y, -maxY, maxY);
this->m_vel = Vector2Scale(Vector2Normalize(this->m_vel), speed);
}
// Reflect ball on Top and Bottom faces of player
if ((this->m_pos.y < player.y
&& CheckCollisionLines(
topLeftCorner, topRightCorner, this->m_pos, nextBallPos,
&collisionPoint
))
|| (this->m_pos.y > player.y + player.height
&& CheckCollisionLines(
bottomLeftCorner, bottomRightCorner, this->m_pos, nextBallPos,
&collisionPoint
))) {
Ball ballHit;
ballHit.m_color = {RED};
ballHit.m_pos = {collisionPoint.x, collisionPoint.y};
ballHit.m_vel = {0, 0};
ballHit.m_radius = {this->m_radius};
DrawCircleV(ballHit.m_pos, ballHit.m_radius, ballHit.m_color);
this->m_vel.y = -this->m_vel.y;
}
}