[DEV] end dev of raycast sample example

This commit is contained in:
Edouard DUPIN 2014-12-01 21:03:33 +01:00
parent a92e7413ed
commit f4074c15a9
5 changed files with 65 additions and 51 deletions

View File

@ -8,6 +8,7 @@
#include <etk/types.h>
#include <ege/Ray.h>
#include <ege/debug.h>
#include <ege/elements/Element.h>
#include <BulletDynamics/Dynamics/btDynamicsWorld.h>
#include <BulletCollision/CollisionDispatch/btCollisionWorld.h>
@ -46,16 +47,40 @@ std::pair<vec3,vec3> 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<vec3,vec3>(end,normal);
}
EGE_ERROR(" No Hit");
EGE_VERBOSE(" No Hit");
return std::pair<vec3,vec3>(vec3(0,0,0),vec3(0,0,0));
}
std::pair<std::shared_ptr<ege::Element>, std::pair<vec3,vec3>> 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<ege::Element*>(rayCallback.m_collisionObject->getUserPointer());
if (elem != nullptr) {
EGE_VERBOSE(" hit at point=" << end << " normal=" << normal);
return std::pair<std::shared_ptr<ege::Element>, std::pair<vec3,vec3>>(elem->shared_from_this(), std::pair<vec3,vec3>(end,normal));
}
EGE_VERBOSE(" Can not get the element pointer");
return std::pair<std::shared_ptr<ege::Element>, std::pair<vec3,vec3>>(nullptr, std::pair<vec3,vec3>(end,normal));
} else {
EGE_VERBOSE(" No Hit");
}
return std::pair<std::shared_ptr<ege::Element>, std::pair<vec3,vec3>>(nullptr, std::pair<vec3,vec3>(vec3(0,0,0),vec3(0,0,0)));
}

View File

@ -12,8 +12,10 @@
#include <etk/math/Vector3D.h>
namespace ege {
class Ray;
class Element;
};
#include <ege/physics/Engine.h>
#include <memory>
namespace ege {
class Ray {
@ -67,6 +69,7 @@ namespace ege {
void set(const vec3& _origin, const vec3& _direction);
public:
std::pair<vec3,vec3> testRay(ege::physics::Engine& _engine);
std::pair<std::shared_ptr<ege::Element>, std::pair<vec3,vec3>> testRayObject(ege::physics::Engine& _engine);
};
std::ostream& operator <<(std::ostream& _os, const ege::Ray& _obj);
};

View File

@ -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));
}

View File

@ -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<ege::camera::View>(vec3(30,30,-100), vec3(0,0,0));
@ -103,12 +101,8 @@ void appl::Windows::init() {
//std::shared_ptr<ege::ElementBase> element = std::make_shared<ege::ElementBase>(m_env);
std::shared_ptr<ege::ElementPhysic> element = std::make_shared<ege::ElementPhysic>(m_env);
// add physic interface:
/*
std::shared_ptr<ege::PhysicsBox> physic = std::make_shared<ege::PhysicsBox>();
physic->setSize(vec3(3.2,3.2,3.2));
*/
std::shared_ptr<ege::PhysicsSphere> physic = std::make_shared<ege::PhysicsSphere>();
physic->setRadius(4.2f);
myMesh->addPhysicElement(physic);
element->setMesh(myMesh);
@ -124,7 +118,7 @@ void appl::Windows::init() {
// add physic interface:
std::shared_ptr<ege::PhysicsSphere> physic = std::make_shared<ege::PhysicsSphere>();
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::shared_ptr<ege::Element>, std::pair<vec3,vec3>> 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<ewol::resource::Colored3DObject>& _obj) {
etk::Color<float> tmpColor(0.0, 1.0, 0.0, 0.3);
etk::Color<float> tmpColor2(1.0, 0.0, 0.0, 0.3);
static std::vector<vec3> vertices;
static std::vector<vec3> 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<vec3> 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<float>(0.0, 1.0, 0.0, 0.8), mat);
}
// display normal impact line
if (true) {
static std::vector<vec3> 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<float>(1.0, 0.0, 0.0, 0.8), mat);
}
}

View File

@ -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<ewol::resource::Colored3DObject>& _obj);
ege::Ray m_ray;