[DEV] add collision basic example

This commit is contained in:
Edouard DUPIN 2014-12-03 21:11:46 +01:00
parent 1b1c9ed887
commit 2b1c2619c7
11 changed files with 469 additions and 56 deletions

View File

@ -425,13 +425,15 @@ void ege::ElementPhysic::setMass(float _value) {
void ege::ElementPhysic::setLinearVelocity(const vec3& _value) { void ege::ElementPhysic::setLinearVelocity(const vec3& _value) {
if (m_body == nullptr) { if (m_body == nullptr) {
EGE_WARNING("no body");
return; return;
} }
m_body->setLinearVelocity(vec3(0,0,0)); m_body->setLinearVelocity(_value);
} }
void ege::ElementPhysic::setTorqueImpulse(const vec3& _value) { void ege::ElementPhysic::setTorqueImpulse(const vec3& _value) {
if (m_body == nullptr) { if (m_body == nullptr) {
EGE_WARNING("no body");
return; return;
} }
@ -439,6 +441,7 @@ void ege::ElementPhysic::setTorqueImpulse(const vec3& _value) {
void ege::ElementPhysic::setAngularVelocity(const vec3& _value) { void ege::ElementPhysic::setAngularVelocity(const vec3& _value) {
if (m_body == nullptr) { if (m_body == nullptr) {
EGE_WARNING("no body");
return; return;
} }
m_body->setAngularVelocity(_value); m_body->setAngularVelocity(_value);

View File

@ -0,0 +1 @@
Camera position sample example

View File

@ -0,0 +1,247 @@
/**
* @author Edouard DUPIN
*
* @copyright 2010, Edouard DUPIN, all right reserved
*
* @license APACHE-2 (see license file)
*/
#include <ewol/ewol.h>
#include <appl/debug.h>
#include <appl/Windows.h>
#include <ewol/widget/Label.h>
#include <ewol/widget/Sizer.h>
#include <ewol/object/Manager.h>
#include <ege/widget/Scene.h>
#include <ege/camera/View.h>
#include <etk/tool.h>
#include <ege/elements/ElementBase.h>
#include <ege/elements/ElementPhysic.h>
#include <ege/physicsShape/PhysicsBox.h>
#include <ege/physicsShape/PhysicsSphere.h>
#include <BulletDynamics/Dynamics/btRigidBody.h>
#include <LinearMath/btDefaultMotionState.h>
#include <BulletDynamics/Dynamics/btDynamicsWorld.h>
#include <BulletCollision/CollisionShapes/btCollisionShape.h>
#undef __class__
#define __class__ "Windows"
appl::Windows::Windows() {
addObjectType("appl::Windows");
}
static std::shared_ptr<ege::resource::Mesh> createViewBoxStar() {
std::shared_ptr<ege::resource::Mesh> out = ege::resource::Mesh::create("viewBoxStar", "DATA:texturedNoMaterial.prog");
if (out != nullptr) {
std::shared_ptr<ege::Material> material = std::make_shared<ege::Material>();
// set the element material properties :
material->setAmbientFactor(vec4(1,1,1,1));
material->setDiffuseFactor(vec4(0,0,0,1));
material->setSpecularFactor(vec4(0,0,0,1));
material->setShininess(1);
// 1024 == > 1<<9
// 2048 == > 1<<10
// 4096 == > 1<<11
int32_t size = 1<<11;
//material->setTexture0(""); //"
material->setTexture0Magic(ivec2(size,size));
out->addMaterial("basics", material);
//material->setImageSize(ivec2(size,size));
egami::Image* myImage = material->get();
if (nullptr == myImage) {
return out;
}
myImage->clear(etk::color::black);
ivec2 tmpPos;
for (int32_t iii=0; iii<6000; iii++) {
tmpPos.setValue(etk::tool::frand(0,size), etk::tool::frand(0,size)) ;
myImage->set(tmpPos, etk::color::white);
}
material->flush();
// basis on cube :
out->createViewBox("basics", 1000/* distance */);
// generate the VBO
out->generateVBO();
}
return out;
}
void appl::Windows::init() {
ewol::widget::Windows::init();
setTitle("example ege : DoubleView");
getObjectManager().periodicCall.bind(shared_from_this(), &appl::Windows::onCallbackPeriodicCheckCollision);
m_env = ege::Environement::create();
// Create basic Camera
m_camera = std::make_shared<ege::camera::View>(vec3(30,30,-100), vec3(0,0,0));
m_camera->setEye(vec3(100*std::sin(m_angleTetha),100*std::cos(m_angleTetha),40*std::cos(m_anglePsy)));
m_env->addCamera("basic", m_camera);
std::shared_ptr<ege::widget::Scene> tmpWidget = ege::widget::Scene::create(m_env);
if (tmpWidget == nullptr) {
APPL_CRITICAL("Can not allocate widget ==> display might be in error");
} else {
tmpWidget->setExpand(bvec2(true,true));
tmpWidget->setFill(bvec2(true,true));
tmpWidget->setCamera("basic");
setSubWidget(tmpWidget);
}
std::shared_ptr<ege::resource::Mesh> myMesh;
// Create an external box :
myMesh = createViewBoxStar();
if (myMesh != nullptr) {
m_env->addStaticMeshToDraw(myMesh);
}
// create basic gird:
myMesh = ege::resource::Mesh::createGrid(10, vec3(0,0,0), 5);
if (myMesh != nullptr) {
m_env->addStaticMeshToDraw(myMesh);
}
myMesh = ege::resource::Mesh::createCube(3);
if (myMesh != nullptr) {
//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));
myMesh->addPhysicElement(physic);
element->setMesh(myMesh);
element->createRigidBody(4000000);
element->setPosition(vec3(20,10,10));
element->setMass(1000);
m_env->addElement(element);
}
myMesh = ege::resource::Mesh::createCube(3);
if (myMesh != nullptr) {
//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::PhysicsSphere> physic = std::make_shared<ege::PhysicsSphere>();
physic->setRadius(4.5f);
myMesh->addPhysicElement(physic);
element->setMesh(myMesh);
element->createRigidBody(4000000);
element->setPosition(vec3(20,-10,10));
element->setMass(3000);
element->iaEnable();
m_env->addElement(element);
}
}
bool appl::Windows::onEventInput(const ewol::event::Input& _event) {
static float ploppp=1;
if (_event.getId() == 1) {
if (_event.getStatus() == ewol::key::statusDown) {
vec2 pos = relativePosition(_event.getPos());
ege::Ray ray = m_camera->getRayFromScreenPosition(pos, m_size);
std::shared_ptr<ege::resource::Mesh> myMesh;
myMesh = ege::resource::Mesh::createCube(1, "basics", etk::color::green);
if (myMesh != nullptr) {
std::shared_ptr<ege::ElementPhysic> element = std::make_shared<ege::ElementPhysic>(m_env);
std::shared_ptr<ege::PhysicsBox> physic = std::make_shared<ege::PhysicsBox>();
physic->setSize(vec3(1.01,1.01,1.01));
myMesh->addPhysicElement(physic);
element->setMesh(myMesh);
element->createRigidBody(4000000);
element->setPosition(ray.getOrigin());
element->setMass(20);
element->setLinearVelocity(ray.getDirection()*100);
m_env->addElement(element);
}
return true;
}
} else if (_event.getId() == 4) {
ploppp += 0.01f;
m_camera->setEye(vec3(100*std::sin(m_angleTetha),100*std::cos(m_angleTetha),80*std::cos(m_anglePsy))*ploppp);
} else if (_event.getId() == 5) {
ploppp -= 0.01f;
if (ploppp == 0) {
ploppp = 1.0f;
}
m_camera->setEye(vec3(100*std::sin(m_angleTetha),100*std::cos(m_angleTetha),80*std::cos(m_anglePsy))*ploppp);
} else if (_event.getId() == 3) {
if (_event.getStatus() == ewol::key::statusDown) {
m_oldScreenPos = relativePosition(_event.getPos());
return true;
} else if (_event.getStatus() == ewol::key::statusMove) {
vec2 pos = relativePosition(_event.getPos());
m_angleTetha -= (m_oldScreenPos.x()-pos.x())*0.05f;
m_anglePsy += (m_oldScreenPos.y()-pos.y())*0.01f;
m_camera->setEye(vec3(100*std::sin(m_angleTetha),100*std::cos(m_angleTetha),80*std::cos(m_anglePsy))*ploppp);
m_oldScreenPos = relativePosition(_event.getPos());
return true;
}
} else if (_event.getId() == 2) {
if (_event.getStatus() == ewol::key::statusDown) {
m_oldScreenPos = relativePosition(_event.getPos());
return true;
} else if (_event.getStatus() == ewol::key::statusMove) {
vec2 pos = relativePosition(_event.getPos())*0.2;
pos -= m_oldScreenPos*0.2;
float cameraAngle = m_camera->getTetha();
vec3 newPos = vec3(std::sin(cameraAngle)*pos.x() + std::cos(cameraAngle)*pos.y(),
std::cos(cameraAngle)*pos.x() + std::sin(cameraAngle)*pos.y(),
0);
APPL_ERROR("apply offset = " << newPos << " from pos=" << pos << " angle=" << cameraAngle);
newPos += m_camera->getTarget();
newPos.setMin(vec3(200,200,200));
newPos.setMax(vec3(-200,-200,-200));
m_camera->setTarget(newPos);
m_oldScreenPos = relativePosition(_event.getPos());
return true;
}
} else if (_event.getId() == 10) {
m_camera->setAngle(m_camera->getAngle() + 0.01f);
} else if (_event.getId() == 11) {
m_camera->setAngle(m_camera->getAngle() - 0.01f);
}
return false;
}
//! http://www.bulletphysics.org/mediawiki-1.5.8/index.php?title=Collision_Callbacks_and_Triggers
// manually detect collision : (loop to detect cillisions ...
void appl::Windows::onCallbackPeriodicCheckCollision(const ewol::event::Time& _event) {
int32_t numManifolds = m_env->getPhysicEngine().getDynamicWorld()->getDispatcher()->getNumManifolds();
if (numManifolds != 0) {
APPL_ERROR("numManifolds=" << numManifolds);
}
for (int i=0;i<numManifolds;i++) {
btPersistentManifold* contactManifold = m_env->getPhysicEngine().getDynamicWorld()->getDispatcher()->getManifoldByIndexInternal(i);
const btCollisionObject* obA = static_cast<const btCollisionObject*>(contactManifold->getBody0());
const btCollisionObject* obB = static_cast<const btCollisionObject*>(contactManifold->getBody1());
int numContacts = contactManifold->getNumContacts();
if (numContacts != 0) {
APPL_ERROR(" numContacts=" << numContacts);
}
for (int j=0;j<numContacts;j++) {
btManifoldPoint& pt = contactManifold->getContactPoint(j);
if (pt.getDistance()<0.f) {
const vec3& ptA = pt.getPositionWorldOnA();
const vec3& ptB = pt.getPositionWorldOnB();
const vec3& normalOnB = pt.m_normalWorldOnB;
APPL_ERROR(" point1=" << ptA << " point2=" << ptB << " normal=" << normalOnB);
}
}
}
}

View File

@ -0,0 +1,38 @@
/**
* @author Edouard DUPIN
*
* @copyright 2010, Edouard DUPIN, all right reserved
*
* @license APACHE-2 (see license file)
*/
#ifndef __APPL_WINDOWS_H__
#define __APPL_WINDOWS_H__
#include <ewol/widget/Windows.h>
#include <ege/Environement.h>
#include <ege/camera/View.h>
#include <ewol/resource/Colored3DObject.h>
namespace appl {
class Windows : public ewol::widget::Windows {
private:
std::shared_ptr<ege::Environement> m_env;
std::shared_ptr<ege::camera::View> m_camera;
protected:
Windows();
void init();
public:
DECLARE_FACTORY(Windows);
virtual ~Windows() { };
private:
bool onEventInput(const ewol::event::Input& _event);
float m_angleTetha;
float m_anglePsy;
vec2 m_oldScreenPos;
void onCallbackPeriodicCheckCollision(const ewol::event::Time& _event);
};
};
#endif

View File

@ -0,0 +1,15 @@
/**
* @author Edouard DUPIN
*
* @copyright 2010, Edouard DUPIN, all right reserved
*
* @license APACHE-2 (see license file)
*/
#include <appl/debug.h>
int32_t appl::getLogId() {
static int32_t g_val = etk::log::registerInstance("GP-spaceShip");
return g_val;
}

View File

@ -0,0 +1,52 @@
/**
* @author Edouard DUPIN
*
* @copyright 2010, Edouard DUPIN, all right reserved
*
* @license APACHE-2 (see license file)
*/
#ifndef __APPL_DEBUG_H__
#define __APPL_DEBUG_H__
#include <etk/log.h>
namespace appl {
int32_t getLogId();
};
// TODO : Review this problem of multiple intanciation of "std::stringbuf sb"
#define APPL_BASE(info,data) \
do { \
if (info <= etk::log::getLevel(appl::getLogId())) { \
std::stringbuf sb; \
std::ostream tmpStream(&sb); \
tmpStream << data; \
etk::log::logStream(appl::getLogId(), info, __LINE__, __class__, __func__, tmpStream); \
} \
} while(0)
#define APPL_CRITICAL(data) APPL_BASE(1, data)
#define APPL_ERROR(data) APPL_BASE(2, data)
#define APPL_WARNING(data) APPL_BASE(3, data)
#ifdef DEBUG
#define APPL_INFO(data) APPL_BASE(4, data)
#define APPL_DEBUG(data) APPL_BASE(5, data)
#define APPL_VERBOSE(data) APPL_BASE(6, data)
#define APPL_TODO(data) APPL_BASE(4, "TODO : " << data)
#else
#define APPL_INFO(data) do { } while(false)
#define APPL_DEBUG(data) do { } while(false)
#define APPL_VERBOSE(data) do { } while(false)
#define APPL_TODO(data) do { } while(false)
#endif
#define APPL_ASSERT(cond,data) \
do { \
if (!(cond)) { \
APPL_CRITICAL(data); \
assert(!#cond); \
} \
} while (0)
#endif

View File

@ -0,0 +1,57 @@
/**
* @author Edouard DUPIN
*
* @copyright 2010, Edouard DUPIN, all right reserved
*
* @license APACHE-2 (see license file)
*/
#include <etk/types.h>
#include <ewol/ewol.h>
#include <ewol/context/commandLine.h>
#include <appl/debug.h>
#include <appl/Windows.h>
#include <ewol/object/Object.h>
#include <ewol/widget/Manager.h>
#include <ewol/context/Context.h>
class MainApplication : public ewol::context::Application {
public:
bool init(ewol::Context& _context, size_t _initId) {
APPL_INFO("==> Init APPL (START) [" << ewol::getBoardType() << "] (" << ewol::getCompilationMode() << ")");
// TODO : Remove this : Move if in the windows properties
_context.setSize(vec2(800, 600));
// select internal data for font ...
_context.getFontDefault().setUseExternal(true);
_context.getFontDefault().set("FreeSerif;DejaVuSansMono", 19);
std::shared_ptr<ewol::widget::Windows> basicWindows = appl::Windows::create();
// create the specific windows
_context.setWindows(basicWindows);
APPL_INFO("==> Init APPL (END)");
return true;
}
void unInit(ewol::Context& _context) {
APPL_INFO("==> Un-Init APPL (START)");
// nothing to do ...
APPL_INFO("==> Un-Init APPL (END)");
}
};
/**
* @brief Main of the program (This can be set in every case, but it is not used in Andoid...).
* @param std IO
* @return std IO
*/
int main(int _argc, const char *_argv[]) {
// second possibility
return ewol::run(new MainApplication(), _argc, _argv);
}

View File

@ -0,0 +1,14 @@
/**
* @author Edouard DUPIN
*
* @copyright 2010, Edouard DUPIN, all right reserved
*
* @license APACHE-2 (see license file)
*/
#ifndef __APPL_MAIN_H__
#define __APPL_MAIN_H__
#endif

View File

@ -0,0 +1,40 @@
#!/usr/bin/python
import lutinModule as module
import lutinTools as tools
import lutinDebug as debug
import datetime
def get_desc():
return "ege sample : Collision"
def create(target):
# module name is 'edn' and type binary.
myModule = module.Module(__file__, 'egeCollision', 'PACKAGE')
myModule.add_src_file([
'appl/debug.cpp',
'appl/main.cpp',
'appl/Windows.cpp'
])
myModule.add_module_depend('ege')
myModule.add_path(tools.get_current_path(__file__))
myModule.copy_folder("data/*")
# set the package properties :
myModule.pkg_set("VERSION", "0.0.0")
myModule.pkg_set("VERSION_CODE", "0")
myModule.pkg_set("COMPAGNY_TYPE", "org")
myModule.pkg_set("COMPAGNY_NAME", "ege")
myModule.pkg_set("MAINTAINER", ["noOne <no.one@noreplay.com>"])
myModule.pkg_set("SECTION", ["Game"])
myModule.pkg_set("PRIORITY", "optional")
myModule.pkg_set("DESCRIPTION", "ege sample : Collision")
myModule.pkg_set("NAME", "Collision")
# add the currrent module at the
return myModule

View File

@ -71,8 +71,6 @@ void appl::Windows::init() {
ewol::widget::Windows::init(); ewol::widget::Windows::init();
setTitle("example ege : DoubleView"); setTitle("example ege : DoubleView");
//getObjectManager().periodicCall.bind(shared_from_this(), &appl::Windows::onCallbackPeriodicUpdateCamera);
m_env = ege::Environement::create(); m_env = ege::Environement::create();
// Create basic Camera // Create basic Camera
m_camera = std::make_shared<ege::camera::View>(vec3(30,30,-100), vec3(0,0,0)); m_camera = std::make_shared<ege::camera::View>(vec3(30,30,-100), vec3(0,0,0));
@ -101,7 +99,6 @@ void appl::Windows::init() {
tmpWidget->setFill(bvec2(true,true)); tmpWidget->setFill(bvec2(true,true));
tmpWidget->setCamera("basic"); tmpWidget->setCamera("basic");
tmpSizerVert->subWidgetAdd(tmpWidget); tmpSizerVert->subWidgetAdd(tmpWidget);
tmpWidget->signalDisplayDebug.bind(shared_from_this(), &appl::Windows::onCallbackDisplayDebug);
} }
std::shared_ptr<ewol::widget::Sizer> tmpSizerHori = ewol::widget::Sizer::create(ewol::widget::Sizer::modeHori); std::shared_ptr<ewol::widget::Sizer> tmpSizerHori = ewol::widget::Sizer::create(ewol::widget::Sizer::modeHori);
if (tmpSizerHori == nullptr) { if (tmpSizerHori == nullptr) {
@ -116,7 +113,6 @@ void appl::Windows::init() {
tmpWidget->setFill(bvec2(true,true)); tmpWidget->setFill(bvec2(true,true));
tmpWidget->setCamera("front"); tmpWidget->setCamera("front");
tmpSizerHori->subWidgetAdd(tmpWidget); tmpSizerHori->subWidgetAdd(tmpWidget);
tmpWidget->signalDisplayDebug.bind(shared_from_this(), &appl::Windows::onCallbackDisplayDebug);
} }
tmpWidget = ege::widget::Scene::create(m_env); tmpWidget = ege::widget::Scene::create(m_env);
if (tmpWidget == nullptr) { if (tmpWidget == nullptr) {
@ -126,7 +122,6 @@ void appl::Windows::init() {
tmpWidget->setFill(bvec2(true,true)); tmpWidget->setFill(bvec2(true,true));
tmpWidget->setCamera("top"); tmpWidget->setCamera("top");
tmpSizerHori->subWidgetAdd(tmpWidget); tmpSizerHori->subWidgetAdd(tmpWidget);
tmpWidget->signalDisplayDebug.bind(shared_from_this(), &appl::Windows::onCallbackDisplayDebug);
} }
} }
} }
@ -181,20 +176,7 @@ void appl::Windows::init() {
bool appl::Windows::onEventInput(const ewol::event::Input& _event) { bool appl::Windows::onEventInput(const ewol::event::Input& _event) {
static float ploppp=1; static float ploppp=1;
if (_event.getId() == 1) { if (_event.getId() == 4) {
vec2 pos = relativePosition(_event.getPos());
// thsi is to simplify example ...
pos *= vec2(1.0f, 2.0f);
ege::Ray ray = m_camera->getRayFromScreenPosition(pos, m_size);
m_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.01f; ploppp += 0.01f;
m_camera->setEye(vec3(100*std::sin(m_angleTetha),100*std::cos(m_angleTetha),80*std::cos(m_anglePsy))*ploppp); m_camera->setEye(vec3(100*std::sin(m_angleTetha),100*std::cos(m_angleTetha),80*std::cos(m_anglePsy))*ploppp);
} else if (_event.getId() == 5) { } else if (_event.getId() == 5) {
@ -242,36 +224,3 @@ bool appl::Windows::onEventInput(const ewol::event::Input& _event) {
return false; return false;
} }
void appl::Windows::onCallbackDisplayDebug(const std::shared_ptr<ewol::resource::Colored3DObject>& _obj) {
mat4 mat;
mat.identity();
// 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

@ -27,12 +27,9 @@ namespace appl {
virtual ~Windows() { }; virtual ~Windows() { };
private: private:
bool onEventInput(const ewol::event::Input& _event); bool onEventInput(const ewol::event::Input& _event);
void onCallbackDisplayDebug(const std::shared_ptr<ewol::resource::Colored3DObject>& _obj);
ege::Ray m_ray;
float m_angleTetha; float m_angleTetha;
float m_anglePsy; float m_anglePsy;
vec2 m_oldScreenPos; vec2 m_oldScreenPos;
std::pair<vec3,vec3> m_destination;
}; };
}; };