[DEV] add engine and separate element property

This commit is contained in:
Edouard DUPIN 2014-11-10 21:32:42 +01:00
parent ca3ead0c8c
commit bdf3a56e6b
16 changed files with 713 additions and 391 deletions

View File

@ -8,7 +8,7 @@
#include <ege/debug.h>
#include <ege/Environement.h>
#include <ege/ElementGame.h>
#include <ege/elements/Element.h>
#include <ewol/object/Manager.h>
#include <ewol/openGL/openGL.h>
@ -30,31 +30,31 @@
#define __class__ "Environement"
std::shared_ptr<ege::ElementGame> ege::Environement::getElementNearest(std::shared_ptr<ege::ElementGame> _sourceRequest, float& _distance) {
std::shared_ptr<ege::Element> ege::Environement::getElementNearest(std::shared_ptr<ege::Element> _sourceRequest, float& _distance) {
if (_sourceRequest == nullptr) {
return nullptr;
}
vec3 sourcePosition = _sourceRequest->getPosition();
std::shared_ptr<ege::ElementGame> result = nullptr;
for (size_t iii=0; iii<m_listElementGame.size() ; iii++) {
std::shared_ptr<ege::Element> result = nullptr;
for (size_t iii=0; iii<m_listElement.size() ; iii++) {
// chack nullptr pointer
if (m_listElementGame[iii] == nullptr) {
if (m_listElement[iii] == nullptr) {
continue;
}
if (m_listElementGame[iii]->getGroup() <= 0) {
if (m_listElement[iii]->getGroup() <= 0) {
continue;
}
// check if they are in the same group:
if (m_listElementGame[iii]->getGroup() == _sourceRequest->getGroup()) {
if (m_listElement[iii]->getGroup() == _sourceRequest->getGroup()) {
continue;
}
// check distance ...
vec3 destPosition = m_listElementGame[iii]->getPosition();
vec3 destPosition = m_listElement[iii]->getPosition();
float distance = btDistance(sourcePosition, destPosition);
//EGE_DEBUG("Distance : " << _distance << " >? " << distance << " id=" << iii);
if (_distance>distance) {
_distance = distance;
result = m_listElementGame[iii];
result = m_listElement[iii];
}
}
return result;
@ -68,9 +68,9 @@ void ege::Environement::getElementNearest(const vec3& _sourcePosition,
ege::Environement::ResultNearestElement result;
result.dist = 99999999999.0f;
result.element = nullptr;
for (size_t iii=0; iii<m_listElementGame.size() ; iii++) {
for (size_t iii=0; iii<m_listElement.size() ; iii++) {
// chack nullptr pointer
result.element = m_listElementGame[iii];
result.element = m_listElement[iii];
if (nullptr == result.element) {
continue;
}
@ -94,9 +94,9 @@ void ege::Environement::getElementNearestFixed(const vec3& _sourcePosition,
ege::Environement::ResultNearestElement result;
result.dist = 99999999999.0f;
result.element = nullptr;
for (size_t iii=0; iii<m_listElementGame.size() ; iii++) {
for (size_t iii=0; iii<m_listElement.size() ; iii++) {
// chack nullptr pointer
result.element = m_listElementGame[iii];
result.element = m_listElement[iii];
if (result.element == nullptr) {
continue;
}
@ -140,7 +140,7 @@ void ege::Environement::addCreator(const std::string& _type, ege::createElement_
EGE_DEBUG("Add creator: " << _type << " (done)");
}
std::shared_ptr<ege::ElementGame> ege::Environement::createElement(const std::string& _type, bool _autoAddElement, enum ege::property _property, void* _value) {
std::shared_ptr<ege::Element> ege::Environement::createElement(const std::string& _type, bool _autoAddElement, enum ege::property _property, void* _value) {
if (false == getHachTableCreating().exist(_type)) {
EGE_ERROR("Request creating of an type that is not known '" << _type << "'");
return nullptr;
@ -150,7 +150,7 @@ std::shared_ptr<ege::ElementGame> ege::Environement::createElement(const std::st
EGE_ERROR("nullptr pointer creator == > internal error... '" << _type << "'");
return nullptr;
}
std::shared_ptr<ege::ElementGame> tmpElement = creatorPointer(std::dynamic_pointer_cast<ege::Environement>(shared_from_this()));
std::shared_ptr<ege::Element> tmpElement = creatorPointer(std::dynamic_pointer_cast<ege::Environement>(shared_from_this()));
if (tmpElement == nullptr) {
EGE_ERROR("allocation error '" << _type << "'");
return nullptr;
@ -160,57 +160,57 @@ std::shared_ptr<ege::ElementGame> ege::Environement::createElement(const std::st
return nullptr;
}
if (_autoAddElement == true) {
addElementGame(tmpElement);
addElement(tmpElement);
}
return tmpElement;
}
std::shared_ptr<ege::ElementGame> ege::Environement::createElement(const std::string& _type, std::string& _description, bool _autoAddElement) {
std::shared_ptr<ege::Element> ege::Environement::createElement(const std::string& _type, std::string& _description, bool _autoAddElement) {
return createElement(_type, _autoAddElement, ege::typeString, static_cast<void*>(&_description));
}
std::shared_ptr<ege::ElementGame> ege::Environement::createElement(const std::string& _type, ejson::Value* _value, bool _autoAddElement) {
std::shared_ptr<ege::Element> ege::Environement::createElement(const std::string& _type, ejson::Value* _value, bool _autoAddElement) {
return createElement(_type, _autoAddElement, ege::typeJson, static_cast<void*>(_value));
}
std::shared_ptr<ege::ElementGame> ege::Environement::createElement(const std::string& _type, exml::Node* _node, bool _autoAddElement) {
std::shared_ptr<ege::Element> ege::Environement::createElement(const std::string& _type, exml::Node* _node, bool _autoAddElement) {
return createElement(_type, _autoAddElement, ege::typeXml, static_cast<void*>(_node));
}
void ege::Environement::addElementGame(std::shared_ptr<ege::ElementGame> _newElement) {
void ege::Environement::addElement(std::shared_ptr<ege::Element> _newElement) {
// prevent memory allocation and un allocation ...
if (_newElement == nullptr) {
return;
}
for (size_t iii=0; iii<m_listElementGame.size() ; iii++) {
if (m_listElementGame[iii] == nullptr) {
m_listElementGame[iii] = _newElement;
m_listElementGame[iii]->dynamicEnable();
for (size_t iii=0; iii<m_listElement.size() ; iii++) {
if (m_listElement[iii] == nullptr) {
m_listElement[iii] = _newElement;
m_listElement[iii]->dynamicEnable();
return;
}
}
m_listElementGame.push_back(_newElement);
m_listElement.push_back(_newElement);
_newElement->dynamicEnable();
}
void ege::Environement::rmElementGame(std::shared_ptr<ege::ElementGame> _removeElement) {
void ege::Environement::rmElement(std::shared_ptr<ege::Element> _removeElement) {
if (_removeElement == nullptr) {
return;
}
// inform the element that an element has been removed == > this permit to keep pointer on elements ...
for (size_t iii=0; iii<m_listElementGame.size() ; iii++) {
if (m_listElementGame[iii] != nullptr) {
m_listElementGame[iii]->elementIsRemoved(_removeElement);
for (size_t iii=0; iii<m_listElement.size() ; iii++) {
if (m_listElement[iii] != nullptr) {
m_listElement[iii]->elementIsRemoved(_removeElement);
}
}
// ream remove on the element :
for (size_t iii=0; iii<m_listElementGame.size() ; iii++) {
if (_removeElement == m_listElementGame[iii]) {
m_listElementGame[iii]->onDestroy();
m_listElementGame[iii]->dynamicDisable();
m_listElementGame[iii]->unInit();
m_listElementGame[iii].reset();
for (size_t iii=0; iii<m_listElement.size() ; iii++) {
if (_removeElement == m_listElement[iii]) {
m_listElement[iii]->onDestroy();
m_listElement[iii]->dynamicDisable();
m_listElement[iii]->unInit();
m_listElement[iii].reset();
}
}
}
@ -219,6 +219,7 @@ void ege::Environement::rmElementGame(std::shared_ptr<ege::ElementGame> _removeE
void ege::Environement::getOrderedElementForDisplay(std::vector<ege::Environement::ResultNearestElement>& _resultList,
const vec3& _position,
const vec3& _direction) {
// TODO : Set it back ... corrected...
// remove all unneeded elements (old display...)
_resultList.clear();
// basic element result
@ -226,27 +227,31 @@ void ege::Environement::getOrderedElementForDisplay(std::vector<ege::Environemen
result.dist = 99999999999.0f;
result.element = nullptr;
// for all element in the game we chek if it is needed to display it ...
for (size_t iii=0; iii<m_listElementGame.size() ; iii++) {
for (size_t iii=0; iii<m_listElement.size() ; iii++) {
// chack nullptr pointer
if (nullptr == m_listElementGame[iii]) {
if (nullptr == m_listElement[iii]) {
// no pointer null are set in the output list ...
continue;
}
result.element = m_listElementGame[iii];
result.element = m_listElement[iii];
// check distance ...
vec3 destPosition = result.element->getPosition();
vec3 angleView = (destPosition - _position).normalized();
float dotResult=angleView.dot(_direction);
//EGE_DEBUG("Dot position : " << destPosition << " == > dot=" << dotResult);
/*
if (dotResult <= 0.85f) {
// they are not in the camera angle view ... == > no need to process display
continue;
}
*/
result.dist = btDistance(_position, destPosition);
/*
if (result.dist>500.0f) {
// The element is realy too far ... == > no need to display
continue;
}
*/
// try to add the element at the best positions:
size_t jjj;
for (jjj=0; jjj<_resultList.size(); jjj++) {
@ -265,27 +270,26 @@ void ege::Environement::getOrderedElementForDisplay(std::vector<ege::Environemen
void ege::Environement::generateInteraction(ege::ElementInteraction& _event) {
// inform the element that an element has been removed == > this permit to keep pointer on elements ...
for (size_t iii=0; iii<m_listElementGame.size() ; iii++) {
if (nullptr == m_listElementGame[iii]) {
for (size_t iii=0; iii<m_listElement.size() ; iii++) {
if (nullptr == m_listElement[iii]) {
continue;
}
_event.applyEvent(*m_listElementGame[iii]);
_event.applyEvent(*m_listElement[iii]);
/*
vec3 destPosition = m_listElementGame[iii]->getPosition();
vec3 destPosition = m_listElement[iii]->getPosition();
float dist = btDistance(sourcePosition, destPosition);
if (dist == 0 || dist>decreasePower) {
continue;
}
float inpact = (decreasePower-dist)/decreasePower * power;
g_listElementGame[iii]->setFireOn(groupIdSource, type, -inpact, sourcePosition);
g_listElement[iii]->setFireOn(groupIdSource, type, -inpact, sourcePosition);
*/
}
}
ege::Environement::Environement() :
signalPlayTimeChange(*this, "time-change"),
m_dynamicsWorld(),
m_listElementGame(),
m_listElement(),
m_status(*this, "status", gameStart, "Satus of the activity of the Environement"),
m_ratio(*this, "ratio", 1.0f, "game speed ratio"),
m_particuleEngine(*this) {
@ -301,7 +305,7 @@ void ege::Environement::init() {
}
void ege::Environement::clear() {
m_listElementGame.clear();
m_listElement.clear();
}
@ -330,18 +334,18 @@ void ege::Environement::periodicCall(const ewol::event::Time& _event) {
}
//EGE_DEBUG("stepSimulation (start)");
///step the simulation
if (m_dynamicsWorld != nullptr) {
m_dynamicsWorld->stepSimulation(curentDelta);
if (m_physicEngine.getDynamicWorld() != nullptr) {
m_physicEngine.getDynamicWorld()->stepSimulation(curentDelta);
//optional but useful: debug drawing
m_dynamicsWorld->debugDrawWorld();
m_physicEngine.getDynamicWorld()->debugDrawWorld();
}
m_particuleEngine.update(curentDelta);
// remove all element that requested it ...
{
int32_t numberEnnemyKilled=0;
int32_t victoryPoint=0;
auto it(m_listElementGame.begin());
while (it != m_listElementGame.end()) {
auto it(m_listElement.begin());
while (it != m_listElement.end()) {
if(*it != nullptr) {
if ((*it)->needToRemove() == true) {
if ((*it)->getGroup() > 1) {
@ -349,7 +353,10 @@ void ege::Environement::periodicCall(const ewol::event::Time& _event) {
victoryPoint++;
}
EGE_DEBUG("[" << (*it)->getUID() << "] element Removing ... " << (*it)->getType());
rmElementGame((*it));
rmElement((*it));
it = m_listElement.begin();
} else {
++it;
}
}
}

View File

@ -29,6 +29,7 @@ class btDynamicsWorld;
#include <ewol/event/Time.h>
#include <ewol/parameter/Value.h>
#include <ege/resource/Mesh.h>
#include <ege/physics/Engine.h>
namespace ege {
enum property {
@ -39,9 +40,9 @@ namespace ege {
typeUser1, //!< user type 1
typeUser2 //!< User type 2
};
class ElementGame;
class Element;
class Environement;
typedef std::shared_ptr<ege::ElementGame> (*createElement_tf)(const std::shared_ptr<ege::Environement>& _env);
typedef std::shared_ptr<ege::Element> (*createElement_tf)(const std::shared_ptr<ege::Environement>& _env);
enum gameStatus {
gameStart,
@ -84,7 +85,7 @@ namespace ege {
m_positionSource(_pos)
{ };
public:
virtual void applyEvent(ege::ElementGame& _element) { };
virtual void applyEvent(ege::Element& _element) { };
};
class Environement : public ewol::Object {
@ -92,8 +93,9 @@ namespace ege {
// extern event
ewol::Signal<float> signalPlayTimeChange;
private:
std::shared_ptr<btDynamicsWorld> m_dynamicsWorld; //!< curent system world description
std::vector<std::shared_ptr<ege::ElementGame>> m_listElementGame; //!< List of all element added in the Game
//std::shared_ptr<btDynamicsWorld> m_dynamicsWorld; //!< curent system world description
ege::physics::Engine m_physicEngine; //!< EGE physic engine interface.
std::vector<std::shared_ptr<ege::Element>> m_listElement; //!< List of all element added in the Game
protected:
Environement();
void init();
@ -168,16 +170,17 @@ namespace ege {
* @return nullptr if an error occured OR the pointer on the element and it is already added on the system.
* @note Pointer is return in case of setting properties on it...
*/
std::shared_ptr<ege::ElementGame> createElement(const std::string& _type, bool _autoAddElement=true, enum ege::property _property=ege::typeNone, void* _value=nullptr);
std::shared_ptr<ege::ElementGame> createElement(const std::string& _type, std::string& _description, bool _autoAddElement=true);
std::shared_ptr<ege::ElementGame> createElement(const std::string& _type, ejson::Value* _value, bool _autoAddElement=true);
std::shared_ptr<ege::ElementGame> createElement(const std::string& _type, exml::Node* _node, bool _autoAddElement=true);
std::shared_ptr<ege::Element> createElement(const std::string& _type, bool _autoAddElement=true, enum ege::property _property=ege::typeNone, void* _value=nullptr);
std::shared_ptr<ege::Element> createElement(const std::string& _type, std::string& _description, bool _autoAddElement=true);
std::shared_ptr<ege::Element> createElement(const std::string& _type, ejson::Value* _value, bool _autoAddElement=true);
std::shared_ptr<ege::Element> createElement(const std::string& _type, exml::Node* _node, bool _autoAddElement=true);
public:
class ResultNearestElement {
public:
std::shared_ptr<ege::ElementGame> element;
std::shared_ptr<ege::Element> element;
float dist;
};
#if 0
/**
* @brief set the curent world
* @param[in] _newWorld Pointer on the current world
@ -192,12 +195,16 @@ namespace ege {
std::shared_ptr<btDynamicsWorld> getDynamicWorld() {
return m_dynamicsWorld;
};
#endif
ege::physics::Engine& getPhysicEngine() {
return m_physicEngine;
}
/**
* @breif get a reference on the curent list of element games
* @return all element list
*/
std::vector<std::shared_ptr<ege::ElementGame>>& getElementGame() {
return m_listElementGame;
std::vector<std::shared_ptr<ege::Element>>& getElement() {
return m_listElement;
};
/**
* @brief get the nearest Element
@ -205,7 +212,7 @@ namespace ege {
* @param[in] _distance Maximum distance search == > return the element distance
* @return Pointer on the neares element OR nullptr
*/
std::shared_ptr<ege::ElementGame> getElementNearest(std::shared_ptr<ege::ElementGame> _sourceRequest, float& _distance);
std::shared_ptr<ege::Element> getElementNearest(std::shared_ptr<ege::Element> _sourceRequest, float& _distance);
void getElementNearest(const vec3& _sourcePosition,
float _distanceMax,
@ -217,12 +224,12 @@ namespace ege {
* @brief add an element on the list availlable.
* @param[in] _newElement Element to add.
*/
void addElementGame(std::shared_ptr<ege::ElementGame> _newElement);
void addElement(std::shared_ptr<ege::Element> _newElement);
/**
* @brief remove an element on the list availlable.
* @param[in] _removeElement Element to remove.
*/
void rmElementGame(std::shared_ptr<ege::ElementGame> _removeElement);
void rmElement(std::shared_ptr<ege::Element> _removeElement);
/**
* @brief get the element order from the nearest to the farest, and remove all element that are not in the camera angle and axes.
* @param[in,out] _resultList List of the element ordered.

View File

@ -32,7 +32,7 @@ void ege::camera::View::update() {
m_matrix.translate(vec3(0,0,-distance));
m_matrix.rotate(vec3(1,0,0), -M_PI*0.5f + psy);
m_matrix.rotate(vec3(0,0,1), tetha);
m_matrix.translate(m_target);
m_matrix.translate(-m_target);
EGE_DEBUG("Camera properties : distance=" << distance );
EGE_DEBUG(" psy=" << psy);

209
ege/elements/Element.cpp Normal file
View File

@ -0,0 +1,209 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license BSD v3 (see license file)
*/
#include <etk/types.h>
#include <ege/debug.h>
#include <ege/elements/Element.h>
#include <ege/Environement.h>
#include <BulletDynamics/Dynamics/btRigidBody.h>
#include <LinearMath/btDefaultMotionState.h>
#include <BulletDynamics/Dynamics/btDynamicsWorld.h>
#include <BulletCollision/CollisionShapes/btCollisionShape.h>
#include <LinearMath/btIDebugDraw.h>
#include <btBulletCollisionCommon.h>
#include <BulletCollision/CollisionShapes/btConvexPolyhedron.h>
#include <BulletCollision/CollisionShapes/btShapeHull.h>
#include <LinearMath/btTransformUtil.h>
#include <LinearMath/btIDebugDraw.h>
#include <btBulletDynamicsCommon.h>
#include <BulletCollision/CollisionDispatch/btCollisionObject.h>
#include <ege/CollisionShapeCreator.h>
#undef __class__
#define __class__ "Element"
const std::string& ege::Element::getType() const {
static const std::string nameType("----");
return nameType;
}
ege::Element::Element(const std::shared_ptr<ege::Environement>& _env) :
m_env(_env),
m_uID(0),
m_mesh(),
m_life(100),
m_lifeMax(100),
m_group(0),
m_fixe(true),
m_radius(0) {
static uint32_t unique=0;
m_uID = unique;
EGE_DEBUG("Create element : uId=" << m_uID);
m_debugText.setFontSize(12);
unique++;
}
ege::Element::~Element() {
EGE_DEBUG("Destroy element : uId=" << m_uID);
}
bool ege::Element::loadMesh(const std::string& _meshFileName) {
std::shared_ptr<ege::resource::Mesh> tmpMesh = ege::resource::Mesh::create(_meshFileName);
if(nullptr == tmpMesh) {
EGE_ERROR("can not load the resources : " << _meshFileName);
return false;
}
return setMesh(tmpMesh);
}
bool ege::Element::setMesh(const std::shared_ptr<ege::resource::Mesh>& _mesh) {
if (nullptr!=m_mesh) {
m_mesh.reset();
}
m_mesh = _mesh;
// auto load the shape :
if (m_mesh == nullptr) {
return true;
}
return true;
}
float ege::Element::getLifeRatio() {
if (0 >= m_life) {
return 0;
}
return m_life/m_lifeMax;
}
void ege::Element::setFireOn(int32_t _groupIdSource, int32_t _type, float _power, const vec3& _center) {
float previousLife = m_life;
m_life += _power;
m_life = std::avg(0.0f, m_life, m_lifeMax);
if (m_life <= 0) {
EGE_DEBUG("[" << getUID() << "] element is killed ..." << getType());
}
if (m_life!=previousLife) {
onLifeChange();
}
}
const vec3& ege::Element::getPosition() {
// this is to prevent error like segmentation fault ...
static vec3 emptyPosition(-1000000,-1000000,-1000000);
return emptyPosition;
};
static void drawSphere(const std::shared_ptr<ewol::resource::Colored3DObject>& _draw,
btScalar _radius,
int _lats,
int _longs,
mat4& _transformationMatrix,
etk::Color<float>& _tmpColor) {
int i, j;
std::vector<vec3> EwolVertices;
for(i = 0; i <= _lats; i++) {
btScalar lat0 = SIMD_PI * (-btScalar(0.5) + (btScalar) (i - 1) / _lats);
btScalar z0 = _radius*sin(lat0);
btScalar zr0 = _radius*cos(lat0);
btScalar lat1 = SIMD_PI * (-btScalar(0.5) + (btScalar) i / _lats);
btScalar z1 = _radius*sin(lat1);
btScalar zr1 = _radius*cos(lat1);
//glBegin(GL_QUAD_STRIP);
for(j = 0; j < _longs; j++) {
btScalar lng = 2 * SIMD_PI * (btScalar) (j - 1) / _longs;
btScalar x = cos(lng);
btScalar y = sin(lng);
vec3 v1 = vec3(x * zr1, y * zr1, z1);
vec3 v4 = vec3(x * zr0, y * zr0, z0);
lng = 2 * SIMD_PI * (btScalar) (j) / _longs;
x = cos(lng);
y = sin(lng);
vec3 v2 = vec3(x * zr1, y * zr1, z1);
vec3 v3 = vec3(x * zr0, y * zr0, z0);
EwolVertices.push_back(v1);
EwolVertices.push_back(v2);
EwolVertices.push_back(v3);
EwolVertices.push_back(v1);
EwolVertices.push_back(v3);
EwolVertices.push_back(v4);
}
}
_draw->draw(EwolVertices, _tmpColor, _transformationMatrix);
}
const float lifeBorder = 0.1f;
const float lifeHeight = 0.3f;
const float lifeWidth = 2.0f;
const float lifeYPos = 1.7f;
void ege::Element::drawLife(const std::shared_ptr<ewol::resource::Colored3DObject>& _draw, const ege::Camera& _camera) {
if (nullptr == _draw) {
return;
}
float ratio = getLifeRatio();
if (ratio == 1.0f) {
return;
}
#if 0
mat4 transformationMatrix = etk::matTranslate(getPosition())
* etk::matRotate(vec3(0,0,1),_camera.getAngleZ())
* etk::matRotate(vec3(1,0,0),(M_PI/2.0f-_camera.getAngleTeta()));
std::vector<vec3> localVertices;
localVertices.push_back(vec3(-lifeWidth/2.0-lifeBorder,lifeYPos -lifeBorder,0));
localVertices.push_back(vec3(-lifeWidth/2.0-lifeBorder,lifeYPos+lifeHeight+lifeBorder,0));
localVertices.push_back(vec3( lifeWidth/2.0+lifeBorder,lifeYPos+lifeHeight+lifeBorder,0));
localVertices.push_back(vec3(-lifeWidth/2.0-lifeBorder,lifeYPos -lifeBorder,0));
localVertices.push_back(vec3( lifeWidth/2.0+lifeBorder,lifeYPos+lifeHeight+lifeBorder,0));
localVertices.push_back(vec3( lifeWidth/2.0+lifeBorder,lifeYPos -lifeBorder,0));
etk::Color<float> myColor(0x0000FF99);
_draw->draw(localVertices, myColor, transformationMatrix, false, false);
localVertices.clear();
/** Bounding box == > model shape **/
localVertices.push_back(vec3(-lifeWidth/2.0 ,lifeYPos,0));
localVertices.push_back(vec3(-lifeWidth/2.0 ,lifeYPos + lifeHeight,0));
localVertices.push_back(vec3(-lifeWidth/2.0+lifeWidth*ratio,lifeYPos + lifeHeight,0));
localVertices.push_back(vec3(-lifeWidth/2.0 ,lifeYPos,0));
localVertices.push_back(vec3(-lifeWidth/2.0+lifeWidth*ratio,lifeYPos + lifeHeight,0));
localVertices.push_back(vec3(-lifeWidth/2.0+lifeWidth*ratio,lifeYPos,0));
myColor =0x00FF00FF;
if (ratio < 0.2f) {
myColor = 0xFF0000FF;
} else if (ratio < 0.4f) {
myColor = 0xDA7B00FF;
}
_draw->draw(localVertices, myColor, transformationMatrix, false, false);
#endif
}
void ege::Element::drawDebug(const std::shared_ptr<ewol::resource::Colored3DObject>& _draw, const ege::Camera& _camera) {
m_debugText.clear();
m_debugText.setColor(etk::Color<>(0x00, 0xFF, 0x00, 0xFF));
m_debugText.setPos(vec3(-20,32,0));
m_debugText.print(getType());
m_debugText.setPos(vec3(-20,20,0));
m_debugText.print("life=("+etk::to_string(getLifeRatio()));
//m_debugText.print(std::string("Axe=(")+std::string(m_tmpAxe.x())+std::string(",")+etk::UString(m_tmpAxe.y())+etk::UString(",")+etk::UString(m_tmpAxe.z())+etk::UString(")"));
/*
m_debugText.draw( etk::matTranslate(getPosition())
* etk::matRotate(vec3(0,0,1),_camera.getAngleZ())
* etk::matRotate(vec3(1,0,0),(M_PI/2.0f-_camera.getAngleTeta()))
* etk::matScale(vec3(0.05,0.05,0.05)));
*/
}

View File

@ -6,8 +6,8 @@
* @license BSD v3 (see license file)
*/
#ifndef __EGE_ELEMENT_GAME_H__
#define __EGE_ELEMENT_GAME_H__
#ifndef __EGE_ELEMENT_H__
#define __EGE_ELEMENT_H__
#include <etk/types.h>
#include <etk/math/Vector3D.h>
@ -29,24 +29,20 @@
#define ELEMENT_SCALE (1.0f/8.0f)
namespace ege {
class ElementGame {
private:
static void FunctionFreeShape(void* _pointer);
class Element {
protected:
std::shared_ptr<ege::Environement> m_env;
protected:
btRigidBody* m_body; //!< all the element have a body == > otherwise it will be not manage with this system...
public:
/**
* @brief Constructor (when constructer is called just add element that did not change.
* The objest will be stored in a pool of element and keep a second time if needed == > redure memory allocation,
* when needed, the system will call the init and un-init function...
*/
ElementGame(const std::shared_ptr<ege::Environement>& _env);
Element(const std::shared_ptr<ege::Environement>& _env);
/**
* @brief Destructor
*/
virtual ~ElementGame();
virtual ~Element();
/**
* @brief get the element Type description string.
* @return A reference on the descriptive string.
@ -76,7 +72,6 @@ namespace ege {
};
protected:
std::shared_ptr<ege::resource::Mesh> m_mesh; //!< Mesh of the Element (can be nullptr)
btCollisionShape* m_shape; //!< shape of the element (set a copy here to have the debug display of it)
public:
/**
* @brief Select a mesh with a specific name.
@ -84,21 +79,14 @@ namespace ege {
* @note Automaticly load the shape if it is specify in the mesh file
* @return true if no error occured
*/
bool loadMesh(const std::string& _meshFileName);
virtual bool loadMesh(const std::string& _meshFileName);
/**
* @brief set the the Mesh properties.
* @param[in] _mesh The mesh pointer. (nullptr to force the mesh remove ...)
* @note : this remove the shape and the mesh properties.
* @return true if no error occured
*/
bool setMesh(const std::shared_ptr<ege::resource::Mesh>& _mesh);
/**
* @brief set the shape properties.
* @param[in] _shape The shape pointer.
* @note : this remove the shape properties.
* @return true if no error occured
*/
bool setShape(btCollisionShape* _shape);
virtual bool setMesh(const std::shared_ptr<ege::resource::Mesh>& _mesh);
/**
* @brief get a pointer on the Mesh file.
* @return the mesh pointer.
@ -106,18 +94,6 @@ namespace ege {
inline const std::shared_ptr<ege::resource::Mesh>& getMesh() {
return m_mesh;
};
/**
* @brief get a pointer on the bullet collision shape.
* @return the collision pointer.
*/
inline btCollisionShape* getShape() {
return m_shape;
};
private:
/**
* @brief remove the curent selected shape.
*/
void removeShape();
protected:
float m_life; //!< Current life of the object
float m_lifeMax; //!< Maximum possible life of the element
@ -150,7 +126,7 @@ namespace ege {
*/
virtual void setFireOn(int32_t _groupIdSource, int32_t _type, float _power, const vec3& _center=vec3(0,0,0));
/**
* @brief Call chan the element life change
* @brief Call when the element life change.
*/
virtual void onLifeChange() { };
protected:
@ -170,7 +146,7 @@ namespace ege {
inline void setGroup(int32_t _newGroup) {
m_group=_newGroup;
};
public:
/**
* @brief Can be call tu opdate the list of the element displayed on the scren (example : no display of the hiden triangle)
* @param[in] the camera properties
@ -181,11 +157,12 @@ namespace ege {
* @brief draw the curent element (can have multiple display)
* @param[in] pass Id of the current pass : [0..?]
*/
virtual void draw(int32_t _pass=0);
virtual void draw(int32_t _pass=0) = 0;
/**
* @brief draw the current life of the element
*/
// TODO : Remove this ...
virtual void drawLife(const std::shared_ptr<ewol::resource::Colored3DObject>& _draw, const ege::Camera& _camera);
protected:
@ -205,36 +182,21 @@ namespace ege {
virtual vec3 getPositionTheoric() {
return getPosition();
};
/**
* @brief set the current Theoric position of the element
* @param[in] set the 3D position.
*/
virtual void setPositionTheoric(const vec3& _pos) { };
/**
* @brief get the current position of the element
* @return the 3D position.
*/
const vec3& getPosition();
virtual const vec3& getPosition();
/**
* @brief set the current position of the element
* @param[in] _pos set the 3D position.
*/
void setPosition(const vec3& _pos);
/**
* @brief get the current speed of the element
* @return the 3D speed.
*/
const vec3& getSpeed();
/**
* @brief get the current mass of the element
* @return the mass in kG.
*/
const float getInvMass();
virtual void setPosition(const vec3& _pos) {};
/**
* @brief Event arrive when an element has been remove from the system == > this permit to keep pointer of ennemy, and not search them every cycle ...
* @param[in] _removedElement Pointer on the element removed.
*/
virtual void elementIsRemoved(std::shared_ptr<ege::ElementGame> _removedElement) { };
virtual void elementIsRemoved(std::shared_ptr<ege::Element> _removedElement) { };
protected:
bool m_fixe; //!< is a fixed element == > used for placement of every elements
public:
@ -252,66 +214,21 @@ namespace ege {
* @brief get the current space needed by the element in the workspace
* @return The dimention needed.
*/
inline float getRadius()
{
inline float getRadius() {
return m_radius;
};
protected:
bool m_elementInPhysicsSystem;
public:
/**
* @brief, call when the element is removed (call only one time)
*/
virtual void onDestroy() {};
/**
* @brief set the elment in the physique engine
*/
void dynamicEnable();
virtual void dynamicEnable() {};
/**
* @brief remove this element from the physique engine
*/
void dynamicDisable();
private:
class localIA : public btActionInterface {
private:
ege::ElementGame& m_element;
public:
/**
* @brief Constructor
*/
localIA(ElementGame& _element) :
m_element(_element) {
};
/**
* @brief Destructor
*/
virtual ~localIA() {
};
public: // herited function
void debugDraw(btIDebugDraw* _debugDrawer) {
};
void updateAction(btCollisionWorld* _collisionWorld, btScalar _step) {
m_element.iaAction(_step);
};
};
localIA* m_IA;
public:
/**
* @brief enable periodic call Of this object for processing Artificial Intelligence
*/
void iaEnable();
/**
* @brief disable periodic call Of this object for processing Artificial Intelligence
*/
void iaDisable();
/**
* @brief periodic call for intelligence artificial.
* @param[in] step : step of time in s
*/
virtual void iaAction(float _step) { };
/**
* @brief, call when the element is removed (call only one time
*/
virtual void onDestroy() {};
virtual void dynamicDisable() {};
};
};

View File

@ -0,0 +1,44 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license BSD v3 (see license file)
*/
#include <ege/elements/ElementBase.h>
#include <ege/debug.h>
ege::ElementBase::ElementBase(const std::shared_ptr<ege::Environement>& _env) :
ege::Element(_env),
m_position(0,0,0) {
}
ege::ElementBase::~ElementBase() {
EGE_WARNING("Remove ... ");
}
const std::string& ege::ElementBase::getType() const {
return ege::Element::getType();
}
void ege::ElementBase::draw(int32_t _pass) {
mat4 transformationMatrix(0);
transformationMatrix.identity();
transformationMatrix.translate(m_position);
//transformationMatrix.transpose();
m_mesh->draw(transformationMatrix);
EGE_VERBOSE("draw ... " << transformationMatrix);
}
const vec3& ege::ElementBase::getPosition() {
return m_position;
}
void ege::ElementBase::setPosition(const vec3& _pos) {
m_position = _pos;
}

View File

@ -0,0 +1,44 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license BSD v3 (see license file)
*/
#ifndef __EGE_ELEMENT_BASE_H__
#define __EGE_ELEMENT_BASE_H__
#include <ege/elements/Element.h>
namespace ege {
class ElementBase : public ege::Element {
public:
/**
* @brief Constructor (when constructer is called just add element that did not change.
* The objest will be stored in a pool of element and keep a second time if needed == > redure memory allocation,
* when needed, the system will call the init and un-init function...
*/
ElementBase(const std::shared_ptr<ege::Environement>& _env);
/**
* @brief Destructor
*/
virtual ~ElementBase();
/**
* @brief get the element Type description string.
* @return A reference on the descriptive string.
*/
virtual const std::string& getType() const;
virtual void draw(int32_t _pass=0);
private:
vec3 m_position;
public:
virtual const vec3& getPosition();
virtual void setPosition(const vec3& _pos);
};
};
#endif

View File

@ -8,7 +8,7 @@
#include <etk/types.h>
#include <ege/debug.h>
#include <ege/ElementGame.h>
#include <ege/elements/ElementPhysic.h>
#include <ege/Environement.h>
#include <BulletDynamics/Dynamics/btRigidBody.h>
#include <LinearMath/btDefaultMotionState.h>
@ -26,35 +26,24 @@
#include <ege/CollisionShapeCreator.h>
#undef __class__
#define __class__ "ElementGame"
#define __class__ "ElementPhysic"
const std::string& ege::ElementGame::getType() const {
const std::string& ege::ElementPhysic::getType() const {
static const std::string nameType("----");
return nameType;
}
ege::ElementGame::ElementGame(const std::shared_ptr<ege::Environement>& _env) :
m_env(_env),
ege::ElementPhysic::ElementPhysic(const std::shared_ptr<ege::Environement>& _env) :
ege::Element(_env),
m_body(nullptr),
m_uID(0),
m_mesh(),
m_shape(nullptr),
m_life(100),
m_lifeMax(100),
m_group(0),
m_fixe(true),
m_radius(0),
m_elementInPhysicsSystem(false),
m_IA(nullptr) {
static uint32_t unique=0;
m_uID = unique;
EGE_DEBUG("Create element : uId=" << m_uID);
m_debugText.setFontSize(12);
unique++;
}
ege::ElementGame::~ElementGame() {
ege::ElementPhysic::~ElementPhysic() {
// in every case remove IA
iaDisable();
// same ...
@ -62,10 +51,35 @@ ege::ElementGame::~ElementGame() {
removeShape();
delete m_body;
m_body = nullptr;
EGE_DEBUG("Destroy element : uId=" << m_uID);
}
void ege::ElementGame::removeShape() {
bool ege::ElementPhysic::setMesh(const std::shared_ptr<ege::resource::Mesh>& _mesh) {
if (nullptr!=m_mesh) {
removeShape();
}
ege::Element::setMesh(_mesh);
// auto load the shape :
if (m_mesh == nullptr) {
return true;
}
if (m_mesh->getShape() != nullptr) {
m_shape = static_cast<btCollisionShape*>(m_mesh->getShape());
return true;
}
m_mesh->setShape(ege::collision::createShape(m_mesh));
m_mesh->setFreeShapeFunction(&FunctionFreeShape);
m_shape = static_cast<btCollisionShape*>(m_mesh->getShape());
return true;
}
bool ege::ElementPhysic::setShape(btCollisionShape* _shape) {
removeShape();
m_shape = _shape;
return true;
}
void ege::ElementPhysic::removeShape() {
// no shape
if (nullptr == m_shape) {
return;
@ -85,68 +99,14 @@ void ege::ElementGame::removeShape() {
// otherwise : the shape is auto remove by the resources when no more needed ...
}
void ege::ElementGame::FunctionFreeShape(void* _pointer) {
void ege::ElementPhysic::FunctionFreeShape(void* _pointer) {
if (nullptr == _pointer) {
return;
}
delete(static_cast<btCollisionShape*>(_pointer));
}
bool ege::ElementGame::loadMesh(const std::string& _meshFileName) {
std::shared_ptr<ege::resource::Mesh> tmpMesh = ege::resource::Mesh::create(_meshFileName);
if(nullptr == tmpMesh) {
EGE_ERROR("can not load the resources : " << _meshFileName);
return false;
}
return setMesh(tmpMesh);
}
bool ege::ElementGame::setMesh(const std::shared_ptr<ege::resource::Mesh>& _mesh) {
if (nullptr!=m_mesh) {
removeShape();
m_mesh.reset();
}
m_mesh = _mesh;
// auto load the shape :
if (m_mesh == nullptr) {
return true;
}
if (m_mesh->getShape() != nullptr) {
m_shape = static_cast<btCollisionShape*>(m_mesh->getShape());
return true;
}
m_mesh->setShape(ege::collision::createShape(m_mesh));
m_mesh->setFreeShapeFunction(&FunctionFreeShape);
m_shape = static_cast<btCollisionShape*>(m_mesh->getShape());
return true;
}
bool ege::ElementGame::setShape(btCollisionShape* _shape) {
removeShape();
m_shape = _shape;
return true;
}
float ege::ElementGame::getLifeRatio() {
if (0 >= m_life) {
return 0;
}
return m_life/m_lifeMax;
}
void ege::ElementGame::setFireOn(int32_t _groupIdSource, int32_t _type, float _power, const vec3& _center) {
float previousLife = m_life;
m_life += _power;
m_life = std::avg(0.0f, m_life, m_lifeMax);
if (m_life <= 0) {
EGE_DEBUG("[" << getUID() << "] element is killed ..." << getType());
}
if (m_life!=previousLife) {
onLifeChange();
}
}
void ege::ElementGame::setPosition(const vec3& _pos) {
void ege::ElementPhysic::setPosition(const vec3& _pos) {
if (nullptr!=m_body) {
btTransform transformation = m_body->getCenterOfMassTransform();
transformation.setOrigin(_pos);
@ -154,17 +114,14 @@ void ege::ElementGame::setPosition(const vec3& _pos) {
}
}
const vec3& ege::ElementGame::getPosition() {
// this is to prevent error like segmentation fault ...
static vec3 emptyPosition(-1000000,-1000000,-1000000);
const vec3& ege::ElementPhysic::getPosition() {
if (nullptr!=m_body) {
return m_body->getCenterOfMassPosition();
}
return emptyPosition;
return ege::Element::getPosition();
};
const vec3& ege::ElementGame::getSpeed() {
// this is to prevent error like segmentation fault ...
const vec3& ege::ElementPhysic::getSpeed() {
static vec3 emptySpeed(0,0,0);
if (nullptr!=m_body) {
return m_body->getLinearVelocity();
@ -172,100 +129,13 @@ const vec3& ege::ElementGame::getSpeed() {
return emptySpeed;
};
const float ege::ElementGame::getInvMass() {
const float ege::ElementPhysic::getInvMass() {
if (nullptr!=m_body) {
return m_body->getInvMass();
}
return 0.0000000001f;
};
static void drawSphere(const std::shared_ptr<ewol::resource::Colored3DObject>& _draw,
btScalar _radius,
int _lats,
int _longs,
mat4& _transformationMatrix,
etk::Color<float>& _tmpColor) {
int i, j;
std::vector<vec3> EwolVertices;
for(i = 0; i <= _lats; i++) {
btScalar lat0 = SIMD_PI * (-btScalar(0.5) + (btScalar) (i - 1) / _lats);
btScalar z0 = _radius*sin(lat0);
btScalar zr0 = _radius*cos(lat0);
btScalar lat1 = SIMD_PI * (-btScalar(0.5) + (btScalar) i / _lats);
btScalar z1 = _radius*sin(lat1);
btScalar zr1 = _radius*cos(lat1);
//glBegin(GL_QUAD_STRIP);
for(j = 0; j < _longs; j++) {
btScalar lng = 2 * SIMD_PI * (btScalar) (j - 1) / _longs;
btScalar x = cos(lng);
btScalar y = sin(lng);
vec3 v1 = vec3(x * zr1, y * zr1, z1);
vec3 v4 = vec3(x * zr0, y * zr0, z0);
lng = 2 * SIMD_PI * (btScalar) (j) / _longs;
x = cos(lng);
y = sin(lng);
vec3 v2 = vec3(x * zr1, y * zr1, z1);
vec3 v3 = vec3(x * zr0, y * zr0, z0);
EwolVertices.push_back(v1);
EwolVertices.push_back(v2);
EwolVertices.push_back(v3);
EwolVertices.push_back(v1);
EwolVertices.push_back(v3);
EwolVertices.push_back(v4);
}
}
_draw->draw(EwolVertices, _tmpColor, _transformationMatrix);
}
const float lifeBorder = 0.1f;
const float lifeHeight = 0.3f;
const float lifeWidth = 2.0f;
const float lifeYPos = 1.7f;
void ege::ElementGame::drawLife(const std::shared_ptr<ewol::resource::Colored3DObject>& _draw, const ege::Camera& _camera) {
if (nullptr == _draw) {
return;
}
float ratio = getLifeRatio();
if (ratio == 1.0f) {
return;
}
#if 0
mat4 transformationMatrix = etk::matTranslate(getPosition())
* etk::matRotate(vec3(0,0,1),_camera.getAngleZ())
* etk::matRotate(vec3(1,0,0),(M_PI/2.0f-_camera.getAngleTeta()));
std::vector<vec3> localVertices;
localVertices.push_back(vec3(-lifeWidth/2.0-lifeBorder,lifeYPos -lifeBorder,0));
localVertices.push_back(vec3(-lifeWidth/2.0-lifeBorder,lifeYPos+lifeHeight+lifeBorder,0));
localVertices.push_back(vec3( lifeWidth/2.0+lifeBorder,lifeYPos+lifeHeight+lifeBorder,0));
localVertices.push_back(vec3(-lifeWidth/2.0-lifeBorder,lifeYPos -lifeBorder,0));
localVertices.push_back(vec3( lifeWidth/2.0+lifeBorder,lifeYPos+lifeHeight+lifeBorder,0));
localVertices.push_back(vec3( lifeWidth/2.0+lifeBorder,lifeYPos -lifeBorder,0));
etk::Color<float> myColor(0x0000FF99);
_draw->draw(localVertices, myColor, transformationMatrix, false, false);
localVertices.clear();
/** Bounding box == > model shape **/
localVertices.push_back(vec3(-lifeWidth/2.0 ,lifeYPos,0));
localVertices.push_back(vec3(-lifeWidth/2.0 ,lifeYPos + lifeHeight,0));
localVertices.push_back(vec3(-lifeWidth/2.0+lifeWidth*ratio,lifeYPos + lifeHeight,0));
localVertices.push_back(vec3(-lifeWidth/2.0 ,lifeYPos,0));
localVertices.push_back(vec3(-lifeWidth/2.0+lifeWidth*ratio,lifeYPos + lifeHeight,0));
localVertices.push_back(vec3(-lifeWidth/2.0+lifeWidth*ratio,lifeYPos,0));
myColor =0x00FF00FF;
if (ratio < 0.2f) {
myColor = 0xFF0000FF;
} else if (ratio < 0.4f) {
myColor = 0xDA7B00FF;
}
_draw->draw(localVertices, myColor, transformationMatrix, false, false);
#endif
}
static void drawShape(const btCollisionShape* _shape,
const std::shared_ptr<ewol::resource::Colored3DObject>& _draw,
mat4 _transformationMatrix,
@ -284,7 +154,7 @@ static void drawShape(const btCollisionShape* _shape,
//EGE_DEBUG(" draw (01): SPHERE_SHAPE_PROXYTYPE");
const btSphereShape* sphereShape = static_cast<const btSphereShape*>(_shape);
float radius = sphereShape->getMargin();//radius doesn't include the margin, so draw with margin
drawSphere(_draw, radius, 10, 10, _transformationMatrix, tmpColor);
// TODO : drawSphere(_draw, radius, 10, 10, _transformationMatrix, tmpColor);
break;
}
case BOX_SHAPE_PROXYTYPE: {
@ -434,14 +304,8 @@ static void drawShape(const btCollisionShape* _shape,
}
}
void ege::ElementGame::drawDebug(const std::shared_ptr<ewol::resource::Colored3DObject>& _draw, const ege::Camera& _camera) {
m_debugText.clear();
m_debugText.setColor(etk::Color<>(0x00, 0xFF, 0x00, 0xFF));
m_debugText.setPos(vec3(-20,32,0));
m_debugText.print(getType());
m_debugText.setPos(vec3(-20,20,0));
m_debugText.print("life=("+etk::to_string(getLifeRatio()));
//m_debugText.print(std::string("Axe=(")+std::string(m_tmpAxe.x())+std::string(",")+etk::UString(m_tmpAxe.y())+etk::UString(",")+etk::UString(m_tmpAxe.z())+etk::UString(")"));
void ege::ElementPhysic::drawDebug(const std::shared_ptr<ewol::resource::Colored3DObject>& _draw, const ege::Camera& _camera) {
ege::Element::drawDebug(_draw, _camera);
btScalar mmm[16];
btDefaultMotionState* myMotionState = (btDefaultMotionState*)m_body->getMotionState();
myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(mmm);
@ -452,15 +316,9 @@ void ege::ElementGame::drawDebug(const std::shared_ptr<ewol::resource::Colored3D
// note : set the vertice here to prevent multiple allocations...
std::vector<vec3> EwolVertices;
drawShape(m_shape, _draw, transformationMatrix, EwolVertices);
/*
m_debugText.draw( etk::matTranslate(getPosition())
* etk::matRotate(vec3(0,0,1),_camera.getAngleZ())
* etk::matRotate(vec3(1,0,0),(M_PI/2.0f-_camera.getAngleTeta()))
* etk::matScale(vec3(0.05,0.05,0.05)));
*/
}
void ege::ElementGame::draw(int32_t _pass) {
void ege::ElementPhysic::draw(int32_t _pass) {
if (false == m_elementInPhysicsSystem) {
return;
}
@ -479,35 +337,35 @@ void ege::ElementGame::draw(int32_t _pass) {
}
}
void ege::ElementGame::dynamicEnable() {
void ege::ElementPhysic::dynamicEnable() {
if (true == m_elementInPhysicsSystem) {
return;
}
if(nullptr!=m_body) {
m_env->getDynamicWorld()->addRigidBody(m_body);
m_env->getPhysicEngine().getDynamicWorld()->addRigidBody(m_body);
}
if(nullptr!=m_IA) {
m_env->getDynamicWorld()->addAction(m_IA);
m_env->getPhysicEngine().getDynamicWorld()->addAction(m_IA);
}
m_elementInPhysicsSystem = true;
}
void ege::ElementGame::dynamicDisable() {
void ege::ElementPhysic::dynamicDisable() {
if (false == m_elementInPhysicsSystem) {
return;
}
if(nullptr!=m_IA) {
m_env->getDynamicWorld()->removeAction(m_IA);
m_env->getPhysicEngine().getDynamicWorld()->removeAction(m_IA);
}
if(nullptr!=m_body) {
// Unlink element from the engine
m_env->getDynamicWorld()->removeRigidBody(m_body);
m_env->getDynamicWorld()->removeCollisionObject(m_body);
m_env->getPhysicEngine().getDynamicWorld()->removeRigidBody(m_body);
m_env->getPhysicEngine().getDynamicWorld()->removeCollisionObject(m_body);
}
m_elementInPhysicsSystem = false;
}
void ege::ElementGame::iaEnable() {
void ege::ElementPhysic::iaEnable() {
if (nullptr != m_IA) {
// IA already started ...
return;
@ -518,17 +376,17 @@ void ege::ElementGame::iaEnable() {
return;
}
if (true == m_elementInPhysicsSystem) {
m_env->getDynamicWorld()->addAction(m_IA);
m_env->getPhysicEngine().getDynamicWorld()->addAction(m_IA);
}
}
void ege::ElementGame::iaDisable() {
void ege::ElementPhysic::iaDisable() {
if (nullptr == m_IA) {
// IA already stopped ...
return;
}
if (true == m_elementInPhysicsSystem) {
m_env->getDynamicWorld()->removeAction(m_IA);
m_env->getPhysicEngine().getDynamicWorld()->removeAction(m_IA);
}
// remove IA :
delete(m_IA);

View File

@ -0,0 +1,170 @@
/**
* @author Edouard DUPIN
*
* @copyright 2011, Edouard DUPIN, all right reserved
*
* @license BSD v3 (see license file)
*/
#ifndef __EGE_ELEMENT_PHYSIC_H__
#define __EGE_ELEMENT_PHYSIC_H__
#include <etk/types.h>
#include <etk/math/Vector3D.h>
#include <etk/math/Matrix4.h>
#include <vector>
#include <ewol/debug.h>
#include <ewol/widget/Widget.h>
#include <ewol/openGL/openGL.h>
#include <ewol/resource/Colored3DObject.h>
#include <ege/resource/Mesh.h>
#include <ege/camera/Camera.h>
#include <ewol/compositing/Text.h>
#include <ege/Environement.h>
#include <ege/elements/Element.h>
#define INDEX_RIGHT_AXIS (0)
#define INDEX_FORWARD_AXIS (1)
#define INDEX_UP_AXIS (2)
#define ELEMENT_SCALE (1.0f/8.0f)
namespace ege {
class ElementPhysic : public ege::Element {
private:
static void FunctionFreeShape(void* _pointer);
protected:
btRigidBody* m_body; //!< all the element have a body == > otherwise it will be not manage with this system...
public:
/**
* @brief Constructor (when constructer is called just add element that did not change.
* The objest will be stored in a pool of element and keep a second time if needed == > redure memory allocation,
* when needed, the system will call the init and un-init function...
*/
ElementPhysic(const std::shared_ptr<ege::Environement>& _env);
/**
* @brief Destructor
*/
virtual ~ElementPhysic();
/**
* @brief get the element Type description string.
* @return A reference on the descriptive string.
*/
virtual const std::string& getType() const;
protected:
btCollisionShape* m_shape; //!< shape of the element (set a copy here to have the debug display of it)
public:
/**
* @brief set the shape properties.
* @param[in] _shape The shape pointer.
* @note : this remove the shape properties.
* @return true if no error occured
*/
bool setShape(btCollisionShape* _shape);
/**
* @brief get a pointer on the bullet collision shape.
* @return the collision pointer.
*/
inline btCollisionShape* getShape() {
return m_shape;
};
private:
/**
* @brief remove the curent selected shape.
*/
void removeShape();
public:
virtual bool setMesh(const std::shared_ptr<ege::resource::Mesh>& _mesh);
/**
* @brief draw the curent element (can have multiple display)
* @param[in] pass Id of the current pass : [0..?]
*/
virtual void draw(int32_t _pass=0);
/**
* @brief draw the current life of the element
*/
// virtual void drawLife(const std::shared_ptr<ewol::resource::Colored3DObject>& _draw, const ege::Camera& _camera);
/**
* @brief get the theoric position. Sometimes, the element has move due to an explosion or something else, then its real position in not the one that woult it be at the end ...
* @return the theoric position
*/
virtual vec3 getPositionTheoric() {
return getPosition();
};
/**
* @brief set the current Theoric position of the element
* @param[in] set the 3D position.
*/
virtual void setPositionTheoric(const vec3& _pos) { };
/**
* @brief get the current speed of the element
* @return the 3D speed.
*/
const vec3& getSpeed();
/**
* @brief get the current mass of the element
* @return the mass in kG.
*/
const float getInvMass();
protected:
bool m_elementInPhysicsSystem;
public:
virtual void dynamicEnable();
virtual void dynamicDisable();
private:
class localIA : public btActionInterface {
private:
ege::ElementPhysic& m_element;
public:
/**
* @brief Constructor
*/
localIA(ElementPhysic& _element) :
m_element(_element) {
};
/**
* @brief Destructor
*/
virtual ~localIA() {
};
public: // herited function
void debugDraw(btIDebugDraw* _debugDrawer) {
};
void updateAction(btCollisionWorld* _collisionWorld, btScalar _step) {
m_element.iaAction(_step);
};
};
localIA* m_IA;
public:
/**
* @brief enable periodic call Of this object for processing Artificial Intelligence
*/
void iaEnable();
/**
* @brief disable periodic call Of this object for processing Artificial Intelligence
*/
void iaDisable();
/**
* @brief periodic call for intelligence artificial.
* @param[in] step : step of time in s
*/
virtual void iaAction(float _step) { };
/**
* @brief, call when the element is removed (call only one time
*/
virtual void onDestroy() {};
virtual const vec3& getPosition();
virtual void setPosition(const vec3& _pos);
virtual void drawDebug(const std::shared_ptr<ewol::resource::Colored3DObject>& _draw, const ege::Camera& _camera);
};
};
#endif

View File

@ -9,6 +9,21 @@
#include <ege/physics/Engine.h>
#include <ewol/openGL/openGL.h>
#include <etk/math/Matrix4.h>
#include <BulletDynamics/Dynamics/btRigidBody.h>
#include <LinearMath/btDefaultMotionState.h>
#include <BulletDynamics/Dynamics/btDynamicsWorld.h>
#include <BulletCollision/CollisionShapes/btCollisionShape.h>
#include <LinearMath/btIDebugDraw.h>
#include <btBulletCollisionCommon.h>
#include <BulletCollision/CollisionShapes/btConvexPolyhedron.h>
#include <BulletCollision/CollisionShapes/btShapeHull.h>
#include <LinearMath/btTransformUtil.h>
#include <LinearMath/btIDebugDraw.h>
#include <btBulletDynamicsCommon.h>
#include <BulletCollision/CollisionDispatch/btCollisionObject.h>
ege::physics::Engine::Engine() {
setBulletConfig();
@ -24,43 +39,43 @@ ege::physics::Engine::~Engine() {
*/
}
void ege::physics::Engine::setBulletConfig(std::make_shared(btDefaultCollisionConfiguration> _collisionConfiguration,
std::make_shared(btCollisionDispatcher> _dispatcher,
std::make_shared(btBroadphaseInterface> _broadphase,
std::make_shared(btConstraintSolver> _solver,
std::make_shared(btDynamicsWorld> _dynamicsWorld) {
void ege::physics::Engine::setBulletConfig(std::shared_ptr<btDefaultCollisionConfiguration> _collisionConfiguration,
std::shared_ptr<btCollisionDispatcher> _dispatcher,
std::shared_ptr<btBroadphaseInterface> _broadphase,
std::shared_ptr<btConstraintSolver> _solver,
std::shared_ptr<btDynamicsWorld> _dynamicsWorld) {
if (_collisionConfiguration != nullptr) {
m_collisionConfiguration = _collisionConfiguration;
} else {
m_collisionConfiguration = std::make_shared(btDefaultCollisionConfiguration());
m_collisionConfiguration = std::make_shared<btDefaultCollisionConfiguration>();
}
///use the default collision dispatcher.
if (_dispatcher != nullptr) {
m_dispatcher = _dispatcher;
} else {
m_dispatcher = std::make_shared(btCollisionDispatcher(m_collisionConfiguration));
m_dispatcher = std::make_shared<btCollisionDispatcher>(m_collisionConfiguration.get());
}
if (_broadphase != nullptr) {
m_broadphase = _broadphase;
} else {
m_broadphase = std::make_shared(btDbvtBroadphase());
m_broadphase = std::make_shared<btDbvtBroadphase>();
}
///the default constraint solver.
if (_solver != nullptr) {
m_solver = _solver;
} else {
m_solver = std::make_shared(btSequentialImpulseConstraintSolver());
m_solver = std::make_shared<btSequentialImpulseConstraintSolver>();
}
if (_dynamicsWorld != nullptr) {
m_dynamicsWorld = _dynamicsWorld;
} else {
m_dynamicsWorld = std::make_shared(btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration));
m_dynamicsWorld = std::make_shared<btDiscreteDynamicsWorld>(m_dispatcher.get(),m_broadphase.get(),m_solver.get(),m_collisionConfiguration.get());
// By default we set no gravity
m_dynamicsWorld->setGravity(btVector3(0,0,0));
}
m_env.setDynamicWorld(m_dynamicsWorld);
//m_env.setDynamicWorld(m_dynamicsWorld);
}

View File

@ -14,11 +14,10 @@
#include <etk/math/Matrix4.h>
#include <vector>
#include <ewol/debug.h>
#include <ege/Camera.h>
#include <ege/camera/Camera.h>
#include <ewol/widget/Widget.h>
#include <ewol/openGL/openGL.h>
#include <ewol/resource/Manager.h>
#include <ege/ElementGame.h>
#include <ewol/Dimension.h>
class btBroadphaseInterface;
@ -32,13 +31,14 @@ class btDynamicsWorld;
#include <LinearMath/btScalar.h>
class btVector3;
//#include <ege/elements/Element.h>
namespace ege {
namespace physics {
class Engine {
private:
///this is the most important class
std::shared_ptr<btDefaultCollisionConfiguration< m_collisionConfiguration;
std::shared_ptr<btDefaultCollisionConfiguration> m_collisionConfiguration;
std::shared_ptr<btCollisionDispatcher> m_dispatcher;
std::shared_ptr<btBroadphaseInterface> m_broadphase;
std::shared_ptr<btConstraintSolver> m_solver;
@ -51,6 +51,20 @@ namespace ege {
std::shared_ptr<btBroadphaseInterface> _broadphase=nullptr,
std::shared_ptr<btConstraintSolver> _solver=nullptr,
std::shared_ptr<btDynamicsWorld> _dynamicsWorld=nullptr);
/**
* @brief set the curent world
* @param[in] _newWorld Pointer on the current world
*/
void setDynamicWorld(const std::shared_ptr<btDynamicsWorld>& _newWorld) {
m_dynamicsWorld=_newWorld;
};
/**
* @brief get the curent world
* @return pointer on the current world
*/
std::shared_ptr<btDynamicsWorld> getDynamicWorld() {
return m_dynamicsWorld;
};
};
};
};

View File

@ -258,19 +258,44 @@ void ege::resource::Mesh::generateVBO() {
// when no normal detected == > auto generate Face normal ....
calculateNormaleFace(m_listFaces.getKeys()[0]);
}
// generate element in 2 pass :
// - create new index dependeng a vertex is a unique componenet of position, texture, normal
// - the index list generation (can be dynamic ... (TODO later)
for (int32_t kkk=0; kkk<m_listFaces.size(); kkk++) {
// clean faces indexes :
m_listFaces.getValue(kkk).m_index.clear();
int32_t nbIndicInFace = 3;
switch (m_materials[m_listFaces.getKey(kkk)]->getRenderMode()) {
case ewol::openGL::renderTriangle:
case ewol::openGL::renderTriangleStrip:
case ewol::openGL::renderTriangleFan:
nbIndicInFace = 3;
break;
case ewol::openGL::renderLine:
case ewol::openGL::renderLineStrip:
case ewol::openGL::renderLineLoop:
nbIndicInFace = 2;
break;
case ewol::openGL::renderPoint:
nbIndicInFace = 1;
break;
case ewol::openGL::renderQuad:
case ewol::openGL::renderQuadStrip:
nbIndicInFace = 4;
break;
case ewol::openGL::renderPolygon:
nbIndicInFace = 3;
break;
}
#ifdef TRY_MINIMAL_VBO
int64_t tmpppppp=0;
#endif
FaceIndexing& tmpFaceList = m_listFaces.getValue(kkk);
for (size_t iii=0; iii<tmpFaceList.m_faces.size() ; iii++) {
int32_t vertexVBOId[3];
for(size_t indice=0 ; indice<3; indice++) {
for(size_t indice=0 ; indice<nbIndicInFace; indice++) {
// ghet position
vec3 position = m_listVertex[tmpFaceList.m_faces[iii].m_vertex[indice]];
// get Color
@ -1077,7 +1102,7 @@ void ege::resource::Mesh::addTriangle(const std::string& _layerName,
}
ewol::openGL::renderMode tmpRenderMode = m_materials[_layerName]->getRenderMode();
if ( tmpRenderMode != ewol::openGL::renderTriangle
&& tmpRenderMode != ewol::openGL::renderLineStrip
&& tmpRenderMode != ewol::openGL::renderTriangleStrip
&& tmpRenderMode != ewol::openGL::renderTriangleFan) {
EGE_ERROR("try to add Line in a mesh material section that not support Line");
return;

View File

@ -83,11 +83,11 @@ void ege::widget::Scene::onDraw() {
std::shared_ptr<ege::Camera> camera = m_env->getCamera(m_cameraName);
//EGE_DEBUG("Draw (start)");
mat4 tmpMatrix;
std::shared_ptr<btDynamicsWorld> world = m_env->getDynamicWorld();
std::shared_ptr<btDynamicsWorld> world = m_env->getPhysicEngine().getDynamicWorld();
if (world != nullptr) {
m_env->getOrderedElementForDisplay(m_displayElementOrdered, camera->getEye(), camera->getViewVector());
//EGE_DEBUG("DRAW : " << m_displayElementOrdered.size() << " elements");
EGE_DEBUG("DRAW : " << m_displayElementOrdered.size() << "/" << m_env->getElement().size() << " elements");
// TODO : remove this == > no more needed ==> checked in the generate the list of the element ordered
for (size_t iii=0; iii<m_displayElementOrdered.size(); iii++) {
@ -104,6 +104,8 @@ void ege::widget::Scene::onDraw() {
m_displayElementOrdered[iii].element->draw(pass);
}
}
} else {
EGE_WARNING("No Dynamic world ...");
}
if (camera != nullptr) {
m_env->getParticuleEngine().draw(*camera);

View File

@ -18,7 +18,7 @@
#include <ewol/widget/Widget.h>
#include <ewol/openGL/openGL.h>
#include <ewol/resource/Manager.h>
#include <ege/ElementGame.h>
#include <ege/elements/Element.h>
#include <ewol/Dimension.h>
class btBroadphaseInterface;

View File

@ -19,7 +19,10 @@ def create(target):
'ege/camera/View.cpp',
'ege/camera/FPS.cpp',
'ege/CollisionShapeCreator.cpp',
'ege/ElementGame.cpp',
'ege/physics/Engine.cpp',
'ege/elements/Element.cpp',
'ege/elements/ElementBase.cpp',
'ege/elements/ElementPhysic.cpp',
'ege/Particule.cpp',
'ege/ParticuleEngine.cpp',
'ege/ParticuleSimple.cpp',

View File

@ -14,6 +14,8 @@
#include <ege/widget/Scene.h>
#include <ege/camera/View.h>
#include <etk/tool.h>
#include <ege/elements/ElementBase.h>
#include <ege/elements/ElementPhysic.h>
#undef __class__
#define __class__ "Windows"
@ -137,7 +139,12 @@ void appl::Windows::init() {
myMesh->addMaterial("basics", material);
myMesh->createIcoSphere("basics", 16, 3);
myMesh->generateVBO();
m_env->addStaticMeshToDraw(myMesh);
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);
element->setPosition(vec3(50,0,0));
element->setMesh(myMesh);
m_env->addElement(element);
//m_env->addStaticMeshToDraw(myMesh);
}
}
}
@ -146,7 +153,7 @@ void appl::Windows::init() {
void appl::Windows::onCallbackPeriodicUpdateCamera(const ewol::event::Time& _event) {
static float offset = 0;
offset += 0.01;
m_camera->setEye(vec3(100*std::sin(offset),100*std::cos(offset),40));
m_camera->setEye(vec3(100*std::sin(offset),100*std::cos(offset),40)+vec3(50,0,0));
}