From ddd68add498befa2b5bbea0687278a601995f8aa Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Wed, 29 Apr 2015 10:35:41 +0200 Subject: [PATCH] added collision detection on outer BBox isOverlapping & isInside --- Weave/Graphix/GLM.h | 2 +- Weave/Graphix/Model/IMetaMesh.cpp | 6 +---- Weave/Graphix/Model/Model.h | 2 ++ Weave/Scene/Scene.cpp | 43 ++++++++++++------------------- Weave/Scene/Scene.h | 9 ++++--- Weave/Scene/SceneObject.cpp | 36 ++++++++++++++++++++++++-- Weave/Scene/SceneObject.h | 2 +- 7 files changed, 61 insertions(+), 39 deletions(-) diff --git a/Weave/Graphix/GLM.h b/Weave/Graphix/GLM.h index 4c95c5e..34f0073 100644 --- a/Weave/Graphix/GLM.h +++ b/Weave/Graphix/GLM.h @@ -32,7 +32,7 @@ using glm::sign; #define M_2_PI 0.636619772367581343075535053490057448 /* 2/pi */ #define M_D_R 57.2957795131 /* 180/pi */ - +__declspec(deprecated) float VektorAbs(const vec3& vek); //#include "matM.h" diff --git a/Weave/Graphix/Model/IMetaMesh.cpp b/Weave/Graphix/Model/IMetaMesh.cpp index 9ef7481..d9a05d4 100644 --- a/Weave/Graphix/Model/IMetaMesh.cpp +++ b/Weave/Graphix/Model/IMetaMesh.cpp @@ -34,14 +34,12 @@ IMetaMesh::IMetaMesh(const string& _modelpath) IMesh* tmpIMesh = new IMesh(scene->mMeshes[0], tmpModelMat); models.push_back(tmpIMesh); tmpIMesh->getBBmm(BBmin, BBmax); - + for (uint i = 1; i < scene->mNumMeshes; i++) { - //delete tmpIMesh; aimat = &(root->mChildren[i]->mTransformation); tmpModelMat = mat4(aimat->a1, aimat->c1, -aimat->b1, aimat->d1, aimat->a2, aimat->c2, -aimat->b2, aimat->d2, aimat->a3, aimat->c3, -aimat->b3, aimat->d3, aimat->a4, aimat->c4, -aimat->b4, aimat->d4); - tmpIMesh = new IMesh(scene->mMeshes[i], tmpModelMat); models.push_back(tmpIMesh); tmpIMesh->getBBmm(tmpMin, tmpMax); @@ -53,7 +51,6 @@ IMetaMesh::IMetaMesh(const string& _modelpath) BBmax[j] = tmpMax[j]; } } - //delete tmpIMesh; updateBB(BBmin, BBmax); } else @@ -61,7 +58,6 @@ IMetaMesh::IMetaMesh(const string& _modelpath) Message::error("The file " + _modelpath + " doesn't contain any nodes."); return; } - } IMetaMesh::~IMetaMesh() diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index 9a83e93..9336b8c 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -23,7 +23,9 @@ public: /* Draws a BoundingBox around the Model */ virtual void drawBBox(Shader* shader, const mat4& modelMat) const; + /* Get min&max from BBox */ void getBBmm(vec3& min, vec3& max) const; + /* Get size&position from BBox */ void getBBsp(vec3& size, vec3& pos) const; protected: diff --git a/Weave/Scene/Scene.cpp b/Weave/Scene/Scene.cpp index 2f6f362..a710152 100644 --- a/Weave/Scene/Scene.cpp +++ b/Weave/Scene/Scene.cpp @@ -25,13 +25,11 @@ using std::set; Scene::Scene(unsigned int x, unsigned int y, unsigned int width, unsigned int height, float fovy, float zNear, float zFar, vec3 pos, SceneObject* _lookat) : -viewPort(new ViewPort(x, y, width, height, fovy, zNear, zFar)), +viewPort(x, y, width, height, fovy, zNear, zFar), lookat(_lookat), -move_delta(0), -SkyBox(new Sky()) +move_delta(0) { - } @@ -46,8 +44,10 @@ Scene::~Scene() - if (viewPort != nullptr) - delete viewPort; + //if (viewPort != nullptr) + // delete viewPort; + //if (SkyBox != nullptr) + // delete SkyBox; } @@ -68,7 +68,7 @@ void Scene::update(float deltaT) //int reset = Events::getAction2(); // XYAchse um den Player - viewPort->rotateView(0.002f*Events::getViewX(), 0.001f*Events::getViewY()); + viewPort.rotateView(0.002f*Events::getViewX(), 0.001f*Events::getViewY()); //Jump if (Events::getJump() && lookat->floorBound()) @@ -78,7 +78,7 @@ void Scene::update(float deltaT) // MOVE Player if (move_x) - lookat->move(SPEED_MOVE_NORMAL *move_x * deltaT * viewPort->rotateDirection(vec3(-1.f, 0.f, 0.f))); + lookat->move(SPEED_MOVE_NORMAL *move_x * deltaT * viewPort.rotateDirection(vec3(-1.f, 0.f, 0.f))); if (move_y){ if (move_y > 0){ @@ -86,10 +86,10 @@ void Scene::update(float deltaT) move_delta += deltaT; if (move_delta > TIME_TILL_MAX_MOVE) move_delta = TIME_TILL_MAX_MOVE; - lookat->move(SPEED_MOVE_NORMAL * (MOVE_FASTER * move_delta / TIME_TILL_MAX_MOVE + 1) *move_y * deltaT * viewPort->rotateDirection(vec3(0.f, 0.f, -1.f))); + lookat->move(SPEED_MOVE_NORMAL * (MOVE_FASTER * move_delta / TIME_TILL_MAX_MOVE + 1) *move_y * deltaT * viewPort.rotateDirection(vec3(0.f, 0.f, -1.f))); } else - lookat->move(SPEED_MOVE_NORMAL *move_y * deltaT * viewPort->rotateDirection(vec3(0.f, 0.f, -1.f))); + lookat->move(SPEED_MOVE_NORMAL *move_y * deltaT * viewPort.rotateDirection(vec3(0.f, 0.f, -1.f))); } else { @@ -99,7 +99,7 @@ void Scene::update(float deltaT) //Rotate Play in move direction if (move_x || move_y) - lookat->turnTo(viewPort->rotateDirection(vec3(0.f, 0.f, -1.f)), 3 * deltaT / TIME_TILL_DIRECTION_ROTATE); + lookat->turnTo(viewPort.rotateDirection(vec3(0.f, 0.f, -1.f)), 3 * deltaT / TIME_TILL_DIRECTION_ROTATE); //// Zoom auf Player @@ -136,30 +136,19 @@ void Scene::update(float deltaT) void Scene::draw() const { - viewPort->useViewPort(); + viewPort.useViewPort(); //Skybox - viewPort->bindView(SkyBox->getShader(), lookat->getPosition() + vec3(0.0f, 1.0f, 0.0f)); + viewPort.bindView(SkyBox.getShader(), lookat->getPosition() + vec3(0.0f, 1.0f, 0.0f)); //BBox - viewPort->bindView(Graphix::shader_BBox, lookat->getPosition() + vec3(0.0f, 1.0f, 0.0f)); + viewPort.bindView(Graphix::shader_BBox, lookat->getPosition() + vec3(0.0f, 1.0f, 0.0f)); for (auto i = ShaderSet.cbegin(); i != ShaderSet.cend(); ++i) { - viewPort->bindView(*i, lookat->getPosition() + vec3(0.0f, 1.0f, 0.0f)); - - //don't know if this is really the right place to set the lighting - //add ambient light - //GLfloat ambientcolor[] = { 0.5f, 0.5f, 0.5f, 1.0f }; - //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientcolor); - - //add positioned light - //GLfloat lightcolor0[] = { 1.5f, 1.5f, 1.5f, 1.0f }; - //GLfloat lightposition0[] = { 1.0f, 1.0f, 1.0f, 1.0f }; - //glLightfv(GL_LIGHT0, GL_DIFFUSE, lightcolor0); - //glLightfv(GL_LIGHT0, GL_POSITION, lightposition0); + viewPort.bindView(*i, lookat->getPosition() + vec3(0.0f, 1.0f, 0.0f)); } - SkyBox->draw(); + SkyBox.draw(); for (auto i = SceneObjects.cbegin(); i != SceneObjects.cend(); ++i) { diff --git a/Weave/Scene/Scene.h b/Weave/Scene/Scene.h index 41c29e9..ae2402c 100644 --- a/Weave/Scene/Scene.h +++ b/Weave/Scene/Scene.h @@ -2,11 +2,15 @@ #include "../Graphix/GLM.h" +#include "../Graphix/ViewPort.h" +#include "Sky.h" + #include #include class SceneObject; class ViewPort; +class Sky; class Shader; class Scene @@ -28,14 +32,13 @@ public: void setLookAt(SceneObject* _lookat); protected: - ViewPort* viewPort; + ViewPort viewPort; + Sky SkyBox; SceneObject* lookat; std::list SceneObjects; std::set ShaderSet; - SceneObject* SkyBox; - float move_delta; }; diff --git a/Weave/Scene/SceneObject.cpp b/Weave/Scene/SceneObject.cpp index 51f8b14..8b4c5d9 100644 --- a/Weave/Scene/SceneObject.cpp +++ b/Weave/Scene/SceneObject.cpp @@ -21,7 +21,7 @@ using std::string; #define YFALL_SPEED 9.8f SceneObject::SceneObject(Shader* _shader, const mat4& _modelMat, const vec4& _material, string _modelpath, string texturepath, unsigned int _model_index) : -model(new IMetaMesh(_modelpath)), +model(nullptr), modelMat(_modelMat), material(_material), shader(_shader), @@ -37,6 +37,9 @@ yStatic(true) //Message::info("Creating modelMat"); //Graphix::getGlError(); //modelID = _shader->getUniformLocation("modelMat"); + if (_modelpath != "") + model = new IMetaMesh(_modelpath); + if (texturepath != "") texture = new Texture(texturepath); //Message::info("Creating SkyBox Shader"); @@ -152,6 +155,35 @@ void SceneObject::collisions(SceneObject* _other) if (collision_ignore || _other->collision_ignore) return; + vec3 posA, sizeA, posB, sizeB; + + model->getBBsp(sizeA, posA); + _other->model->getBBsp(sizeB, posB); + + vec3 pDist, sDist; + vec3 overlap, inside; + int isOverlaping, isInside; + + sizeA /= 2; + sizeB /= 2; + + pDist = posB - posA; + pDist *= sign(pDist); + + sDist = sizeB - sizeA; + sDist *= sign(sDist); + + overlap =(sizeA + sizeB) - pDist; + inside = sDist - pDist; + + + /* overlapping 1 yes, 0 touch, -1 no*/ + isOverlaping = sign(glm::min(glm::min(overlap.x, overlap.y), overlap.z)); + /* inside 1 yes, 0 touch, -1 no*/ + isInside = sign(glm::min(glm::min(inside.x, inside.y), inside.z)); + + + } bool SceneObject::floorBound() const @@ -225,7 +257,7 @@ void SceneObject::setMainScene(Scene* _scene) } -Shader* SceneObject::getShader() +Shader* SceneObject::getShader() const { return shader; } diff --git a/Weave/Scene/SceneObject.h b/Weave/Scene/SceneObject.h index 5014fbe..8e221e6 100644 --- a/Weave/Scene/SceneObject.h +++ b/Weave/Scene/SceneObject.h @@ -45,7 +45,7 @@ public: void setCollision(bool); - Shader* getShader(); + Shader* getShader() const; __declspec(deprecated) void setEnemy(bool isenemy); -- 2.47.3