Consertado spawn do mapa 2
[Projeto-PCG.git] / collision.cpp
blob86087308f7b1c928f957734677489eba4d060c22
1 #include "collision.h"
3 void CollisionManager::subscribe(Thing* thing){
4 things.insert(thing);
7 void CollisionManager::remove(Thing* thing){
8 things.erase(thing);
11 void CollisionManager::update(){
12 std::set<Thing*>::iterator it1,it2;
13 for (it1 = things.begin(); it1 != things.end(); it1++) {
14 for (it2 = it1; it2 != things.end(); it2++) {
15 if (*it1 == *it2) continue;
16 checkCollision(*it1,*it2);
21 bool pointInsidePolygon(Ponto ponto, Polygon p) {
22 std::vector<Linha>::iterator it;
23 Ponto infinito(0xffffff,ponto.y);
24 int inter = 0;
25 Linha toinfinity(ponto,infinito);
26 for (it = p.linhas.begin(); it != p.linhas.end(); it++) {
27 if (linesIntersect(*it,toinfinity)) {
28 inter++;
31 if (inter % 2 == 1)
32 return true;
33 else
34 return false;
37 bool CollisionManager::checkCollision(Thing* a, Thing* b){
38 if (a->dead || b->dead)
39 return false;
40 if (!(a->canCollide(b) && b->canCollide(a)))
41 return false;
42 if (distance(a->getPosition(),b->getPosition()) > 500)
43 return false;
44 Polygon pa,pb;
45 pa = a->getCollision();
46 pb = b->getCollision();
47 pa.translate(a->getPosition());
48 pb.translate(b->getPosition());
49 std::vector<Linha>::iterator ita,itb;
50 if (pointInsidePolygon(pb.linhas[0].vertices[0],pa)) {
51 a->collide(b);
52 b->collide(a);
53 return true;
55 for (ita = pa.linhas.begin(); ita != pa.linhas.end(); ita++) {
56 for (itb = pb.linhas.begin(); itb != pb.linhas.end(); itb++) {
57 /*std::cout<<"A:"<<(*ita).vertices[0].x<<" "<<(*ita).vertices[0].y<<" "
58 <<(*ita).vertices[1].x<<" "<<(*ita).vertices[1].y<<std::endl;
59 std::cout<<"B:"<<(*itb).vertices[0].x<<" "<<(*itb).vertices[0].y<<" "
60 <<(*itb).vertices[1].x<<" "<<(*itb).vertices[1].y<<std::endl;*/
61 if (linesIntersect(*ita,*itb)) {
62 a->collide(b);
63 b->collide(a);
64 return true;
68 return false;