From c0ad78d511d29c1c4a216684ab101f6ac4e702c0 Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Thu, 30 Apr 2015 10:24:26 +0200 Subject: [PATCH] improved rotation Colliding (not final but works) --- Weave/Game.cpp | 4 +++- Weave/Graphix/Model/IMesh.cpp | 5 +++++ Weave/Graphix/Model/IMesh.h | 4 ++++ Weave/Graphix/Model/Model.cpp | 19 ++++++++++++++++--- Weave/Graphix/Model/Model.h | 2 ++ Weave/Scene/SceneObject.cpp | 13 +++++++++---- Weave/Scene/SceneObject.h | 4 ++++ 7 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Weave/Game.cpp b/Weave/Game.cpp index 8d079d0..b24c49d 100644 --- a/Weave/Game.cpp +++ b/Weave/Game.cpp @@ -52,7 +52,9 @@ Game::Game() : playing(true) // Shader* shaderSky = new Shader("skybox_VS.hlsl", "skybox_FS.hlsl"); // load LVL - current_world->addObject(new SceneObject(shader1, scale(3.f * vec3(1.f, .3f, 1.f)), vec4(4.0f, 1.0f, 1.0f, 2.0f), "level_test.dae", "model_levelTest_2D.png")); + SceneObject* tmp_world = new SceneObject(shader1, scale(3.f * vec3(1.f, .3f, 1.f)), vec4(4.0f, 1.0f, 1.0f, 2.0f), "level_test.dae", "model_levelTest_2D.png"); + tmp_world->ignore = true; + current_world->addObject(tmp_world); current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, 1.f, 0.f)), vec4(1.0f, 1.0f, 1.0f, 3.0f), new BBox(), "")); diff --git a/Weave/Graphix/Model/IMesh.cpp b/Weave/Graphix/Model/IMesh.cpp index 9adaad2..19edef9 100644 --- a/Weave/Graphix/Model/IMesh.cpp +++ b/Weave/Graphix/Model/IMesh.cpp @@ -149,3 +149,8 @@ bool IMesh::import(const aiMesh* mesh, uint& numvertices, uint& numfaces, float* return true; } + +void IMesh::isColliding(int& _overlap, int& _inside, const Model* _model, const mat4& _modelMatThis, const mat4& _modelMatOther) const +{ + Model::isColliding(_overlap, _inside, _model, _modelMatThis*modelMat, _modelMatOther); +} \ No newline at end of file diff --git a/Weave/Graphix/Model/IMesh.h b/Weave/Graphix/Model/IMesh.h index b36d740..164bc6a 100644 --- a/Weave/Graphix/Model/IMesh.h +++ b/Weave/Graphix/Model/IMesh.h @@ -26,6 +26,10 @@ public: //void bindShader(Shader* shader); + /* calls Colliding to check if self collides with model*/ + virtual void isColliding(int& overlap, int& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const; + + protected: mat4 modelMat; diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index b454685..15a0eae 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -251,11 +251,11 @@ void Model::isColliding(int& _overlap, int& _inside, const Model* _model,const m getBBsp(sizeA, posA); _model->getBBsp(sizeB, posB); - posA = (vec3)(_modelMatThis*vec4(posA, 1.f)); + posA = (vec3)(_modelMatThis*vec4(posA, 1.f)); //transpose sollte eigentlich reichen posB = (vec3)(_modelMatOther*vec4(posB, 1.f)); - sizeA = (mat3)_modelMatThis * sizeA; - sizeB = (mat3)_modelMatOther * sizeB; + sizeA = rotateSize(sizeA, _modelMatThis); + sizeB = rotateSize(sizeB, _modelMatOther); vec3 pDist, sDist; vec3 overlap, inside; @@ -279,6 +279,19 @@ void Model::isColliding(int& _overlap, int& _inside, const Model* _model,const m _inside = (int)sign(glm::min(glm::min(inside.x, inside.y), inside.z)); } +vec3 Model::rotateSize(const vec3& _size, const mat4& _modelMat) +{ + vec3 Size = vec3(0.f); + vec4 tmp; + for (auto i = 0; i < 3; ++i) + { + tmp = vec4(0.f); + tmp[i] = _size[i]; + Size += abs((vec3)(_modelMat* tmp)); + } + return Size; +} + Model* Model::BoundingBox = nullptr; Shader* Model::BBoxShader = nullptr; diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index da68854..d5f2df3 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -54,6 +54,8 @@ protected: static Shader* BBoxShader; static bool exBBox; + static vec3 rotateSize(const vec3& size, const mat4& modelMat); + vec3 BBmin, BBmax; private: diff --git a/Weave/Scene/SceneObject.cpp b/Weave/Scene/SceneObject.cpp index 2bac599..5d11b0e 100644 --- a/Weave/Scene/SceneObject.cpp +++ b/Weave/Scene/SceneObject.cpp @@ -29,7 +29,8 @@ mainScene(NULL), collision_ignore(false), texture(nullptr), ySpeed(0), -yStatic(true) +yStatic(true), +ignore(false) { //Message::info("Error from befor?"); //Graphix::getGlError(); @@ -61,7 +62,8 @@ mainScene(NULL), collision_ignore(false), texture(nullptr), ySpeed(0), -yStatic(true) +yStatic(true), +ignore(false) { //Message::info("Error from befor?"); //Graphix::getGlError(); @@ -159,8 +161,11 @@ void SceneObject::collisions(SceneObject* _other) model->isColliding(isOverlaping,isInside,_other->model,modelMat,_other->modelMat); /* overlapping 1 yes, 0 touch, -1 no*/ /* inside 1 yes, 0 touch, -1 no*/ - //if (isOverlaping >= 0) - // 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 (isOverlaping >= 0 && (!ignore && !_other->ignore)) + Message::info("Kollision"); + } } bool SceneObject::floorBound() const diff --git a/Weave/Scene/SceneObject.h b/Weave/Scene/SceneObject.h index 8e221e6..80a2309 100644 --- a/Weave/Scene/SceneObject.h +++ b/Weave/Scene/SceneObject.h @@ -60,8 +60,12 @@ public: bool yStatic; float ySpeed; + bool ignore; + protected: + + mat4 modelMat; vec4 material; Shader* shader; -- 2.47.3