Last active
May 29, 2017 12:20
-
-
Save plq/1fa35e9193851cfee32a0304b2f408a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
randcoll: randcoll.cpp | |
g++ -g randcoll.cpp -std=c++11 -o randcoll | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This small exercise aims to propose an implementation of the main | |
* collision checking loop of a fictional 3D game engine. | |
* | |
* Our game engine supports two types of objects: Cubes and Spheres. | |
* We are trying to come up with the most elegant (ie easy to | |
* understand and maintain) solution for choosing among three possible | |
* collision check cases: Cube <=> Cube, Sphere <=> Sphere, and | |
* Cube <=> Sphere. | |
* | |
* Please fork this gist and submit a path with the following TODO items | |
* completed. | |
* | |
* TODO Items: | |
* 1. Fix std::vector definition for the object array named "objects" | |
* 2. Add 2 cubes and 2 spheres to the objects array. | |
* 3. Implement collides_with member methods for all object types. | |
* ie. cube <=> cube, cube <=> sphere and sphere <=> sphere | |
* The functions themselves are to be EMPTY!. no actual collision | |
* detection code is requested. Just make them return true or false | |
* randomly! | |
*/ | |
#include <time.h> | |
#include <vector> | |
#include <random> | |
#include <iostream> | |
#include <typeinfo> | |
bool true_or_false() { | |
return std::rand() % 2; | |
} | |
class Object; | |
class Sphere; | |
class Cube; | |
class Object { | |
public: | |
explicit Object() | |
: m_object_id(++s_num_objects) { | |
} | |
int get_object_id() const { | |
return m_object_id; | |
} | |
static int get_num_objects() { | |
return s_num_objects; | |
} | |
bool operator==(const Object &other) const { | |
return m_object_id == other.get_object_id(); | |
} | |
virtual bool collides_with(Object &obj) = 0; | |
/* TODO 3 */ | |
private: | |
static int s_num_objects; | |
int m_object_id; | |
}; | |
class Sphere: public Object { | |
/* TODO 3 */ | |
}; | |
class Cube: public Object { | |
/* TODO 3 */ | |
}; | |
std::vector<Object> objects; // FIXME 1: Fix the object type here. | |
int main(void) { | |
srand(time(NULL)); | |
/* TODO 2: add two spheres and two cubes to the objects array */ | |
for(int i = 0; i < objects.size(); ++i) { | |
auto &obj1 = objects.at(i); | |
for(int j = i; j < objects.size(); ++j) { | |
auto &obj2 = objects.at(j); | |
if (obj1 == obj2) { | |
continue; | |
} | |
/* TODO 3: implement collides_with */ | |
if (obj1.collides_with(obj2)) { | |
/* as an example | |
std::cout | |
<< "object id " << obj1.get_object_id() | |
<< " of type " << typeid(obj1).name() | |
<< " collides with " | |
<< "object id " << obj2.get_object_id() | |
<< " of type " << typeid(obj2).name(); | |
*/ | |
} | |
else { | |
/* as an example | |
std::cout | |
<< "object id " << obj1.get_object_id() | |
<< " of type " << typeid(obj1).name() | |
<< " does NOT collide with " | |
<< "object id " << obj2.get_object_id() | |
<< " of type " << typeid(obj2).name(); | |
*/ | |
} | |
} | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object id 1 of type 4Cube check Cube => Cube collides with object id 2 of type 4Cube | |
object id 1 of type 4Cube check Sphere => Cube collides with object id 3 of type 6Sphere | |
object id 1 of type 4Cube check Sphere => Cube does not collide with object id 4 of type 6Sphere | |
object id 2 of type 4Cube check Sphere => Cube collides with object id 3 of type 6Sphere | |
object id 2 of type 4Cube check Sphere => Cube does not collide with object id 4 of type 6Sphere | |
object id 3 of type 6Sphere check Sphere => Sphere collides with object id 4 of type 6Sphere |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment