diff --git a/ege/Ray.cpp b/ege/Ray.cpp index 9d464d1..439a023 100644 --- a/ege/Ray.cpp +++ b/ege/Ray.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -46,16 +47,40 @@ std::pair ege::Ray::testRay(ege::physics::Engine& _engine) { vec3 stop = m_origin+m_direction*1000.0f; // Start and End are vectors btCollisionWorld::ClosestRayResultCallback rayCallback(start, stop); - EGE_ERROR("Process Raycast :"); + EGE_VERBOSE("Process Raycast :"); // Perform raycast _engine.getDynamicWorld()->rayTest(start, stop, rayCallback); if(rayCallback.hasHit()) { vec3 end = rayCallback.m_hitPointWorld; vec3 normal = rayCallback.m_hitNormalWorld; - EGE_ERROR(" hit at point=" << end << " normal=" << normal); + EGE_VERBOSE(" hit at point=" << end << " normal=" << normal); return std::pair(end,normal); } - EGE_ERROR(" No Hit"); + EGE_VERBOSE(" No Hit"); return std::pair(vec3(0,0,0),vec3(0,0,0)); } + +std::pair, std::pair> ege::Ray::testRayObject(ege::physics::Engine& _engine) { + vec3 start = m_origin; + vec3 stop = m_origin+m_direction*1000.0f; + // Start and End are vectors + btCollisionWorld::ClosestRayResultCallback rayCallback(start, stop); + EGE_VERBOSE("Process Raycast :"); + // Perform raycast + _engine.getDynamicWorld()->rayTest(start, stop, rayCallback); + if(rayCallback.hasHit()) { + vec3 end = rayCallback.m_hitPointWorld; + vec3 normal = rayCallback.m_hitNormalWorld; + ege::Element* elem = static_cast(rayCallback.m_collisionObject->getUserPointer()); + if (elem != nullptr) { + EGE_VERBOSE(" hit at point=" << end << " normal=" << normal); + return std::pair, std::pair>(elem->shared_from_this(), std::pair(end,normal)); + } + EGE_VERBOSE(" Can not get the element pointer"); + return std::pair, std::pair>(nullptr, std::pair(end,normal)); + } else { + EGE_VERBOSE(" No Hit"); + } + return std::pair, std::pair>(nullptr, std::pair(vec3(0,0,0),vec3(0,0,0))); +} diff --git a/ege/Ray.h b/ege/Ray.h index 011749d..bc34106 100644 --- a/ege/Ray.h +++ b/ege/Ray.h @@ -12,8 +12,10 @@ #include namespace ege { class Ray; + class Element; }; #include +#include namespace ege { class Ray { @@ -67,6 +69,7 @@ namespace ege { void set(const vec3& _origin, const vec3& _direction); public: std::pair testRay(ege::physics::Engine& _engine); + std::pair, std::pair> testRayObject(ege::physics::Engine& _engine); }; std::ostream& operator <<(std::ostream& _os, const ege::Ray& _obj); }; diff --git a/ege/elements/ElementPhysic.cpp b/ege/elements/ElementPhysic.cpp index faa2f84..d84b045 100644 --- a/ege/elements/ElementPhysic.cpp +++ b/ege/elements/ElementPhysic.cpp @@ -71,6 +71,7 @@ void ege::ElementPhysic::createRigidBody(float _mass) { btDefaultMotionState* motionState = new btDefaultMotionState(startTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(_mass, motionState, getShape(), localInertia); m_body = new btRigidBody(rbInfo); + m_body->setUserPointer((void*)this); //m_body->applyTorqueImpulse(btVector3(0,0,0.2)); m_body->setAngularVelocity(vec3(0,0,0)); } diff --git a/sample/RayTest/appl/Windows.cpp b/sample/RayTest/appl/Windows.cpp index af94699..c408c72 100644 --- a/sample/RayTest/appl/Windows.cpp +++ b/sample/RayTest/appl/Windows.cpp @@ -69,8 +69,6 @@ void appl::Windows::init() { ewol::widget::Windows::init(); setTitle("example ege : RayTest"); - getObjectManager().periodicCall.bind(shared_from_this(), &appl::Windows::onCallbackPeriodicUpdateCamera); - m_env = ege::Environement::create(); // Create basic Camera m_camera = std::make_shared(vec3(30,30,-100), vec3(0,0,0)); @@ -103,12 +101,8 @@ void appl::Windows::init() { //std::shared_ptr element = std::make_shared(m_env); std::shared_ptr element = std::make_shared(m_env); // add physic interface: - /* std::shared_ptr physic = std::make_shared(); physic->setSize(vec3(3.2,3.2,3.2)); - */ - std::shared_ptr physic = std::make_shared(); - physic->setRadius(4.2f); myMesh->addPhysicElement(physic); element->setMesh(myMesh); @@ -124,7 +118,7 @@ void appl::Windows::init() { // add physic interface: std::shared_ptr physic = std::make_shared(); - physic->setRadius(3.3f); + physic->setRadius(4.5f); myMesh->addPhysicElement(physic); @@ -139,25 +133,18 @@ void appl::Windows::init() { } } - -void appl::Windows::onCallbackPeriodicUpdateCamera(const ewol::event::Time& _event) { - static float offset = 0; - offset += 0.01; - static float offset2 = 0; - //offset2 += 0.003; - //m_camera->setEye(vec3(100*std::sin(offset),100*std::cos(offset),40*std::cos(offset2))); -} - - - bool appl::Windows::onEventInput(const ewol::event::Input& _event) { static float ploppp=1; if (_event.getId() == 1) { vec2 pos = relativePosition(_event.getPos()); ege::Ray ray = m_camera->getRayFromScreenPosition(pos, m_size); m_ray = ray; - APPL_INFO("pos=" << pos << " ray = " << ray); + APPL_DEBUG("pos=" << pos << " ray = " << ray); m_destination = ray.testRay(m_env->getPhysicEngine()); + std::pair, std::pair> result = ray.testRayObject(m_env->getPhysicEngine()); + if (result.first != nullptr) { + APPL_INFO("Select Object :" << result.first->getUID()); + } return true; } else if (_event.getId() == 4) { ploppp += 0.2f; @@ -185,35 +172,34 @@ bool appl::Windows::onEventInput(const ewol::event::Input& _event) { } void appl::Windows::onCallbackDisplayDebug(const std::shared_ptr& _obj) { - etk::Color tmpColor(0.0, 1.0, 0.0, 0.3); - etk::Color tmpColor2(1.0, 0.0, 0.0, 0.3); - static std::vector vertices; - static std::vector vertices2; - //vertices.push_back(m_ray.getOrigin());//+vec3(1,0,0)); - //vertices.push_back(vec3(100,0,0));//m_ray.getOrigin() + m_ray.getDirection()*1000); - //vertices.push_back(m_ray.getOrigin() + m_ray.getDirection()*1000); - #if 0 - vertices.push_back(vec3(0,0,0)); - vertices.push_back(m_ray.getDirection()*50); - #else - vertices.push_back(m_ray.getOrigin()); - vertices.push_back(m_ray.getOrigin()+m_ray.getDirection()*100); - #endif - if (vertices.size() > 50) { - vertices.erase(vertices.begin(), vertices.begin()+vertices.size()-50); - } - if (m_destination.second != vec3(0,0,0)) { - vertices2.push_back(m_destination.first); - vertices2.push_back(m_destination.first + m_destination.second*100); - m_destination.second = vec3(0,0,0); - } - if (vertices2.size() > 50) { - vertices2.erase(vertices2.begin(), vertices2.begin()+vertices2.size()-50); - } mat4 mat; mat.identity(); - mat.translate(vec3(0,0,0)); - _obj->drawLine(vertices, tmpColor, mat); - _obj->drawLine(vertices2, tmpColor2, mat); + // Display ray line + if (true) { + static std::vector vertices; + if (m_ray.getOrigin() != vec3(0,0,0)) { + vertices.push_back(m_ray.getOrigin()); + vertices.push_back(m_ray.getOrigin()+m_ray.getDirection()*50); + // prevent Ray removing with empty + m_ray.setOrigin(vec3(0,0,0)); + } + if (vertices.size() > 250) { + vertices.erase(vertices.begin(), vertices.begin()+vertices.size()-250); + } + _obj->drawLine(vertices, etk::Color(0.0, 1.0, 0.0, 0.8), mat); + } + // display normal impact line + if (true) { + static std::vector vertices; + if (m_destination.second != vec3(0,0,0)) { + vertices.push_back(m_destination.first); + vertices.push_back(m_destination.first + m_destination.second*20); + m_destination.second = vec3(0,0,0); + } + if (vertices.size() > 250) { + vertices.erase(vertices.begin(), vertices.begin()+vertices.size()-250); + } + _obj->drawLine(vertices, etk::Color(1.0, 0.0, 0.0, 0.8), mat); + } } diff --git a/sample/RayTest/appl/Windows.h b/sample/RayTest/appl/Windows.h index b4fe862..4efabde 100644 --- a/sample/RayTest/appl/Windows.h +++ b/sample/RayTest/appl/Windows.h @@ -26,7 +26,6 @@ namespace appl { DECLARE_FACTORY(Windows); virtual ~Windows() { }; private: - void onCallbackPeriodicUpdateCamera(const ewol::event::Time& _event); bool onEventInput(const ewol::event::Input& _event); void onCallbackDisplayDebug(const std::shared_ptr& _obj); ege::Ray m_ray;