-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.cpp
More file actions
83 lines (77 loc) · 5.26 KB
/
world.cpp
File metadata and controls
83 lines (77 loc) · 5.26 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
#include "world.h"
World::World(Camera *camera): currentCamera{camera} {}
void World::addObject(Object *newObject)
{
objects.push_back(newObject);
}
std::vector<Object *> *World::getObjects()
{
return &objects;
}
void World::process(sf::Time dt)
{
std::vector<Object*> temp_objects;
for (auto i_objects{objects.begin()}; i_objects != objects.end(); i_objects++) {
for (auto i_outers{i_objects + 1}; i_outers != objects.end(); i_outers++) {
if (((*i_outers)->position - (*i_objects)->position).length() < ((*i_outers)->radius + (*i_objects)->radius)) {
if ((*i_objects)->getPriority() == 2 && (*i_outers)->getPriority() == 2) {
sf::Vector2f contact = ((*i_outers)->position * (*i_objects)->getR() + (*i_objects)->position * (*i_outers)->getR()) / ((*i_objects)->getR() + (*i_outers)->getR());
sf::Vector2f vec = ((*i_objects)->getPos() - contact).normalized().rotatedBy(sf::degrees(90));
for (int i = 0; i < 10; i++) {
temp_objects.push_back(new Corpuscle(contact + vec * float(rand() % 100), (vec.rotatedBy(sf::degrees(rand() % 90 - 45)) * float(rand() % 100 + 100))));
}
for (int i = 0; i < 10; i++) {
temp_objects.push_back(new Corpuscle(contact - vec * float(rand() % 100), (-vec.rotatedBy(sf::degrees(rand() % 90 - 45)) * float(rand() % 100 + 100))));
}
}
if ((*i_objects)->getPriority() >= (*i_outers)->getPriority()) {
(*i_objects)->position = ((*i_objects)->position * (*i_objects)->mass + (*i_outers)->position * (*i_outers)->mass) / ((*i_objects)->mass + (*i_outers)->mass);
(*i_objects)->velocity = ((*i_objects)->velocity * (*i_objects)->mass + (*i_outers)->velocity * (*i_outers)->mass) / ((*i_objects)->mass + (*i_outers)->mass);
(*i_objects)->F /= (*i_objects)->mass;
(*i_objects)->mass = ((*i_objects)->mass + (*i_outers)->mass);
(*i_objects)->F *= (*i_objects)->mass;
sf::Color self{(*i_objects)->getColor()}, other{(*i_outers)->getColor()};
(*i_objects)->setColor({std::uint8_t((self.r * (*i_objects)->mass + other.r * (*i_outers)->mass) / (2.f * (((*i_objects)->mass + (*i_outers)->mass) / 2.f))), std::uint8_t((self.g * (*i_objects)->mass + other.g * (*i_outers)->mass) / (2.f * (((*i_objects)->mass + (*i_outers)->mass) / 2.f))), std::uint8_t((self.b * (*i_objects)->mass + other.b * (*i_outers)->mass) / (2.f * (((*i_objects)->mass + (*i_outers)->mass) / 2.f)))});
if (currentCamera->getTracker() == *i_outers) {
currentCamera->setTracked(*i_objects);
}
delete *i_outers;
objects.erase(i_outers);
if (i_outers == objects.end()) {
break;
}
} else {
(*i_outers)->position = ((*i_outers)->position * (*i_outers)->mass + (*i_objects)->position * (*i_objects)->mass) / ((*i_outers)->mass + (*i_objects)->mass);
(*i_outers)->velocity = ((*i_outers)->velocity * (*i_outers)->mass + (*i_objects)->velocity * (*i_objects)->mass) / ((*i_outers)->mass + (*i_objects)->mass);
(*i_outers)->F /= (*i_outers)->mass;
(*i_outers)->mass = ((*i_outers)->mass + (*i_objects)->mass);
(*i_outers)->F *= (*i_outers)->mass;
sf::Color self{(*i_outers)->getColor()}, other{(*i_objects)->getColor()};
(*i_outers)->setColor({std::uint8_t((self.r * (*i_outers)->mass + other.r * (*i_objects)->mass) / (2.f * (((*i_outers)->mass + (*i_objects)->mass) / 2.f))), std::uint8_t((self.g * (*i_outers)->mass + other.g * (*i_objects)->mass) / (2.f * (((*i_outers)->mass + (*i_objects)->mass) / 2.f))), std::uint8_t((self.b * (*i_outers)->mass + other.b * (*i_objects)->mass) / (2.f * (((*i_outers)->mass + (*i_objects)->mass) / 2.f)))});
if (currentCamera->getTracker() == *i_objects) {
currentCamera->setTracked(*i_outers);
}
delete *i_objects;
*i_objects = *i_outers;
objects.erase(i_outers);
if (i_outers == objects.end()) {
break;
}
}
continue;
}
if (((*i_outers)->position - (*i_objects)->position).lengthSquared() == 0.f){
continue;
}
sf::Vector2f dF = ((*i_outers)->position - (*i_objects)->position).normalized() * (*i_objects)->mass * (*i_outers)->mass / ((*i_outers)->position - (*i_objects)->position).lengthSquared() * 1000.f;
(*i_objects)->F += dF;
(*i_outers)->F -= dF;
}
(*i_objects)->velocity += (*i_objects)->F / (*i_objects)->mass * dt.asSeconds();
(*i_objects)->position += (*i_objects)->velocity * dt.asSeconds();
(*i_objects)->F = {0.f, 0.f};
//std::cout << (*i_objects)->position.x << std::endl;
(*i_objects)->reload();
}
objects.insert(objects.end(), temp_objects.begin(), temp_objects.end());
}