-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathball.cpp
More file actions
45 lines (36 loc) · 1023 Bytes
/
ball.cpp
File metadata and controls
45 lines (36 loc) · 1023 Bytes
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
#include "ball.h"
void Ball::Update() {
position.x += velocity[0];
position.y += velocity[1];
// check bounds of camera only if not outside them
if (!out_of_bounds) {
if (position.y - radius <= 0 || position.y + radius >= cam_height) {
velocity[1] *= -1;
out_of_bounds = true;
}
}
else {
if (position.x - radius > 0 && position.x + radius < cam_width &&
position.y - radius > 0 && position.y + radius < cam_height)
out_of_bounds = false;
}
}
void Ball::Draw(Mat* image) {
circle(*image, position, radius, color, -1);
circle(*image, position, radius, (0,0,0), 3);
}
void Ball::Reset(int side) {
SetPosition(cam_width/2, cam_height/2);
int radius = 30; // velocity magnitude
srand(time(NULL));
int rand_side = rand() % 2;
int y = rand() % (radius * 2) - radius;
int x = sqrt(pow(radius, 2) - pow(y, 2));
if (side == 0) {
x *= -1;
} else if (side == 1) {
if (rand_side == 0)
x *= -1;
}
SetVelocity(float(x), float(y));
}