-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircleVector.cpp.orig
More file actions
75 lines (70 loc) · 2.08 KB
/
Copy pathCircleVector.cpp.orig
File metadata and controls
75 lines (70 loc) · 2.08 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
#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;
}
}
for(iterator it = begin(); it < end(); it++)
{
for(iterator it2 = it; it2 < end(); it2++)
{
if(haveCollided(it, it2))
{
double n_x, n_y; //< vector of mirror line
n_x = it->posX - it
}
}
}
}
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);
}
}