diff --git a/ege/elements/ElementPhysic.cpp b/ege/elements/ElementPhysic.cpp index 3d0fc80..bf3062a 100644 --- a/ege/elements/ElementPhysic.cpp +++ b/ege/elements/ElementPhysic.cpp @@ -425,13 +425,15 @@ void ege::ElementPhysic::setMass(float _value) { void ege::ElementPhysic::setLinearVelocity(const vec3& _value) { if (m_body == nullptr) { + EGE_WARNING("no body"); return; } - m_body->setLinearVelocity(vec3(0,0,0)); + m_body->setLinearVelocity(_value); } void ege::ElementPhysic::setTorqueImpulse(const vec3& _value) { if (m_body == nullptr) { + EGE_WARNING("no body"); return; } @@ -439,6 +441,7 @@ void ege::ElementPhysic::setTorqueImpulse(const vec3& _value) { void ege::ElementPhysic::setAngularVelocity(const vec3& _value) { if (m_body == nullptr) { + EGE_WARNING("no body"); return; } m_body->setAngularVelocity(_value); diff --git a/sample/Collision/README.md b/sample/Collision/README.md index e69de29..8db39b2 100644 --- a/sample/Collision/README.md +++ b/sample/Collision/README.md @@ -0,0 +1 @@ +Camera position sample example \ No newline at end of file diff --git a/sample/Collision/appl/Windows.cpp b/sample/Collision/appl/Windows.cpp new file mode 100644 index 0000000..552a1ad --- /dev/null +++ b/sample/Collision/appl/Windows.cpp @@ -0,0 +1,247 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2010, Edouard DUPIN, all right reserved + * + * @license APACHE-2 (see license file) + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + + +#undef __class__ +#define __class__ "Windows" + +appl::Windows::Windows() { + addObjectType("appl::Windows"); +} + + +static std::shared_ptr createViewBoxStar() { + std::shared_ptr out = ege::resource::Mesh::create("viewBoxStar", "DATA:texturedNoMaterial.prog"); + if (out != nullptr) { + std::shared_ptr material = std::make_shared(); + // 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(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 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 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 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)); + 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(m_env); + std::shared_ptr element = std::make_shared(m_env); + + // add physic interface: + std::shared_ptr physic = std::make_shared(); + 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 myMesh; + myMesh = ege::resource::Mesh::createCube(1, "basics", etk::color::green); + if (myMesh != nullptr) { + std::shared_ptr element = std::make_shared(m_env); + std::shared_ptr physic = std::make_shared(); + 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;igetPhysicEngine().getDynamicWorld()->getDispatcher()->getManifoldByIndexInternal(i); + const btCollisionObject* obA = static_cast(contactManifold->getBody0()); + const btCollisionObject* obB = static_cast(contactManifold->getBody1()); + int numContacts = contactManifold->getNumContacts(); + if (numContacts != 0) { + APPL_ERROR(" numContacts=" << numContacts); + } + for (int j=0;jgetContactPoint(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); + } + } + } +} + + + diff --git a/sample/Collision/appl/Windows.h b/sample/Collision/appl/Windows.h new file mode 100644 index 0000000..0a4cc3d --- /dev/null +++ b/sample/Collision/appl/Windows.h @@ -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 +#include +#include +#include + +namespace appl { + class Windows : public ewol::widget::Windows { + private: + std::shared_ptr m_env; + std::shared_ptr 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 \ No newline at end of file diff --git a/sample/Collision/appl/debug.cpp b/sample/Collision/appl/debug.cpp new file mode 100644 index 0000000..07299b6 --- /dev/null +++ b/sample/Collision/appl/debug.cpp @@ -0,0 +1,15 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2010, Edouard DUPIN, all right reserved + * + * @license APACHE-2 (see license file) + */ + + +#include + +int32_t appl::getLogId() { + static int32_t g_val = etk::log::registerInstance("GP-spaceShip"); + return g_val; +} diff --git a/sample/Collision/appl/debug.h b/sample/Collision/appl/debug.h new file mode 100644 index 0000000..8036fb5 --- /dev/null +++ b/sample/Collision/appl/debug.h @@ -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 + +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 diff --git a/sample/Collision/appl/main.cpp b/sample/Collision/appl/main.cpp new file mode 100644 index 0000000..ba5e841 --- /dev/null +++ b/sample/Collision/appl/main.cpp @@ -0,0 +1,57 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2010, Edouard DUPIN, all right reserved + * + * @license APACHE-2 (see license file) + */ + + +#include +#include +#include + +#include +#include +#include +#include +#include + + +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 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); +} + + diff --git a/sample/Collision/appl/main.h b/sample/Collision/appl/main.h new file mode 100644 index 0000000..caa7110 --- /dev/null +++ b/sample/Collision/appl/main.h @@ -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 + diff --git a/sample/Collision/lutin_egeCollision.py b/sample/Collision/lutin_egeCollision.py new file mode 100644 index 0000000..3672e99 --- /dev/null +++ b/sample/Collision/lutin_egeCollision.py @@ -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 "]) + 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 + diff --git a/sample/DoubleView/appl/Windows.cpp b/sample/DoubleView/appl/Windows.cpp index cbc838f..c8b49e6 100644 --- a/sample/DoubleView/appl/Windows.cpp +++ b/sample/DoubleView/appl/Windows.cpp @@ -71,8 +71,6 @@ void appl::Windows::init() { ewol::widget::Windows::init(); setTitle("example ege : DoubleView"); - //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)); @@ -101,7 +99,6 @@ void appl::Windows::init() { tmpWidget->setFill(bvec2(true,true)); tmpWidget->setCamera("basic"); tmpSizerVert->subWidgetAdd(tmpWidget); - tmpWidget->signalDisplayDebug.bind(shared_from_this(), &appl::Windows::onCallbackDisplayDebug); } std::shared_ptr tmpSizerHori = ewol::widget::Sizer::create(ewol::widget::Sizer::modeHori); if (tmpSizerHori == nullptr) { @@ -116,7 +113,6 @@ void appl::Windows::init() { tmpWidget->setFill(bvec2(true,true)); tmpWidget->setCamera("front"); tmpSizerHori->subWidgetAdd(tmpWidget); - tmpWidget->signalDisplayDebug.bind(shared_from_this(), &appl::Windows::onCallbackDisplayDebug); } tmpWidget = ege::widget::Scene::create(m_env); if (tmpWidget == nullptr) { @@ -126,7 +122,6 @@ void appl::Windows::init() { tmpWidget->setFill(bvec2(true,true)); tmpWidget->setCamera("top"); 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) { static float ploppp=1; - if (_event.getId() == 1) { - 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::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) { + 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) { @@ -242,36 +224,3 @@ bool appl::Windows::onEventInput(const ewol::event::Input& _event) { return false; } - -void appl::Windows::onCallbackDisplayDebug(const std::shared_ptr& _obj) { - mat4 mat; - mat.identity(); - // 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/DoubleView/appl/Windows.h b/sample/DoubleView/appl/Windows.h index 4efabde..2e619b8 100644 --- a/sample/DoubleView/appl/Windows.h +++ b/sample/DoubleView/appl/Windows.h @@ -27,12 +27,9 @@ namespace appl { virtual ~Windows() { }; private: bool onEventInput(const ewol::event::Input& _event); - void onCallbackDisplayDebug(const std::shared_ptr& _obj); - ege::Ray m_ray; float m_angleTetha; float m_anglePsy; vec2 m_oldScreenPos; - std::pair m_destination; }; };