From 922eab9f50495017e3514661759d14ff136a0a80 Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Sun, 3 May 2015 23:50:03 +0200 Subject: [PATCH] updated EventBox updated Colliding concept (SCENE) broken Colliding concept (MODEL) outer BBox working only --- Weave/Game.cpp | 3 --- Weave/Graphix/Model/IMesh.cpp | 4 ++-- Weave/Graphix/Model/IMesh.h | 3 ++- Weave/Graphix/Model/IMetaMesh.cpp | 6 +++--- Weave/Graphix/Model/IMetaMesh.h | 3 ++- Weave/Graphix/Model/Model.cpp | 32 ++++++++++++++++++++++++++++++- Weave/Graphix/Model/Model.h | 5 ++++- Weave/Scene/EventBox.cpp | 17 ++++++++++++++-- Weave/Scene/EventBox.h | 13 ++++++++++--- Weave/Scene/Scene.cpp | 2 +- Weave/Scene/SceneObject.cpp | 23 ++++++++++++++++++---- Weave/Scene/SceneObject.h | 6 +++++- 12 files changed, 94 insertions(+), 23 deletions(-) diff --git a/Weave/Game.cpp b/Weave/Game.cpp index a801935..e2d586a 100644 --- a/Weave/Game.cpp +++ b/Weave/Game.cpp @@ -36,9 +36,6 @@ using std::string; typedef unsigned int uint; - - - Game::Game() : playing(true) { srand((int)time(NULL)); diff --git a/Weave/Graphix/Model/IMesh.cpp b/Weave/Graphix/Model/IMesh.cpp index 51c14e1..75942ee 100644 --- a/Weave/Graphix/Model/IMesh.cpp +++ b/Weave/Graphix/Model/IMesh.cpp @@ -147,9 +147,9 @@ bool IMesh::import(const aiMesh* mesh, uint& numvertices, uint& numfaces, float* return true; } -void IMesh::isColliding(float& _overlap, float& _inside, const Model* _model, const mat4& _modelMatThis, const mat4& _modelMatOther) const +void IMesh::checkColliding(float& _overlap, float& _inside, const Model* _model, const mat4& _modelMatThis, const mat4& _modelMatOther) const { - Model::isColliding(_overlap, _inside, _model, _modelMatThis*modelMat, _modelMatOther); + Model::checkColliding(_overlap, _inside, _model, _modelMatThis*modelMat, _modelMatOther); /* nicht überschneidend also keine weiteren Tests nötig */ if (_overlap < 0) return; diff --git a/Weave/Graphix/Model/IMesh.h b/Weave/Graphix/Model/IMesh.h index 781eac9..c75f7ad 100644 --- a/Weave/Graphix/Model/IMesh.h +++ b/Weave/Graphix/Model/IMesh.h @@ -26,10 +26,11 @@ public: //void bindShader(Shader* shader); /* calls Colliding to check if self collides with model*/ - virtual void isColliding(float& overlap, float& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const; + virtual void checkColliding(float& overlap, float& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const; protected: + mat4 modelMat; // Mesh Speichern? diff --git a/Weave/Graphix/Model/IMetaMesh.cpp b/Weave/Graphix/Model/IMetaMesh.cpp index 4cbe1a8..9348c87 100644 --- a/Weave/Graphix/Model/IMetaMesh.cpp +++ b/Weave/Graphix/Model/IMetaMesh.cpp @@ -99,9 +99,9 @@ void IMetaMesh::drawBBox(Shader* _shader, const mat4& _modelMat) const } -void IMetaMesh::isColliding(float& _overlap, float& _inside, const Model* _model, const mat4& _modelMatThis, const mat4& _modelMatOther) const +void IMetaMesh::checkColliding(float& _overlap, float& _inside, const Model* _model, const mat4& _modelMatThis, const mat4& _modelMatOther) const { - Model::isColliding(_overlap, _inside, _model, _modelMatThis, _modelMatOther); + Model::checkColliding(_overlap, _inside, _model, _modelMatThis, _modelMatOther); if (min(_overlap, _inside) >= 0) { @@ -109,7 +109,7 @@ void IMetaMesh::isColliding(float& _overlap, float& _inside, const Model* _model float subOverlap = -1, subInside = -1; for (auto i = models.begin(); i != models.end(); ++i) { - _model->isColliding(subOverlap, subInside, *i, _modelMatThis, _modelMatOther); + _model->checkColliding(subOverlap, subInside, *i, _modelMatThis, _modelMatOther); if (subOverlap > _overlap) _overlap = subOverlap; if (subInside > _inside) diff --git a/Weave/Graphix/Model/IMetaMesh.h b/Weave/Graphix/Model/IMetaMesh.h index 9561fd5..5d2b3cb 100644 --- a/Weave/Graphix/Model/IMetaMesh.h +++ b/Weave/Graphix/Model/IMetaMesh.h @@ -25,9 +25,10 @@ public: void drawBBox(Shader* shader, const mat4& modelMat) const; /* calls Colliding to check if self collides with model*/ - void isColliding(float& overlap, float& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const; + void checkColliding(float& overlap, float& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const; protected: + std::list models; }; diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index af12050..0177d8e 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -240,7 +240,37 @@ void Model::useModelMat(const mat4& _model, Shader* _shader) const glUniformMatrix4fv(tmp, 1, GL_FALSE, value_ptr(_model)); } -void Model::isColliding(float& _overlap, float& _inside, const Model* _model, const mat4& _modelMatThis, const mat4& _modelMatOther) const +void Model::isColliding(float& _overlap, float& _inside, const Model* _modelThis, const Model* _modelOther, const mat4& _modelMatThis, const mat4& _modelMatOther) +{ + vec3 posA, sizeA, posB, sizeB; + float overlap, inside; + + _modelThis->getBBsp(sizeA, posA); + _modelOther->getBBsp(sizeB, posB); + + mat4 modelAR = glm::inverse(removeScale(_modelMatOther))*_modelMatThis; + mat4 modelBR = glm::inverse(removeScale(_modelMatThis))*_modelMatOther; + + vec3 posAR = (vec3)(modelAR*vec4(posA, 1.f)); + vec3 posBR = (vec3)(modelBR*vec4(posB, 1.f)); + + sizeA /= 2; + sizeB /= 2; + + vec3 sizeAR = rotateSize(sizeA, modelAR); + vec3 sizeBR = rotateSize(sizeB, modelBR); + + checkCollideByAxis(_overlap, _inside, 3, value_ptr(posAR), value_ptr(posB), value_ptr(sizeAR), value_ptr(sizeB)); + if (_overlap >= 0) + { + checkCollideByAxis(overlap, inside, 3, value_ptr(posA), value_ptr(posBR), value_ptr(sizeA), value_ptr(sizeBR)); + _overlap = min(overlap, _overlap); + _inside = min(overlap, _inside); + } + +} + +void Model::checkColliding(float& _overlap, float& _inside, const Model* _model, const mat4& _modelMatThis, const mat4& _modelMatOther) const { vec3 posA, sizeA, posB, sizeB; float overlap, inside; diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index 672c936..154c623 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -29,7 +29,8 @@ public: virtual void getBBsp(vec3& size, vec3& pos) const; /* calls Colliding to check if self collides with model*/ - virtual void isColliding(float& overlap, float& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const; + static void isColliding(float& overlap, float& inside, const Model* modelThis, const Model* modelOther, const mat4& modelMatThis, const mat4& modelMatOther); + void checkColliding(float& overlap, float& inside, const Model* model, const mat4& modelMatThis, const mat4& modelMatOther) const; static Model* getBBoxModel(); @@ -55,6 +56,8 @@ protected: static Shader* BBoxShader; static bool exBBox; + + static vec3 rotateSize(const vec3& size, const mat4& modelMat); static void checkCollideByAxis(float& overlap, float& inside, uint dim, const float* posA, const float* posB, const float* sizeA, const float* sizeB); static void scaleVec3(vec3& vec, const vec3& scale); diff --git a/Weave/Scene/EventBox.cpp b/Weave/Scene/EventBox.cpp index 516f009..7fd6f96 100644 --- a/Weave/Scene/EventBox.cpp +++ b/Weave/Scene/EventBox.cpp @@ -4,11 +4,15 @@ #include "../Graphix/Model.h" #include "../Events.h" +#include "../Graphix/Texture.h" +#include "../Message.h" -EventBox::EventBox(const mat4& _modelMat) : SceneObject(Graphix::shader_BBox,_modelMat,vec4(0.f),Model::getBBoxModel(),"") -{ +EventBox::EventBox(const mat4& _modelMat, uint _EB_TYPE) : + SceneObject(Graphix::shader_BBox, _modelMat, vec4(0.f), Model::getBBoxModel(), ""), + eb_type(_EB_TYPE) +{ } @@ -24,4 +28,13 @@ void EventBox::draw() const { if (Events::isKToggleActive(SDLK_F6)) model->drawBBox(shader, modelMat); +} + +void EventBox::collides(SceneObject* _other) +{ + if ((texture != nullptr && texture->operator std::string() == "model_player_2D.png") || _other->getTexture() != nullptr && (_other->getTexture()->operator std::string() == "model_player_2D.png")) + { + if (!ignore && !_other->ignore) + Message::info("EB Kollision!" + eb_type); + } } \ No newline at end of file diff --git a/Weave/Scene/EventBox.h b/Weave/Scene/EventBox.h index 005706a..6be1a34 100644 --- a/Weave/Scene/EventBox.h +++ b/Weave/Scene/EventBox.h @@ -2,17 +2,24 @@ #include "SceneObject.h" -#define EB_WINZONE 1; -#define EB_LOSTZONE 0; +#define EB_WINZONE 1 +#define EB_LOSTZONE 2 + +typedef unsigned int uint; class EventBox : public SceneObject { public: - EventBox(const mat4& modelMat); + EventBox(const mat4& modelMat, uint EB_TYPE=0); ~EventBox(); virtual void update(float); virtual void draw() const; + virtual void collides(SceneObject* other); + +protected: + uint eb_type; + }; diff --git a/Weave/Scene/Scene.cpp b/Weave/Scene/Scene.cpp index a710152..aa0034c 100644 --- a/Weave/Scene/Scene.cpp +++ b/Weave/Scene/Scene.cpp @@ -125,7 +125,7 @@ void Scene::update(float deltaT) auto j = i; for (j++; j != SceneObjects.end(); ++j) { - (*i)->collisions(*j); + SceneObject::checkCollision(*i, *j); } } diff --git a/Weave/Scene/SceneObject.cpp b/Weave/Scene/SceneObject.cpp index 1a65e9d..769dc3f 100644 --- a/Weave/Scene/SceneObject.cpp +++ b/Weave/Scene/SceneObject.cpp @@ -136,18 +136,28 @@ void SceneObject::draw() const model->drawBBox(shader, modelMat); } -void SceneObject::collisions(SceneObject* _other) +void SceneObject::checkCollision(SceneObject* _first, SceneObject* _second) { - if (collision_ignore || _other->collision_ignore) + if (_first->collision_ignore || _second->collision_ignore) return; float isOverlaping, isInside; - model->isColliding(isOverlaping,isInside,_other->model,modelMat,_other->modelMat); + Model::isColliding(isOverlaping, isInside, _first->model,_second->model, _first->modelMat, _second->modelMat); /* overlapping 1 yes, 0 touch, -1 no*/ /* inside 1 yes, 0 touch, -1 no*/ + if (isOverlaping >= 0) + { + _first->collides(_second); + _second->collides(_first); + } + +} + +void SceneObject::collides(SceneObject* _other) +{ 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)) + if (!ignore && !_other->ignore) Message::info("Kollision"); } } @@ -223,3 +233,8 @@ Shader* SceneObject::getShader() const { return shader; } + +Texture* SceneObject::getTexture() const +{ + return texture; +} diff --git a/Weave/Scene/SceneObject.h b/Weave/Scene/SceneObject.h index 7e8712b..80a353a 100644 --- a/Weave/Scene/SceneObject.h +++ b/Weave/Scene/SceneObject.h @@ -26,7 +26,9 @@ public: virtual void update(float); virtual void draw() const; - virtual void collisions(SceneObject*); + static void checkCollision(SceneObject* first, SceneObject* second); + + virtual void collides(SceneObject* other); virtual vec3 getPosition() const; @@ -46,6 +48,8 @@ public: Shader* getShader() const; + Texture* getTexture() const; + __declspec(deprecated) void setEnemy(bool isenemy); __declspec(deprecated) -- 2.47.3