From dec77b5505b1c95a3244848d61b942b7ea2df208 Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Mon, 4 May 2015 20:11:18 +0200 Subject: [PATCH] fixed BBox collide Bug (Scale Position&Size) size saves now half size --- Weave/Game.cpp | 8 +++-- Weave/Graphix/GLM.cpp | 11 +++++++ Weave/Graphix/GLM.h | 2 ++ Weave/Graphix/Model/Model.cpp | 60 ++++++++++++++++++++++++++--------- Weave/Graphix/Model/Model.h | 2 ++ Weave/Scene/SceneObject.cpp | 7 +++- Weave/Scene/SceneObject.h | 2 ++ 7 files changed, 73 insertions(+), 19 deletions(-) diff --git a/Weave/Game.cpp b/Weave/Game.cpp index e2d586a..b204211 100644 --- a/Weave/Game.cpp +++ b/Weave/Game.cpp @@ -46,14 +46,16 @@ Game::Game() : playing(true) Shader* shader1 = new Shader("basicTexture_VS.hlsl", "lightingTexture_FS.hlsl"); // load LVL - 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"); + SceneObject* tmp_world = new SceneObject(shader1, scale(3.f * vec3(1.f, .32f, 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); + float test = tmp_world->getModel()->getPDistHit(vec3(3.f, 3.f, 4.f), vec3(0.f, -1.f, 0.f)); + // 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(), "")); //Player - SceneObject* tmp_playerObject = new Marvin(shader1, translate(vec3(1.f, 3.f, 1.f))*rotate((float)M_PI_4,vec3(0.f,1.f,0.f))); + SceneObject* tmp_playerObject = new Marvin(shader1, translate(vec3(1.f, 3.f, 1.f))*rotate((float)M_PI_4,vec3(0.f,1.f,0.f))*scale(vec3(.2f))); tmp_playerObject->yStatic= false; @@ -62,7 +64,7 @@ Game::Game() : playing(true) //current_world->addObject(new SceneObject(shader1, glm::mat4(1.0f), "Player.dae", "model_player.png")); //current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, .4f, 0.f))*scale(vec3(3.f)), "cow/cow.dae", "model_cow_2D.jpg")); - current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, .4f, 0.f))*scale(vec3(.3f)), vec4(3.0f, 0.5f, 0.4f, 1.5f), "duck.dae", "model_duck_2D.png")); + current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, .4f, 0.f))*scale(vec3(.32f)), vec4(3.0f, 0.5f, 0.4f, 1.5f), "duck.dae", "model_duck_2D.png")); current_world->addObject(new EventBox(translate(vec3(3.f, .4f, 0.f)))); diff --git a/Weave/Graphix/GLM.cpp b/Weave/Graphix/GLM.cpp index 59c1c85..6ca7de2 100644 --- a/Weave/Graphix/GLM.cpp +++ b/Weave/Graphix/GLM.cpp @@ -16,3 +16,14 @@ mat4 removeScale(const mat4& matrix) return matrix * scale(scalingV); } + +vec3 getScale(const mat4& matrix) +{ + mat3 scalingM = transpose((mat3)matrix) * (mat3)matrix; + + vec3 scalingV; + for (int i = 0; i < 3; ++i) + scalingV[i] = sqrt(scalingM[i][i]); + + return scalingV; +} \ No newline at end of file diff --git a/Weave/Graphix/GLM.h b/Weave/Graphix/GLM.h index 35c208a..b02fb3b 100644 --- a/Weave/Graphix/GLM.h +++ b/Weave/Graphix/GLM.h @@ -20,6 +20,7 @@ using glm::normalize; using glm::inverse; using glm::transpose; using glm::dot; +using glm::cross; using glm::angle; using glm::orientedAngle; @@ -41,4 +42,5 @@ __declspec(deprecated) float VektorAbs(const vec3& vek); mat4 removeScale(const mat4& matrix); +vec3 getScale(const mat4& matrix); diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index 0177d8e..fa5b7f1 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -22,7 +22,6 @@ indexBuffer(-1) { } - Model::~Model() { for (auto i = shader_map.begin(); i != shader_map.end(); ++i){ @@ -30,6 +29,7 @@ Model::~Model() } } + void Model::bindShader(Shader* _shader) { //Message::info("bindShader"); @@ -115,15 +115,15 @@ void Model::drawBBox(Shader* _shader, const mat4& _modelMat) const // BoundingBox->bindShader(_shader); vec3 BBsiz, BBpos; getBBsp(BBsiz, BBpos); - BoundingBox->drawModel(Graphix::shader_BBox, (Texture*)nullptr, _modelMat*translate(BBpos)*glm::scale(BBsiz)); + BoundingBox->drawModel(Graphix::shader_BBox, (Texture*)nullptr, _modelMat*translate(BBpos)*glm::scale(BBsiz*2.f)); } void Model::updateBB(const vec3& _min, const vec3& _max) { BBmin = _min; BBmax = _max; - BBsize = _max - _min; - BBposition = (_min + _max) / 2.f; + BBsize = (BBmax - BBmin) / 2.f; + BBposition = (BBmin + BBmax) / 2.f; } void Model::updateBB(const uint _numvertices, const float* _vertices) @@ -138,7 +138,7 @@ void Model::updateBB(const uint _numvertices, const float* _vertices) BBmax[i % 3] = _vertices[i]; } - BBsize = BBmax - BBmin; + BBsize = (BBmax - BBmin) / 2.f; BBposition = (BBmin + BBmax) / 2.f; } @@ -159,7 +159,7 @@ void Model::updateBB(const uint _numvertices, const float* _vertices, const mat4 } } - BBsize = BBmax - BBmin; + BBsize = (BBmax - BBmin) / 2.f; BBposition = (BBmin + BBmax) / 2.f; } @@ -243,7 +243,7 @@ void Model::useModelMat(const mat4& _model, Shader* _shader) 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; + float overlap=INFINITY, inside=INFINITY; _modelThis->getBBsp(sizeA, posA); _modelOther->getBBsp(sizeB, posB); @@ -254,12 +254,18 @@ void Model::isColliding(float& _overlap, float& _inside, const Model* _modelThis 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); + vec3 scaleA = getScale(_modelMatThis); + vec3 scaleB = getScale(_modelMatOther); + + scaleVec3(posA, scaleA); + scaleVec3(posB, scaleB); + + scaleVec3(sizeA, scaleA); + scaleVec3(sizeB, scaleB); + checkCollideByAxis(_overlap, _inside, 3, value_ptr(posAR), value_ptr(posB), value_ptr(sizeAR), value_ptr(sizeB)); if (_overlap >= 0) { @@ -284,12 +290,19 @@ void Model::checkColliding(float& _overlap, float& _inside, const Model* _model, 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); + vec3 scaleA = getScale(_modelMatThis); + vec3 scaleB = getScale(_modelMatOther); + + scaleVec3(posA, scaleA); + scaleVec3(posB, scaleB); + + scaleVec3(sizeA, scaleA); + scaleVec3(sizeB, scaleB); + + checkCollideByAxis(_overlap, _inside, 3, value_ptr(posAR), value_ptr(posB), value_ptr(sizeAR), value_ptr(sizeB)); if (_overlap >= 0) { @@ -302,8 +315,8 @@ void Model::checkColliding(float& _overlap, float& _inside, const Model* _model, void Model::checkCollideByAxis(float& _overlap, float& _inside, uint dim, const float* posA, const float* posB, const float* sizeA,const float* sizeB) { - _overlap = sizeA[0] + sizeB[0] - abs(posA[0] - posB[0]); - _inside = abs(sizeA[0] - sizeB[0]) - abs(posA[0] - posB[0]); + _overlap = INFINITY; + _inside = INFINITY; for (uint i = 0; i < dim; ++i) { _overlap = min(sizeA[i] + sizeB[i] - abs(posA[i] - posB[i]), _overlap); @@ -349,6 +362,23 @@ Model* Model::getBBoxModel() return BoundingBox; } +float Model::getPDistHit(const vec3& _P, const vec3& _direction) const +{ + vec3 n2 = cross(_direction, vec3(1.f, 0.f, 0.f)); + if (n2.length()<=0.01f) + n2 = cross(_direction, vec3(0.f, 1.f, 0.f)); + vec3 n3 = cross(_direction, n2); + + vec3 posA, sizeA; + getBBsp(sizeA, posA); + + //vec3 posAR = (vec3)(modelAR*vec4(posA, 1.f)); + //vec3 posBR = (vec3)(modelBR*vec4(posB, 1.f)); + + + return 1; +} + Model* Model::BoundingBox = nullptr; Shader* Model::BBoxShader = nullptr; bool Model::exBBox = false; \ No newline at end of file diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index 154c623..283ea1b 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -34,6 +34,8 @@ public: static Model* getBBoxModel(); + virtual float getPDistHit(const vec3& P, const vec3& direction) const; + protected: uint numvertices, numfaces; uint vertexBuffer, indexBuffer, normalBuffer, uvBuffer; diff --git a/Weave/Scene/SceneObject.cpp b/Weave/Scene/SceneObject.cpp index 769dc3f..c60ccd0 100644 --- a/Weave/Scene/SceneObject.cpp +++ b/Weave/Scene/SceneObject.cpp @@ -89,7 +89,7 @@ SceneObject::~SceneObject() } float SceneObject::getFloor() const -{ +{ return 0; } @@ -238,3 +238,8 @@ Texture* SceneObject::getTexture() const { return texture; } + +Model* SceneObject::getModel() const +{ + return model; +} \ No newline at end of file diff --git a/Weave/Scene/SceneObject.h b/Weave/Scene/SceneObject.h index 80a353a..46a8c21 100644 --- a/Weave/Scene/SceneObject.h +++ b/Weave/Scene/SceneObject.h @@ -50,6 +50,8 @@ public: Texture* getTexture() const; + Model* getModel() const; + __declspec(deprecated) void setEnemy(bool isenemy); __declspec(deprecated) -- 2.47.3