-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircleVector.cpp
144 lines (132 loc) · 3.31 KB
/
CircleVector.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
#include "CircleVector.h"
CircleVector::CircleVector(SDL_Renderer* renderer)
: mRenderer(renderer)
{
SDL_Surface* circleSurface;
circleSurface = SDL_LoadBMP("circ.bmp");
if(circleSurface == NULL)
throw "File not found.";
diameter = circleSurface->w;
SDL_SetColorKey(circleSurface, SDL_TRUE, SDL_MapRGB(circleSurface->format, 0, 0xFF, 0xFF));
circleTexture = SDL_CreateTextureFromSurface(renderer, circleSurface);
SDL_FreeSurface(circleSurface);
}
CircleVector::~CircleVector()
{
SDL_DestroyTexture(circleTexture);
}
bool CircleVector::haveCollided(Circle* circle_1, Circle* circle_2)
{
// if distance squared less than diameter squared
if((double)(circle_1->posX - circle_2->posX) * (circle_1->posX - circle_2->posX) +
(circle_1->posY - circle_2->posY) * (circle_1->posY - circle_2->posY) <
diameter * diameter)
{
return true;
}
return false;
}
/**
* @brief Moves all the circles in the vector at constant velocity, checks for collisions with
* the screen border and other circles.
* @todo Collision detection with other circles is simple and unrealistic. The angle is not taken
* into account.
*/
void CircleVector::move()
{
for(iterator it = begin(); it < end(); it++)
{
it->posX += it->velX;
it->posY += it->velY;
if(it->posX < diameter / 2 || it->posX > 800 - diameter / 2)
{
it->velX = -it->velX;
}
if(it->posY < diameter / 2 || it->posY > 600 - diameter / 2)
{
it->velY = -it->velY;
}
it->coordinatesToVector();
}
for(iterator it = begin(); it < end(); it++)
{
for(iterator it2 = it+1; it2 < end(); it2++)
{
if(haveCollided(it, it2))
{
Vector CenterLine = it->pos - it2->pos;
Vector ContactLine;
ContactLine.x = CenterLine.y;
ContactLine.y = -CenterLine.x;
CenterLine.norm();
ContactLine.norm();
Vector i, j, i2, j2;
i = it->vel.project(CenterLine);
j = it->vel.project(ContactLine);
i2 = it2->vel.project(CenterLine);
j2 = it2->vel.project(ContactLine);
Vector temp;
temp = i;
i = i2;
i2 = temp;
it->vel = i + j;
it2->vel = i2 + j2;
it->vectorToCoordinates();
it2->vectorToCoordinates();
}
}
}
}
void CircleVector::render()
{
iterator _end = end();
for(iterator it = begin(); it < _end; it++)
{
SDL_Rect dst = { it->posX - diameter / 2, it->posY - diameter / 2, diameter, diameter };
SDL_RenderCopy(mRenderer, circleTexture, NULL, &dst);
}
}
Vector Vector::operator-(Vector& v)
{
Vector result;
result.x = x - v.x;
result.y = y - v.y;
return result;
}
Vector Vector::operator+(Vector& v)
{
Vector result;
result.x = x + v.x;
result.y = y + v.y;
return result;
}
double Vector::dotProduct(Vector& v)
{
return x * v.x + y * v.y;
}
void Vector::norm()
{
double length = sqrt(dotProduct(*this));
x /= length;
y /= length;
}
Vector Vector::project(Vector& n)
{
double length = dotProduct(n);
Vector projection;
projection.x = n.x*length;
projection.y = n.y*length;
return projection;
}
void Circle::vectorToCoordinates()
{
velX = round(vel.x);
velY = round(vel.y);
}
void Circle::coordinatesToVector()
{
pos.x = posX;
pos.y = posY;
vel.x = velX;
vel.y = velY;
}