]> git.leopard-lacewing.eu Git - cgue_weave.git/commitdiff
added collideGroups
authorPeter Schaefer <schaeferpm@gmail.com>
Mon, 1 Jun 2015 23:30:34 +0000 (01:30 +0200)
committerPeter Schaefer <schaeferpm@gmail.com>
Mon, 1 Jun 2015 23:30:34 +0000 (01:30 +0200)
looks like Transform dont work

Weave/Graphix/Model/IMesh.cpp
Weave/Scene/Marvin.cpp
Weave/Scene/Scene.cpp
Weave/Scene/SceneObject.cpp
Weave/Scene/SceneObject.h

index a3a621c59867c9a06f543e6c9bb4783d604f7f1f..4d07f26e854699ac4346856d50599e22ca530e8e 100644 (file)
@@ -72,6 +72,11 @@ IMesh::IMesh(const aiMesh* _mesh, const mat4& _transformation) : modelMat(_trans
 
        bt_collision_shape = tmp_shape;
 
+       btVector3 min,max;
+       btTransform t;
+
+       bt_collision_shape->getAabb(t,min,max);
+
 #ifdef COMPARE_MTREE
        root = buildMeshTree(_transformation, numvertices, numfaces, vertex, normals, index);
 
index 1f0c1410f854b7b39b8f8d86f723c56b3d1b952f..a0268e055dece42f1fe833c5beb43692617a190e 100644 (file)
@@ -4,9 +4,11 @@
 #include "../GLM.h"
 
 
-Marvin::Marvin(Shader* _shader, const mat4& _modelMat) : SceneObject(_shader, _modelMat, vec4(7.0f, 0.7f, 1.0f, 3.0f), "Player.dae", "model_player_2D.png")
+Marvin::Marvin(Shader* _shader, const mat4& _modelMat) : 
+SceneObject(_shader, _modelMat, vec4(7.0f, 0.7f, 1.0f, 3.0f), "Player.dae", "model_player_2D.png")
 {
-       
+       collide_group = COL_MARVIN;
+       collide_with = COL_LEVEL | COL_ENEMY;
 }
 
 
index f7d59ea36b7c04aaddd769802828a870a59033b6..14fff6eae00dbef86fe2e9b3367810c5aba01513 100644 (file)
@@ -58,10 +58,10 @@ move_delta(0)
 Scene::~Scene()
 {
 
-       for (auto i = SceneObjects.begin(); i != SceneObjects.end(); ++i)
-       {
-               delete (*i);
-       }
+       //for (auto i = SceneObjects.begin(); i != SceneObjects.end(); ++i)
+       //{
+       //      delete (*i);
+       //}
 
        //Bullet
        delete bt_collision_world;
@@ -156,16 +156,29 @@ void Scene::update(float deltaT)
                        (*i)->update(deltaT);
                }
 
+               bt_collision_world->performDiscreteCollisionDetection();
+
+
+               int numManifolds = bt_collision_world->getDispatcher()->getNumManifolds();
+               //For each contact manifold
+               for (int i = 0; i < numManifolds; i++) {
+                       btPersistentManifold* contactManifold = bt_collision_world->getDispatcher()->getManifoldByIndexInternal(i);
+                       btCollisionObject* obA = const_cast<btCollisionObject*>(contactManifold->getBody0());
+                       btCollisionObject* obB = const_cast<btCollisionObject*>(contactManifold->getBody1());
+
+                       ((SceneObject*)obA->getUserPointer())->collides((SceneObject*)obB->getUserPointer(), contactManifold);
 
-               for (auto i = SceneObjects.begin(); i != SceneObjects.end(); ++i)
-               {
-                       auto j = i;
-                       for (j++; j != SceneObjects.end(); ++j)
-                       {
-                               SceneObject::checkCollision(*i, *j);
-                       }
                }
 
+               //for (auto i = SceneObjects.begin(); i != SceneObjects.end(); ++i)
+               //{
+               //      auto j = i;
+               //      for (j++; j != SceneObjects.end(); ++j)
+               //      {
+               //              SceneObject::checkCollision(*i, *j);
+               //      }
+               //}
+
        }
        else
        {
@@ -217,6 +230,9 @@ void Scene::addObject(SceneObject* _add)
 {
        SceneObjects.push_back(_add);
        ShaderSet.insert(_add->getShader());
+
+       bt_collision_world->addCollisionObject(*_add,_add->getCollideGroup(),_add->getCollideWith());
+
 //     _add->setMainScene(this);
 }
 
index 5cb5e979b4c3179867d3e6ca9f1a10df9ed0a50d..a2a159e6a05a00c58235510c720eded38c6eb7fe 100644 (file)
@@ -21,8 +21,6 @@ using std::string;
 
 #define YFALL_SPEED 9.8f
 
-#define BIT(x) 1<<(X)
-
 SceneObject::SceneObject(Shader* _shader, const mat4& _modelMat, const vec4& _material, string _modelpath, string texturepath) :
 model(nullptr),
 modelMat(_modelMat),
@@ -34,6 +32,8 @@ ySpeed(0),
 yStatic(true),
 ignore(false),
 newModel(true),
+collide_group(0),
+collide_with(0),
 bt_collision_object(new btCollisionObject())
 {
        //Message::info("Error from befor?");
@@ -46,10 +46,16 @@ bt_collision_object(new btCollisionObject())
        {
                model = Model::newIMetaModel(_modelpath);
                model->bindShader(shader);
+                       
        }
 
        if (texturepath != "")
                texture = Texture::newTexture(texturepath, _material);
+
+       if (_modelpath == "model_duck_2D.png")
+       {
+               collide_group = COL_ENEMY;
+       }
        //Message::info("Creating SkyBox Shader");
        //Graphix::getGlError();
 
@@ -59,6 +65,7 @@ bt_collision_object(new btCollisionObject())
        //Message::info("Done");
 
        bt_collision_object->setCollisionShape(*model);
+       bt_collision_object->setUserPointer(this);
 }
 
 SceneObject::SceneObject(Shader* _shader, const mat4& _modelMat, const vec4& _material, Model* _model, string texturepath) :
@@ -72,6 +79,8 @@ ySpeed(0),
 yStatic(true),
 ignore(false),
 newModel(false),
+collide_group(0),
+collide_with(0),
 bt_collision_object(new btCollisionObject())
 {
        //Message::info("Error from befor?");
@@ -92,6 +101,7 @@ bt_collision_object(new btCollisionObject())
        
        //TypeCast btCollisionShape
        bt_collision_object->setCollisionShape(*model);
+       bt_collision_object->setUserPointer(this);
 }
 
 SceneObject::~SceneObject()
@@ -101,7 +111,6 @@ SceneObject::~SceneObject()
        if (newModel)
                Model::deleteIMetaModel(*model);
 
-       delete bt_collision_object;
 }
 
 float SceneObject::getFloor() const
@@ -134,6 +143,10 @@ void SceneObject::update(float deltaT)
        //modelMat = scale(modelMat, vec3(matter));
        //modelMat = (deltaT * spin) * modelMat; //TODO drehen scheint nicht zu funktionieren
 
+       btTransform tmp;
+       tmp.setFromOpenGLMatrix(value_ptr(modelMat));
+       bt_collision_object->setWorldTransform(tmp);
+
 }
 
 void SceneObject::draw() const
@@ -170,8 +183,20 @@ void SceneObject::checkCollision(SceneObject* _first, SceneObject* _second)
 
 }
 
-void SceneObject::collides(SceneObject* _other)
+void SceneObject::collides(SceneObject* _other, btPersistentManifold* _contactManifold)
 {
+       _contactManifold->refreshContactPoints(this->operator btCollisionObject *()->getWorldTransform(), _other->operator btCollisionObject *()->getWorldTransform());
+       int numContacts = _contactManifold->getNumContacts();
+       //For each contact point in that manifold
+       for (int j = 0; j < numContacts; j++) {
+               //Get the contact information
+               btManifoldPoint& pt = _contactManifold->getContactPoint(j);
+               btVector3 ptA = pt.getPositionWorldOnA();
+               btVector3 ptB = pt.getPositionWorldOnB();
+               double ptdist = pt.getDistance();
+       }
+       Message::info("Kollision!");
+
        if ((texture != nullptr && texture->operator std::string() == "model_player_2D.png") || _other->texture != nullptr && (_other->texture->operator std::string() == "model_player_2D.png"))
        {
                if (!ignore && !_other->ignore)
@@ -262,4 +287,19 @@ Texture* SceneObject::getTexture() const
 Model* SceneObject::getModel() const
 {
        return model;
+}
+
+SceneObject::operator btCollisionObject *() const
+{
+       return bt_collision_object;
+}
+
+short SceneObject::getCollideGroup() const
+{
+       return collide_group;
+}
+
+short SceneObject::getCollideWith() const
+{
+       return collide_with;
 }
\ No newline at end of file
index 77db4528f3bde0c3233faf3bf387292f48152c0a..51d13a73f8a2789805780e6368547c9fdffabf23 100644 (file)
@@ -12,6 +12,15 @@ class Texture;
 class Shader;
 class Model;
 
+#define BIT(x) (1<<(x))
+enum collisiontypes {
+       COL_NOTHING = 0, //<Collide with nothing
+       COL_LEVEL = BIT(0), //<Collide with LEVEL
+       COL_MARVIN = BIT(1), //<Collide with Marvin
+       COL_ENEMY = BIT(2), //<Collide with Enemies
+       COL_KEY = BIT(3) //<Collide with Key's
+};
+
 //class string;
 
 class SceneObject
@@ -29,7 +38,7 @@ public:
 
        static void checkCollision(SceneObject* first, SceneObject* second);
 
-       virtual void collides(SceneObject* other);
+       virtual void collides(SceneObject* other, btPersistentManifold* contactManifold = nullptr);
 
        virtual vec3 getPosition() const;
 
@@ -63,6 +72,11 @@ public:
        bool floorBound() const;
        float getFloor() const;
 
+
+       operator btCollisionObject*() const;
+       short getCollideGroup() const;
+       short getCollideWith() const;
+
        //Falling
        bool yStatic;
        float ySpeed;
@@ -82,6 +96,8 @@ protected:
 
        bool collision_ignore;
 
+       short collide_group, collide_with;
+
        //Bullet
        btCollisionObject* bt_collision_object;