[DEV] try to create concave shape

This commit is contained in:
Edouard DUPIN 2017-06-07 21:05:33 +02:00
parent a87d61c996
commit 307e8bc944
10 changed files with 196 additions and 24 deletions

View File

@ -70,6 +70,7 @@ namespace ege {
m_groupSource(_groupSource),
m_positionSource(_pos)
{ };
virtual ~EntityInteraction() = default;
public:
virtual void applyEvent(ege::Entity& _entity) { };
};

View File

@ -13,6 +13,9 @@
#include <ege/physics/shape/ConvexHull.hpp>
#include <ege/physics/shape/Cylinder.hpp>
#include <ege/physics/shape/Sphere.hpp>
#include <ege/physics/shape/Concave.hpp>
#include <ephysics/collision/shapes/ConcaveShape.h>
#include <ephysics/collision/shapes/ConcaveMeshShape.h>
const std::string& ege::physics::Component::getType() const {
static std::string tmp("physics");
@ -63,7 +66,13 @@ ege::physics::Component::Component(ememory::SharedPtr<ege::Environement> _env, c
EGE_ERROR("Bounciness=" << m_rigidBody->getMaterial().getBounciness());
EGE_ERROR("FrictionCoefficient=" << m_rigidBody->getMaterial().getFrictionCoefficient());
EGE_ERROR("RollingResistance=" << m_rigidBody->getMaterial().getRollingResistance());
m_rigidBody->getMaterial().setFrictionCoefficient(0.5);
EGE_ERROR("LinearDamping=" << m_rigidBody->getLinearDamping());
EGE_ERROR("AngularDamping=" << m_rigidBody->getAngularDamping());
m_rigidBody->getMaterial().setBounciness(0.4);
//m_rigidBody->getMaterial().setFrictionCoefficient(0.01);
//m_rigidBody->getMaterial().setRollingResistance(0.01);
m_rigidBody->setAngularDamping(0.9);
m_rigidBody->setLinearDamping(0.9);
}
void ege::physics::Component::setType(enum ege::physics::Component::type _type) {
@ -117,7 +126,7 @@ void ege::physics::Component::generate() {
tmpElement->getSize().y(),
tmpElement->getSize().z());
// Create the box shape
rp3d::BoxShape* shape = new rp3d::BoxShape(halfExtents);
rp3d::BoxShape* shape = new rp3d::BoxShape(halfExtents, 0.0001);
m_listShape.push_back(shape);
rp3d::Vector3 position(it->getOrigin().x(),
it->getOrigin().y(),
@ -230,24 +239,52 @@ void ege::physics::Component::generate() {
m_listProxyShape.push_back(proxyShape);
break;
}
case ege::physics::Shape::type::convexHull: {
EGE_DEBUG(" convexHull");
const ege::physics::shape::ConvexHull* tmpElement = it->toConvexHull();
case ege::physics::Shape::type::concave: {
EGE_DEBUG(" Concave");
const ege::physics::shape::Concave* tmpElement = it->toConcave();
if (tmpElement == nullptr) {
EGE_ERROR(" convexHull ==> can not cast in convexHull");
EGE_ERROR(" Concave ==> can not cast in Concave");
continue;
}
float* vertices = (float*)&tmpElement->getVertex()[0];
int* indices = (int*)&tmpElement->getIndices()[0];
int32_t nbVertices = tmpElement->getVertex().size();
int32_t nbTriangles = tmpElement->getIndices().size()/3;
// TODO : Manage memory leak ...
//we have an error here ...
rp3d::TriangleVertexArray* triangleArray = new rp3d::TriangleVertexArray(nbVertices,
vertices,
4 * sizeof(float),
nbTriangles,
indices,
sizeof(int32_t),
rp3d::TriangleVertexArray::VERTEX_FLOAT_TYPE,
rp3d::TriangleVertexArray::INDEX_INTEGER_TYPE);
// Now that we have a TriangleVertexArray, we need to create a TriangleMesh and add the TriangleVertexArray into it as a subpart.
// Once this is done, we can create the actual ConcaveMeshShape and add it to the body we want to simulate as in the following example:
rp3d::TriangleMesh triangleMesh;
// Add the triangle vertex array to the triangle mesh
triangleMesh.addSubpart(triangleArray);
// Create the concave mesh shape
/*
btConvexHullShape* tmpShape = new btConvexHullShape(&(tmpElement->getPointList()[0].x()), tmpElement->getPointList().size());
if (tmpShape != nullptr) {
if (outputShape == nullptr) {
return tmpShape;
} else {
vec4 qqq = tmpElement->getQuaternion();
const btTransform localTransform(btQuaternion(qqq.x(),qqq.y(),qqq.z(),qqq.w()), tmpElement->getOrigin());
outputShape->addChildShape(localTransform, tmpShape);
}
}
// TODO : Manage memory leak ...
reactphysics3d::ConcaveShape* shape = new reactphysics3d::ConcaveMeshShape(&triangleMesh);
rp3d::Vector3 position(it->getOrigin().x(),
it->getOrigin().y(),
it->getOrigin().z());
rp3d::Quaternion orientation(it->getQuaternion().x(),
it->getQuaternion().y(),
it->getQuaternion().z(),
it->getQuaternion().w());
// The ephysic use Y as UP ==> ege use Z as UP
orientation = orientation * rp3d::Quaternion(-0.707107, 0, 0, 0.707107);
rp3d::Transform transform(position, orientation);
rp3d::ProxyShape* proxyShape = m_rigidBody->addCollisionShape(shape, transform, it->getMass());
proxyShape->setUserData(this);
m_listProxyShape.push_back(proxyShape);
*/
break;
}

View File

@ -0,0 +1,16 @@
/** @file
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#include <ege/debug.hpp>
#include <ege/physics/shape/Concave.hpp>
bool ege::physics::shape::Concave::parse(const char* _line) {
if (ege::physics::Shape::parse(_line) == true) {
return true;
}
return false;
}

View File

@ -0,0 +1,66 @@
/** @file
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#pragma once
#include <etk/types.hpp>
#include <ege/physics/shape/Shape.hpp>
namespace ege {
namespace physics {
namespace shape {
class Concave : public ege::physics::Shape {
public:
Concave() {};
virtual ~Concave() {};
public:
virtual bool parse(const char* _line);
virtual void display() {};
public:
virtual enum ege::physics::Shape::type getType() const {
return ege::physics::Shape::type::concave;
}
public:
virtual const ege::physics::shape::Concave* toConcave() const {
return this;
}
virtual ege::physics::shape::Concave* toConcave() {
return this;
}
private:
std::vector<vec3> m_listVertex;
std::vector<int> m_indices; // TODO : do it better: this is dependent of ephysics
public:
void clear() {
m_listVertex.clear();
m_indices.clear();
}
void setListOfVertex(const std::vector<vec3>& _listVertex) {
m_listVertex = _listVertex;
}
void addTriangle(const std::vector<uint32_t>& _index) {
/*
if (m_indices.size() == 0) {
m_indices = _index;
return;
}
*/
for (auto &it: _index) {
m_indices.push_back(it);
}
}
const std::vector<vec3>& getVertex() const {
return m_listVertex;
}
const std::vector<int>& getIndices() const {
return m_indices;
}
};
}
}
}

View File

@ -31,6 +31,7 @@ bool ege::physics::shape::ConvexHull::parse(const char* _line) {
EGE_VERBOSE(" parsed " << m_points[iii]);
}
*/
return true;
}
if(strncmp(_line, "scale:", 6) == 0) {

View File

@ -11,6 +11,7 @@
#include <ege/physics/shape/ConvexHull.hpp>
#include <ege/physics/shape/Cylinder.hpp>
#include <ege/physics/shape/Sphere.hpp>
#include <ege/physics/shape/Concave.hpp>
ememory::SharedPtr<ege::physics::Shape> ege::physics::Shape::create(const std::string& _name) {
@ -28,8 +29,10 @@ ememory::SharedPtr<ege::physics::Shape> ege::physics::Shape::create(const std::s
tmpp = ememory::makeShared<ege::physics::shape::Capsule>();
} else if (name == "convexhull") {
tmpp = ememory::makeShared<ege::physics::shape::ConvexHull>();
} else if (name == "autoconcave") {
tmpp = ememory::makeShared<ege::physics::shape::Concave>();
} else {
EGE_ERROR("Create an unknow element : '" << _name << "' availlable : [BOX,SPHERE,CONE,CYLINDER,CAPSULE,CONVEXHULL]");
EGE_ERROR("Create an unknow element : '" << _name << "' availlable : [BOX,SPHERE,CONE,CYLINDER,CAPSULE,CONVEXHULL,autoConcave]");
return nullptr;
}
if (tmpp == nullptr) {

View File

@ -21,6 +21,7 @@ namespace ege {
class Cone;
class ConvexHull;
class Sphere;
class Concave;
}
class Shape {
@ -34,7 +35,8 @@ namespace ege {
cone,
convexHull,
cylinder,
sphere
sphere,
concave
};
public:
Shape() :
@ -97,6 +99,9 @@ namespace ege {
bool isSphere() {
return getType() == ege::physics::Shape::type::sphere;
};
bool isConcave() {
return getType() == ege::physics::Shape::type::concave;
};
virtual const ege::physics::shape::Box* toBox() const {
return nullptr;
@ -139,6 +144,12 @@ namespace ege {
virtual ege::physics::shape::Sphere* toSphere() {
return nullptr;
};
virtual const ege::physics::shape::Concave* toConcave() const {
return nullptr;
};
virtual ege::physics::shape::Concave* toConcave() {
return nullptr;
};
};
}
}

View File

@ -398,9 +398,9 @@ void ege::resource::Mesh::generateVBO() {
}
EGE_WARNING("Generate VBO for nb faces layers: " << m_listFaces.size() << " list layer=" << etk::to_string(m_listFaces.getKeys()));
// 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)
// generate element in 2 pass:
// - create new index depending on a vertex is a unique component 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();
@ -711,3 +711,26 @@ void ege::resource::Mesh::addTriangle(const std::string& _layerName, const vec3&
return;
}
}
#include <ege/physics/shape/Concave.hpp>
const std::vector<ememory::SharedPtr<ege::physics::Shape>>& ege::resource::Mesh::getPhysicalProperties() {
for (auto &it: m_physics) {
if (it->getType() == ege::physics::Shape::type::concave) {
// need to generate the internal list of point and triangle needed:
ege::physics::shape::Concave* tmpElement = it->toConcave();
if (tmpElement == nullptr) {
EGE_ERROR(" Concave ==> can not cast in Concave");
return m_physics;
}
tmpElement->clear();
tmpElement->setListOfVertex(m_listVertex);
for (int32_t kkk=0; kkk<m_listFaces.size(); kkk++) {
tmpElement->addTriangle(m_listFaces.getValue(kkk).m_index);
}
// Can have only one concave element in a mesh ...
return m_physics;
}
}
return m_physics;
}

View File

@ -28,6 +28,20 @@
namespace ege {
namespace resource {
/* TODO: A mesh is composed with:
* - a geometric property:
* * list of points
* * list of normals
* * List of texture UV mapping
* + List of triangle
* + List of quad
* + List of polygones
* - List of material
* - Physics shapes
* - UV texture
* - Normal texture
* - open GL shaders
*/
class Mesh : public gale::Resource {
public:
static ememory::SharedPtr<ege::resource::Mesh> createGrid(int32_t _lineCount,
@ -149,9 +163,7 @@ namespace ege {
bool getCheckNormal() {
return m_checkNormal;
};
const std::vector<ememory::SharedPtr<ege::physics::Shape>>& getPhysicalProperties() const {
return m_physics;
};
const std::vector<ememory::SharedPtr<ege::physics::Shape>>& getPhysicalProperties();
void addPhysicElement(const ememory::SharedPtr<ege::physics::Shape>& _shape) {
m_physics.push_back(_shape);
}

View File

@ -72,6 +72,7 @@ def configure(target, my_module):
'ege/physics/shape/ConvexHull.cpp',
'ege/physics/shape/Cylinder.cpp',
'ege/physics/shape/Sphere.cpp',
'ege/physics/shape/Concave.cpp',
'ege/Ray.cpp',
])
my_module.copy_path('data/ParticuleMesh.*')
@ -121,6 +122,7 @@ def configure(target, my_module):
'ege/physics/shape/ConvexHull.hpp',
'ege/physics/shape/Cylinder.hpp',
'ege/physics/shape/Sphere.hpp',
'ege/physics/shape/Concave.hpp',
'ege/Ray.hpp',
])
# TODO: Remove this ...