-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.cpp
145 lines (140 loc) · 3.83 KB
/
logic.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "logic.h"
/**
* @brief Checks whether the ball hit the corners.
*
* @param circleX
* @param circleY
* @param width
* @param height
* @return int
*/
int detectCornerCollison(int circleX, int circleY, int width, int height)
{
if (circleY - CIRCLE_RADIUS <= 0)
{ // top collison
return 0;
}
if (circleX - CIRCLE_RADIUS <= X_OFFSET)
{ // left collison
return 1;
}
if (circleX + CIRCLE_RADIUS >= width)
{ // Right Collison
return 2;
}
if (circleY + CIRCLE_RADIUS >= height - Y_OFFSET / 2)
{ // bottom collison
return 3;
}
return -1;
}
/**
* @brief Checks whether the ball hit the paddle
*
* @param circleX
* @param circleY
* @param boxPosition
* @param height
* @param direction
* @return int
*/
int detectPaddleCollision(int circleX, int circleY, int boxPosition, int height, int *direction)
{
int x1 = X_OFFSET + boxPosition;
int x2 = X_OFFSET + boxPosition + BOX_WIDTH;
int y1 = height - Y_OFFSET;
// int y2 = height;
int midpoint = (x1 + x2) / 2;
if ((circleY + CIRCLE_RADIUS >= y1) && (circleX >= x1 && circleX <= x2))
{
if (circleX <= midpoint)
{
*direction = 0; // go left
}
else
{
*direction = 1; // go right
}
return 1;
}
return -1;
}
/**
* @brief Checks whether the ball hit the blocks, and returns the block number.
*
* @param objects
* @param circleX
* @param circleY
* @param width
* @param direction
* @return int
*/
int detectCollision(int *objects, int circleX, int circleY, int width, int *direction)
{
int blockWidth = width / BLOCKS_PER_ROW;
for (int i = BLOCKS_PER_ROW * 5 - 1; i >= 0; --i)
{ // we check from bottom to top.
int row = i / 10;
int col = i % 10;
int x1 = X_OFFSET + blockWidth * col; // (x1, y1) = top left corner
int x2 = X_OFFSET + blockWidth * (col + 1); // (x2, y2) = bottom right corner
int y1 = Y_OFFSET + HEIGHT * row;
int y2 = Y_OFFSET + HEIGHT * (row + 1);
// Check for collision
if (objects[i] == 0)
{ // block needs to exist (0 = exist, 1 = gone)
// check from right collison
if ((circleX - CIRCLE_RADIUS <= x2 && circleX + CIRCLE_RADIUS >= x1) && (circleY >= y1 && circleY <= y2))
{
*direction = 0; // right
return i;
}
else if ((circleX + CIRCLE_RADIUS >= x1 && circleX + CIRCLE_RADIUS <= x2) && (circleY >= y1 && circleY <= y2))
{ // from left collison
*direction = 1; // left
return i;
}
if ((circleY + CIRCLE_RADIUS >= y1 && circleY + CIRCLE_RADIUS <= y2) && (circleX >= x1 && circleX <= x2))
{ // from top collison
*direction = 3; // top
return i;
}
else if ((circleY - CIRCLE_RADIUS <= y2 && circleY + CIRCLE_RADIUS >= y1) && (circleX >= x1 && circleX <= x2))
{ // from bottom collison
*direction = 2; // bottom
return i;
}
}
}
return -1;
}
/**
* @brief Get the Speed of the box
*
* @param width
* @param boxPosition
* @param boxDirection
* @return int
*/
int getSpeed(int width, int boxPosition, int *boxDirection)
{
if (width - BOX_WIDTH == boxPosition)
{ // reaches right corner
*boxDirection = 1; // move left
return -SPEED;
}
else if (boxPosition == 0)
{
*boxDirection = 0; // move right
return SPEED;
}
return (*boxDirection == 0 ? SPEED : -SPEED);
}
/**
* @brief A function that does absolutely nothing. Used mainly for development purposes.
*
*/
void doNothing()
{
asm volatile("nop");
}