Bubba-3D  0.9.0
Awesome game engine!
BFBroadPhase.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 //
18 // Created by johan on 2015-12-05.
19 //
20 
21 #include <GameObject.h>
22 #include <Collider.h>
23 #include <timer.h>
24 #include <Logger.h>
25 #include <sstream>
26 #include <SFML/System/Clock.hpp>
27 #include "BFBroadPhase.h"
28 
29 BFBroadPhase::BFBroadPhase() {
30 
31 }
32 
33 CollisionPairList BFBroadPhase::computeCollisionPairs(Scene *scene) {
34  std::vector<GameObject*> sceneObjects = scene->getGameObjects();
35  CollisionPairList collisionPairs;
36 
37  for (auto i = sceneObjects.begin(); i != sceneObjects.end(); i++) {
38  for (auto j = i + 1; j != sceneObjects.end(); j++) {
39  GameObject* gameObject1 = *i;
40  GameObject* gameObject2 = *j;
41 
42  if(isPossiblyColliding(gameObject1,gameObject2)) {
43  collisionPairs.push_back(std::pair<GameObject *, GameObject *>(gameObject1, gameObject2));
44  }
45 
46  }
47  }
48  return collisionPairs;
49 }
50 
51 
52 bool BFBroadPhase::isPossiblyColliding(GameObject* gameObject1, GameObject* gameObject2) {
53  bool noDynamic = !gameObject1->isDynamicObject() && !gameObject2->isDynamicObject();
54  bool neverCollides = !gameObject1->collidesWith(gameObject2->getIdentifier())
55  && !gameObject2->collidesWith(gameObject1->getIdentifier());
56  if(noDynamic || neverCollides || gameObject1->isDirty() || gameObject2->isDirty() || gameObject1 == gameObject2) {
57  return false;
58  }
59  bool sphereInt = sphereIntersection(gameObject1->getTransformedSphere(),gameObject2->getTransformedSphere());
60  if(sphereInt) {
61 
62  AABB aabb1 = gameObject1->getTransformedAABB();
63  AABB aabb2 = gameObject2->getTransformedAABB();
64 
65  bool aabb = AabbAabbintersection(&aabb1, &aabb2);
66  return aabb;
67  }
68 
69  return false;
70 }
virtual CollisionPairList computeCollisionPairs(Scene *scene) override
A class for containing all information about a object in the game world.
Definition: GameObject.h:67
Definition: Scene.h:29
Definition: AABB2.h:23