diff --git a/sources/ewol/game/BoundingAABB.cpp b/sources/ewol/game/BoundingAABB.cpp index d0a31e1b..0cdfed90 100644 --- a/sources/ewol/game/BoundingAABB.cpp +++ b/sources/ewol/game/BoundingAABB.cpp @@ -181,4 +181,120 @@ bool game::BoundingAABB::HasContact(game::BoundingAABB& obj) } +vec3 game::BoundingAABB::CalculatePetetration(game::BoundingAABB& bbb, vec3& movingVect) +{ + float tmin = 0; + // note : We considere the AAA has this is the moving element... + // B is condered has a fixed element ... + vec3 penetration(0,0,0); + // X + if (movingVect.x >= 0) { + // case 1 : + // ==> + // AAAAAAAAAAAAAA + // BBBBBBBBBBBBB + // case 2 : + // ==> + // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + // BBBBBBBBBBBBB + // case 3 : + // ==> + // AAAAAAAAAAAAA + // BBBBBBBBBBBBBBB + // case 4 : + // ==> + // AAAAAAAAAAAAA + // BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB + penetration.x = m_PointStop.x - bbb.m_PointStart.x; + } else { + // case 1 : + // <== + // AAAAAAAAAAAAAA + // BBBBBBBBBBBBB + // case 2 : + // <== + // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + // BBBBBBBBBBBBB + // case 3 : + // <== + // AAAAAAAAAAAAA + // BBBBBBBBBBBBBBB + // case 4 : + // <== + // AAAAAAAAAAAAA + // BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB + penetration.x = m_PointStart.x - bbb.m_PointStop.x; + } + // Y + if (movingVect.y >= 0) { + penetration.y = m_PointStop.y - bbb.m_PointStart.y; + } else { + penetration.y = m_PointStart.y - bbb.m_PointStop.y; + } + // Z + if (movingVect.z >= 0) { + penetration.z = m_PointStop.z - bbb.m_PointStart.z; + } else { + penetration.z = m_PointStart.z - bbb.m_PointStop.z; + } + return penetration; + + +/* + // note : We considere the AAA has this is the moving element... + // B is condered has a fixed element ... + vec3 penetration(0,0,0); + // X + if (movingVect.x >= 0) { + // case 1 : + // ==> + // AAAAAAAAAAAAAA + // BBBBBBBBBBBBB + // case 2 : + // ==> + // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + // BBBBBBBBBBBBB + // case 3 : + // ==> + // AAAAAAAAAAAAA + // BBBBBBBBBBBBBBB + // case 4 : + // ==> + // AAAAAAAAAAAAA + // BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB + penetration.x = m_PointStop.x - bbb.m_PointStart.x; + } else { + // case 1 : + // <== + // AAAAAAAAAAAAAA + // BBBBBBBBBBBBB + // case 2 : + // <== + // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + // BBBBBBBBBBBBB + // case 3 : + // <== + // AAAAAAAAAAAAA + // BBBBBBBBBBBBBBB + // case 4 : + // <== + // AAAAAAAAAAAAA + // BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB + penetration.x = m_PointStart.x - bbb.m_PointStop.x; + } + // Y + if (movingVect.y >= 0) { + penetration.y = m_PointStop.y - bbb.m_PointStart.y; + } else { + penetration.y = m_PointStart.y - bbb.m_PointStop.y; + } + // Z + if (movingVect.z >= 0) { + penetration.z = m_PointStop.z - bbb.m_PointStart.z; + } else { + penetration.z = m_PointStart.z - bbb.m_PointStop.z; + } + return penetration; +*/ +} diff --git a/sources/ewol/game/BoundingAABB.h b/sources/ewol/game/BoundingAABB.h index fe22d408..d97d4376 100644 --- a/sources/ewol/game/BoundingAABB.h +++ b/sources/ewol/game/BoundingAABB.h @@ -47,6 +47,10 @@ namespace game * @brief Detect the colision positions. */ virtual bool HasContact(game::BoundingAABB& otherbounding); + /** + * @brief Calculate the penetration size in 2 bounding. + */ + virtual vec3 CalculatePetetration(game::BoundingAABB& otherbounding, vec3& movingVect); /** * @brief Set the contact property at a specific value ... */ diff --git a/sources/ewol/game/Element.cpp b/sources/ewol/game/Element.cpp index c6029a12..61c33515 100644 --- a/sources/ewol/game/Element.cpp +++ b/sources/ewol/game/Element.cpp @@ -157,8 +157,10 @@ void game::Element::ProcessPosition(float delta) } } //EWOL_DEBUG("[" << m_uniqueId << "] New pos:" << tmpPos << "m"); + vec3 moving = tmpPos - m_position; m_position = tmpPos; UpdateMatrix(); + m_engine->ObjectMove(this, moving); } } @@ -205,4 +207,12 @@ void game::Element::Rotate(etk::Vector3D vect, float angleRad) mat4& game::Element::GetMatrix(void) { return m_matrix; -}; +} + + +void game::Element::CollisionDetected(game::Element* elementCollide, vec3& penetrate) +{ + m_position -= penetrate; + UpdateMatrix(); +} + diff --git a/sources/ewol/game/Element.h b/sources/ewol/game/Element.h index 67d1cc1d..28068830 100644 --- a/sources/ewol/game/Element.h +++ b/sources/ewol/game/Element.h @@ -144,6 +144,12 @@ namespace game * @brief Get the current static or dynamic mode */ bool GetStaticMode(void) { return m_static; }; + /** + * @brief A collision has bee detected ... + * @param[in] elementCollide the element that has been collided + * @param[in] penetrate The size of the penetration ... + */ + virtual void CollisionDetected(game::Element* elementCollide, vec3& penetrate); }; }; diff --git a/sources/ewol/game/Engine.cpp b/sources/ewol/game/Engine.cpp index 34b9a2d1..04b9a93d 100644 --- a/sources/ewol/game/Engine.cpp +++ b/sources/ewol/game/Engine.cpp @@ -92,6 +92,9 @@ void game::Engine::ProcessCollision(float deltaTime) bool game::Engine::HasCollision(game::BoundingAABB& bounding, game::Element* currentElement) { + if (NULL == currentElement) { + return false; + } // Brut force bounding detection : for (int32_t iii=0; iiiGetBounding(); + // Brut force bounding detection : + for (int32_t iii=0; iiiGetBounding())) { + vec3 penetrate = bounding.CalculatePetetration(m_elements[iii]->GetBounding(), movingVect); + currentElement->CollisionDetected(m_elements[iii], penetrate); + } + } + } +} void game::Engine::Draw(ewol::DrawProperty& displayProp) diff --git a/sources/ewol/game/Engine.h b/sources/ewol/game/Engine.h index 0ee3b5f7..287a0292 100644 --- a/sources/ewol/game/Engine.h +++ b/sources/ewol/game/Engine.h @@ -57,7 +57,18 @@ namespace game * @param[in] deltaTime delta time in µs from the previous call. */ void ProcessCollision(float deltaTime); + /** + * @brief parse all the element to know if a bounding has a contact with some other elements ... + * @param[in] bounding the reference bounding + * @param[in] currentElement The element that request the detection ==> permit to not detect itself... + */ bool HasCollision(game::BoundingAABB& bounding, game::Element* currentElement); + /** + * @brief : this object has move, we need to update some collition, and some other things ... + * @param[in] currentElement The element that request the detection ==> permit to not detect itself... + * @param[in] movingVect : the vector that has move the current element. + */ + void ObjectMove(game::Element* currentElement, vec3& movingVect); /** * @brief Display the environement. */