[DEV] Rework can be good
This commit is contained in:
parent
0d7b7b7465
commit
8b4f2f2865
@ -7,76 +7,33 @@
|
|||||||
// Libraries
|
// Libraries
|
||||||
#include <ephysics/collision/TriangleVertexArray.hpp>
|
#include <ephysics/collision/TriangleVertexArray.hpp>
|
||||||
|
|
||||||
using namespace ephysics;
|
|
||||||
|
|
||||||
// Constructor
|
ephysics::TriangleVertexArray::TriangleVertexArray(const std::vector<vec3>& _vertices, std::vector<size_t> _triangles):
|
||||||
/// Note that your data will not be copied int32_to the TriangleVertexArray and
|
m_vertices(_vertices),
|
||||||
/// therefore, you need to make sure that those data are always valid during
|
m_triangles(_triangles) {
|
||||||
/// the lifetime of the TriangleVertexArray.
|
|
||||||
/**
|
|
||||||
* @param nbVertices Number of vertices in the array
|
|
||||||
* @param verticesStart Pointer to the first vertices of the array
|
|
||||||
* @param verticesStride Number of bytes between the beginning of two consecutive vertices
|
|
||||||
* @param nbTriangles Number of triangles in the array
|
|
||||||
* @param indexesStart Pointer to the first triangle index
|
|
||||||
* @param indexesStride Number of bytes between the beginning of two consecutive triangle indices
|
|
||||||
* @param vertexDataType Type of data for the vertices (float, double)
|
|
||||||
* @param indexDataType Type of data for the indices (short, int32_t)
|
|
||||||
*/
|
|
||||||
TriangleVertexArray::TriangleVertexArray(uint32_t nbVertices, void* verticesStart, int32_t verticesStride,
|
|
||||||
uint32_t nbTriangles, void* indexesStart, int32_t indexesStride,
|
|
||||||
VertexDataType vertexDataType, IndexDataType indexDataType) {
|
|
||||||
m_numberVertices = nbVertices;
|
|
||||||
m_verticesStart = reinterpret_cast<unsigned char*>(verticesStart);
|
|
||||||
m_verticesStride = verticesStride;
|
|
||||||
m_numberTriangles = nbTriangles;
|
|
||||||
m_indicesStart = reinterpret_cast<unsigned char*>(indexesStart);
|
|
||||||
m_indicesStride = indexesStride;
|
|
||||||
m_vertexDataType = vertexDataType;
|
|
||||||
m_indexDataType = indexDataType;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
size_t ephysics::TriangleVertexArray::getNbVertices() const {
|
||||||
TriangleVertexArray::~TriangleVertexArray() {
|
return m_vertices.size();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the vertex data type
|
size_t ephysics::TriangleVertexArray::getNbTriangles() const {
|
||||||
TriangleVertexArray::VertexDataType TriangleVertexArray::getVertexDataType() const {
|
return m_triangles.size()/3;
|
||||||
return m_vertexDataType;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the index data type
|
const std::vector<vec3>& ephysics::TriangleVertexArray::getVertices() const {
|
||||||
TriangleVertexArray::IndexDataType TriangleVertexArray::getIndexDataType() const {
|
return m_vertices;
|
||||||
return m_indexDataType;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the number of vertices
|
const std::vector<size_t>& ephysics::TriangleVertexArray::getIndices() const{
|
||||||
uint32_t TriangleVertexArray::getNbVertices() const {
|
return m_triangles;
|
||||||
return m_numberVertices;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the number of triangles
|
ephysics::Triangle ephysics::TriangleVertexArray::getTriangle(size_t _id) const {
|
||||||
uint32_t TriangleVertexArray::getNbTriangles() const {
|
ephysics::Triangle out;
|
||||||
return m_numberTriangles;
|
out[0] = m_vertices[m_triangles[_id*3]];
|
||||||
}
|
out[1] = m_vertices[m_triangles[_id*3+1]];
|
||||||
|
out[2] = m_vertices[m_triangles[_id*3+2]];
|
||||||
// Return the vertices stride (number of bytes)
|
return out;
|
||||||
int32_t TriangleVertexArray::getVerticesStride() const {
|
|
||||||
return m_verticesStride;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the indices stride (number of bytes)
|
|
||||||
int32_t TriangleVertexArray::getIndicesStride() const {
|
|
||||||
return m_indicesStride;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the pointer to the start of the vertices array
|
|
||||||
unsigned char* TriangleVertexArray::getVerticesStart() const {
|
|
||||||
return m_verticesStart;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the pointer to the start of the indices array
|
|
||||||
unsigned char* TriangleVertexArray::getIndicesStart() const {
|
|
||||||
return m_indicesStart;
|
|
||||||
}
|
}
|
@ -6,8 +6,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ephysics/configuration.hpp>
|
#include <ephysics/configuration.hpp>
|
||||||
|
#include <etk/math/Vector3D.hpp>
|
||||||
|
|
||||||
namespace ephysics {
|
namespace ephysics {
|
||||||
|
class Triangle {
|
||||||
|
public:
|
||||||
|
vec3 value[3];
|
||||||
|
vec3& operator[] (size_t _id) {
|
||||||
|
return value[_id];
|
||||||
|
}
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* This class is used to describe the vertices and faces of a triangular mesh.
|
* This class is used to describe the vertices and faces of a triangular mesh.
|
||||||
* A TriangleVertexArray represents a continuous array of vertices and indexes
|
* A TriangleVertexArray represents a continuous array of vertices and indexes
|
||||||
@ -18,43 +26,42 @@ namespace ephysics {
|
|||||||
* remains valid during the TriangleVertexArray life.
|
* remains valid during the TriangleVertexArray life.
|
||||||
*/
|
*/
|
||||||
class TriangleVertexArray {
|
class TriangleVertexArray {
|
||||||
public:
|
|
||||||
/// Data type for the vertices in the array
|
|
||||||
enum VertexDataType {VERTEX_FLOAT_TYPE, VERTEX_DOUBLE_TYPE};
|
|
||||||
/// Data type for the indices in the array
|
|
||||||
enum IndexDataType {INDEX_INTEGER_TYPE, INDEX_SHORT_TYPE};
|
|
||||||
protected:
|
protected:
|
||||||
uint32_t m_numberVertices; //!< Number of vertices in the array
|
std::vector<vec3> m_vertices; //!< Vertice list
|
||||||
unsigned char* m_verticesStart; //!< Pointer to the first vertex value in the array
|
std::vector<size_t> m_triangles; //!< List of triangle (3 pos for each triangle)
|
||||||
int32_t m_verticesStride; //!< Stride (number of bytes) between the beginning of two vertices values in the array
|
|
||||||
uint32_t m_numberTriangles; //!< Number of triangles in the array
|
|
||||||
unsigned char* m_indicesStart; //!< Pointer to the first vertex index of the array
|
|
||||||
int32_t m_indicesStride; //!< Stride (number of bytes) between the beginning of two indices in the array
|
|
||||||
VertexDataType m_vertexDataType; //!< Data type of the vertices in the array
|
|
||||||
IndexDataType m_indexDataType; //!< Data type of the indices in the array
|
|
||||||
public:
|
public:
|
||||||
/// Constructor
|
/**
|
||||||
TriangleVertexArray(uint32_t nbVertices, void* verticesStart, int32_t verticesStride,
|
* @brief Constructor
|
||||||
uint32_t nbTriangles, void* indexesStart, int32_t indexesStride,
|
* @param[in] _vertices List Of all vertices
|
||||||
VertexDataType vertexDataType, IndexDataType indexDataType);
|
* @param[in] _triangles List of all linked points
|
||||||
/// Destructor
|
*/
|
||||||
virtual ~TriangleVertexArray();
|
TriangleVertexArray(const std::vector<vec3>& _vertices,
|
||||||
/// Return the vertex data type
|
std::vector<size_t> _triangles);
|
||||||
VertexDataType getVertexDataType() const;
|
/**
|
||||||
/// Return the index data type
|
* @brief Get the number of vertices
|
||||||
IndexDataType getIndexDataType() const;
|
* @return Number of vertices
|
||||||
/// Return the number of vertices
|
*/
|
||||||
uint32_t getNbVertices() const;
|
size_t getNbVertices() const;
|
||||||
/// Return the number of triangles
|
/**
|
||||||
uint32_t getNbTriangles() const;
|
* @brief Get the number of triangle
|
||||||
/// Return the vertices stride (number of bytes)
|
* @return Number of triangles
|
||||||
int32_t getVerticesStride() const;
|
*/
|
||||||
/// Return the indices stride (number of bytes)
|
size_t getNbTriangles() const;
|
||||||
int32_t getIndicesStride() const;
|
/**
|
||||||
/// Return the pointer to the start of the vertices array
|
* @brief Get The table of the vertices
|
||||||
unsigned char* getVerticesStart() const;
|
* @return reference on the vertices
|
||||||
/// Return the pointer to the start of the indices array
|
*/
|
||||||
unsigned char* getIndicesStart() const;
|
const std::vector<vec3>& getVertices() const;
|
||||||
|
/**
|
||||||
|
* @brief Get The table of the triangle indice
|
||||||
|
* @return reference on the triangle indice
|
||||||
|
*/
|
||||||
|
const std::vector<size_t>& getIndices() const;
|
||||||
|
/**
|
||||||
|
* @brief Get a triangle at the specific ID
|
||||||
|
* @return Buffer of 3 points
|
||||||
|
*/
|
||||||
|
ephysics::Triangle getTriangle(size_t _id) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,153 +4,80 @@
|
|||||||
* @license BSD 3 clauses (see license file)
|
* @license BSD 3 clauses (see license file)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Libraries
|
|
||||||
#include <ephysics/collision/shapes/ConcaveMeshShape.hpp>
|
#include <ephysics/collision/shapes/ConcaveMeshShape.hpp>
|
||||||
|
#include <ephysics/debug.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
using namespace ephysics;
|
using namespace ephysics;
|
||||||
|
|
||||||
// Constructor
|
ConcaveMeshShape::ConcaveMeshShape(TriangleMesh* _triangleMesh):
|
||||||
ConcaveMeshShape::ConcaveMeshShape(TriangleMesh* triangleMesh):
|
|
||||||
ConcaveShape(CONCAVE_MESH) {
|
ConcaveShape(CONCAVE_MESH) {
|
||||||
m_triangleMesh = triangleMesh;
|
m_triangleMesh = _triangleMesh;
|
||||||
m_raycastTestType = FRONT;
|
m_raycastTestType = FRONT;
|
||||||
// Insert all the triangles int32_to the dynamic AABB tree
|
|
||||||
initBVHTree();
|
initBVHTree();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
|
||||||
ConcaveMeshShape::~ConcaveMeshShape() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert all the triangles int32_to the dynamic AABB tree
|
|
||||||
void ConcaveMeshShape::initBVHTree() {
|
void ConcaveMeshShape::initBVHTree() {
|
||||||
// TODO : Try to randomly add the triangles int32_to the tree to obtain a better tree
|
// TODO : Try to randomly add the triangles into the tree to obtain a better tree
|
||||||
// For each sub-part of the mesh
|
// For each sub-part of the mesh
|
||||||
for (uint32_t subPart=0; subPart<m_triangleMesh->getNbSubparts(); subPart++) {
|
for (uint32_t subPart=0; subPart<m_triangleMesh->getNbSubparts(); subPart++) {
|
||||||
// Get the triangle vertex array of the current sub-part
|
// Get the triangle vertex array of the current sub-part
|
||||||
TriangleVertexArray* triangleVertexArray = m_triangleMesh->getSubpart(subPart);
|
TriangleVertexArray* triangleVertexArray = m_triangleMesh->getSubpart(subPart);
|
||||||
TriangleVertexArray::VertexDataType vertexType = triangleVertexArray->getVertexDataType();
|
|
||||||
TriangleVertexArray::IndexDataType indexType = triangleVertexArray->getIndexDataType();
|
|
||||||
unsigned char* verticesStart = triangleVertexArray->getVerticesStart();
|
|
||||||
unsigned char* indicesStart = triangleVertexArray->getIndicesStart();
|
|
||||||
int32_t vertexStride = triangleVertexArray->getVerticesStride();
|
|
||||||
int32_t indexStride = triangleVertexArray->getIndicesStride();
|
|
||||||
// For each triangle of the concave mesh
|
// For each triangle of the concave mesh
|
||||||
for (uint32_t triangleIndex=0; triangleIndex<triangleVertexArray->getNbTriangles(); triangleIndex++) {
|
for (size_t iii=0; iii<triangleVertexArray->getNbTriangles(); ++iii) {
|
||||||
void* vertexIndexPointer = (indicesStart + triangleIndex * 3 * indexStride);
|
ephysics::Triangle trianglePoints = triangleVertexArray->getTriangle(iii);
|
||||||
vec3 trianglePoints[3];
|
vec3 trianglePoints2[3];
|
||||||
// For each vertex of the triangle
|
trianglePoints2[0] = trianglePoints[0];
|
||||||
for (int32_t k=0; k < 3; k++) {
|
trianglePoints2[1] = trianglePoints[1];
|
||||||
// Get the index of the current vertex in the triangle
|
trianglePoints2[2] = trianglePoints[2];
|
||||||
int32_t vertexIndex = 0;
|
|
||||||
if (indexType == TriangleVertexArray::INDEX_INTEGER_TYPE) {
|
|
||||||
vertexIndex = ((uint32_t*)vertexIndexPointer)[k];
|
|
||||||
} else if (indexType == TriangleVertexArray::INDEX_SHORT_TYPE) {
|
|
||||||
vertexIndex = ((unsigned short*)vertexIndexPointer)[k];
|
|
||||||
} else {
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
// Get the vertices components of the triangle
|
|
||||||
if (vertexType == TriangleVertexArray::VERTEX_FLOAT_TYPE) {
|
|
||||||
const float* vertices = (float*)(verticesStart + vertexIndex * vertexStride);
|
|
||||||
trianglePoints[k][0] = float(vertices[0]) * m_scaling.x();
|
|
||||||
trianglePoints[k][1] = float(vertices[1]) * m_scaling.y();
|
|
||||||
trianglePoints[k][2] = float(vertices[2]) * m_scaling.z();
|
|
||||||
} else if (vertexType == TriangleVertexArray::VERTEX_DOUBLE_TYPE) {
|
|
||||||
const double* vertices = (double*)(verticesStart + vertexIndex * vertexStride);
|
|
||||||
trianglePoints[k][0] = float(vertices[0]) * m_scaling.x();
|
|
||||||
trianglePoints[k][1] = float(vertices[1]) * m_scaling.y();
|
|
||||||
trianglePoints[k][2] = float(vertices[2]) * m_scaling.z();
|
|
||||||
} else {
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Create the AABB for the triangle
|
// Create the AABB for the triangle
|
||||||
AABB aabb = AABB::createAABBForTriangle(trianglePoints);
|
AABB aabb = AABB::createAABBForTriangle(trianglePoints2);
|
||||||
aabb.inflate(m_triangleMargin, m_triangleMargin, m_triangleMargin);
|
aabb.inflate(m_triangleMargin, m_triangleMargin, m_triangleMargin);
|
||||||
// Add the AABB with the index of the triangle int32_to the dynamic AABB tree
|
// Add the AABB with the index of the triangle int32_to the dynamic AABB tree
|
||||||
m_dynamicAABBTree.addObject(aabb, subPart, triangleIndex);
|
m_dynamicAABBTree.addObject(aabb, subPart, iii);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the three vertices coordinates (in the array outTriangleVertices) of a triangle
|
void ConcaveMeshShape::getTriangleVerticesWithIndexPointer(int32_t _subPart, int32_t _triangleIndex, vec3* _outTriangleVertices) const {
|
||||||
// given the start vertex index pointer of the triangle
|
EPHY_ASSERT(_outTriangleVertices != nullptr, "Input check error");
|
||||||
void ConcaveMeshShape::getTriangleVerticesWithIndexPointer(int32_t subPart, int32_t triangleIndex, vec3* outTriangleVertices) const {
|
|
||||||
// Get the triangle vertex array of the current sub-part
|
// Get the triangle vertex array of the current sub-part
|
||||||
TriangleVertexArray* triangleVertexArray = m_triangleMesh->getSubpart(subPart);
|
TriangleVertexArray* triangleVertexArray = m_triangleMesh->getSubpart(_subPart);
|
||||||
if (triangleVertexArray == nullptr) {
|
if (triangleVertexArray == nullptr) {
|
||||||
std::cout << "get nullptr ..." << std::endl;
|
std::cout << "get nullptr ..." << std::endl;
|
||||||
}
|
}
|
||||||
TriangleVertexArray::VertexDataType vertexType = triangleVertexArray->getVertexDataType();
|
ephysics::Triangle trianglePoints = triangleVertexArray->getTriangle(_triangleIndex);
|
||||||
TriangleVertexArray::IndexDataType indexType = triangleVertexArray->getIndexDataType();
|
_outTriangleVertices[0] = trianglePoints[0] * m_scaling;
|
||||||
unsigned char* verticesStart = triangleVertexArray->getVerticesStart();
|
_outTriangleVertices[1] = trianglePoints[1] * m_scaling;
|
||||||
unsigned char* indicesStart = triangleVertexArray->getIndicesStart();
|
_outTriangleVertices[2] = trianglePoints[2] * m_scaling;
|
||||||
int32_t vertexStride = triangleVertexArray->getVerticesStride();
|
|
||||||
int32_t indexStride = triangleVertexArray->getIndicesStride();
|
|
||||||
void* vertexIndexPointer = (indicesStart + triangleIndex * 3 * indexStride);
|
|
||||||
// For each vertex of the triangle
|
|
||||||
for (int32_t k=0; k < 3; k++) {
|
|
||||||
// Get the index of the current vertex in the triangle
|
|
||||||
int32_t vertexIndex = 0;
|
|
||||||
if (indexType == TriangleVertexArray::INDEX_INTEGER_TYPE) {
|
|
||||||
vertexIndex = ((uint32_t*)vertexIndexPointer)[k];
|
|
||||||
} else if (indexType == TriangleVertexArray::INDEX_SHORT_TYPE) {
|
|
||||||
vertexIndex = ((unsigned short*)vertexIndexPointer)[k];
|
|
||||||
} else {
|
|
||||||
std::cout << "wrong type of array : " << int32_t(indexType) << std::endl;
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
// Get the vertices components of the triangle
|
|
||||||
if (vertexType == TriangleVertexArray::VERTEX_FLOAT_TYPE) {
|
|
||||||
const float* vertices = (float*)(verticesStart + vertexIndex * vertexStride);
|
|
||||||
outTriangleVertices[k][0] = float(vertices[0]) * m_scaling.x();
|
|
||||||
outTriangleVertices[k][1] = float(vertices[1]) * m_scaling.y();
|
|
||||||
outTriangleVertices[k][2] = float(vertices[2]) * m_scaling.z();
|
|
||||||
} else if (vertexType == TriangleVertexArray::VERTEX_DOUBLE_TYPE) {
|
|
||||||
const double* vertices = (double*)(verticesStart + vertexIndex * vertexStride);
|
|
||||||
outTriangleVertices[k][0] = float(vertices[0]) * m_scaling.x();
|
|
||||||
outTriangleVertices[k][1] = float(vertices[1]) * m_scaling.y();
|
|
||||||
outTriangleVertices[k][2] = float(vertices[2]) * m_scaling.z();
|
|
||||||
} else {
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use a callback method on all triangles of the concave shape inside a given AABB
|
void ConcaveMeshShape::testAllTriangles(TriangleCallback& _callback, const AABB& _localAABB) const {
|
||||||
void ConcaveMeshShape::testAllTriangles(TriangleCallback& callback, const AABB& localAABB) const {
|
ConvexTriangleAABBOverlapCallback overlapCallback(_callback, *this, m_dynamicAABBTree);
|
||||||
ConvexTriangleAABBOverlapCallback overlapCallback(callback, *this, m_dynamicAABBTree);
|
|
||||||
// Ask the Dynamic AABB Tree to report all the triangles that are overlapping
|
// Ask the Dynamic AABB Tree to report all the triangles that are overlapping
|
||||||
// with the AABB of the convex shape.
|
// with the AABB of the convex shape.
|
||||||
m_dynamicAABBTree.reportAllShapesOverlappingWithAABB(localAABB, overlapCallback);
|
m_dynamicAABBTree.reportAllShapesOverlappingWithAABB(_localAABB, overlapCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Raycast method with feedback information
|
bool ConcaveMeshShape::raycast(const Ray& _ray, RaycastInfo& _raycastInfo, ProxyShape* _proxyShape) const {
|
||||||
/// Note that only the first triangle hit by the ray in the mesh will be returned, even if
|
|
||||||
/// the ray hits many triangles.
|
|
||||||
bool ConcaveMeshShape::raycast(const Ray& ray, RaycastInfo& raycastInfo, ProxyShape* proxyShape) const {
|
|
||||||
PROFILE("ConcaveMeshShape::raycast()");
|
PROFILE("ConcaveMeshShape::raycast()");
|
||||||
// Create the callback object that will compute ray casting against triangles
|
// Create the callback object that will compute ray casting against triangles
|
||||||
ConcaveMeshRaycastCallback raycastCallback(m_dynamicAABBTree, *this, proxyShape, raycastInfo, ray);
|
ConcaveMeshRaycastCallback raycastCallback(m_dynamicAABBTree, *this, _proxyShape, _raycastInfo, _ray);
|
||||||
// Ask the Dynamic AABB Tree to report all AABB nodes that are hit by the ray.
|
// Ask the Dynamic AABB Tree to report all AABB nodes that are hit by the ray.
|
||||||
// The raycastCallback object will then compute ray casting against the triangles
|
// The raycastCallback object will then compute ray casting against the triangles
|
||||||
// in the hit AABBs.
|
// in the hit AABBs.
|
||||||
m_dynamicAABBTree.raycast(ray, raycastCallback);
|
m_dynamicAABBTree.raycast(_ray, raycastCallback);
|
||||||
raycastCallback.raycastTriangles();
|
raycastCallback.raycastTriangles();
|
||||||
return raycastCallback.getIsHit();
|
return raycastCallback.getIsHit();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collect all the AABB nodes that are hit by the ray in the Dynamic AABB Tree
|
float ConcaveMeshRaycastCallback::raycastBroadPhaseShape(int32_t _nodeId, const Ray& _ray) {
|
||||||
float ConcaveMeshRaycastCallback::raycastBroadPhaseShape(int32_t nodeId, const Ray& ray) {
|
|
||||||
// Add the id of the hit AABB node int32_to
|
// Add the id of the hit AABB node int32_to
|
||||||
m_hitAABBNodes.push_back(nodeId);
|
m_hitAABBNodes.push_back(_nodeId);
|
||||||
return ray.maxFraction;
|
return _ray.maxFraction;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Raycast all collision shapes that have been collected
|
|
||||||
void ConcaveMeshRaycastCallback::raycastTriangles() {
|
void ConcaveMeshRaycastCallback::raycastTriangles() {
|
||||||
std::vector<int32_t>::const_iterator it;
|
std::vector<int32_t>::const_iterator it;
|
||||||
float smallestHitFraction = m_ray.maxFraction;
|
float smallestHitFraction = m_ray.maxFraction;
|
||||||
@ -183,46 +110,24 @@ void ConcaveMeshRaycastCallback::raycastTriangles() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the number of bytes used by the collision shape
|
|
||||||
size_t ConcaveMeshShape::getSizeInBytes() const {
|
size_t ConcaveMeshShape::getSizeInBytes() const {
|
||||||
return sizeof(ConcaveMeshShape);
|
return sizeof(ConcaveMeshShape);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the local bounds of the shape in x, y and z directions.
|
void ConcaveMeshShape::getLocalBounds(vec3& _min, vec3& _max) const {
|
||||||
// This method is used to compute the AABB of the box
|
|
||||||
/**
|
|
||||||
* @param min The minimum bounds of the shape in local-space coordinates
|
|
||||||
* @param max The maximum bounds of the shape in local-space coordinates
|
|
||||||
*/
|
|
||||||
void ConcaveMeshShape::getLocalBounds(vec3& min, vec3& max) const {
|
|
||||||
|
|
||||||
// Get the AABB of the whole tree
|
// Get the AABB of the whole tree
|
||||||
AABB treeAABB = m_dynamicAABBTree.getRootAABB();
|
AABB treeAABB = m_dynamicAABBTree.getRootAABB();
|
||||||
|
_min = treeAABB.getMin();
|
||||||
min = treeAABB.getMin();
|
_max = treeAABB.getMax();
|
||||||
max = treeAABB.getMax();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the local scaling vector of the collision shape
|
void ConcaveMeshShape::setLocalScaling(const vec3& _scaling) {
|
||||||
void ConcaveMeshShape::setLocalScaling(const vec3& scaling) {
|
CollisionShape::setLocalScaling(_scaling);
|
||||||
|
|
||||||
CollisionShape::setLocalScaling(scaling);
|
|
||||||
|
|
||||||
// Reset the Dynamic AABB Tree
|
|
||||||
m_dynamicAABBTree.reset();
|
m_dynamicAABBTree.reset();
|
||||||
|
|
||||||
// Rebuild Dynamic AABB Tree here
|
|
||||||
initBVHTree();
|
initBVHTree();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the local inertia tensor of the shape
|
|
||||||
/**
|
|
||||||
* @param[out] tensor The 3x3 inertia tensor matrix of the shape in local-space
|
|
||||||
* coordinates
|
|
||||||
* @param mass Mass to use to compute the inertia tensor of the collision shape
|
|
||||||
*/
|
|
||||||
void ConcaveMeshShape::computeLocalInertiaTensor(etk::Matrix3x3& _tensor, float _mass) const {
|
void ConcaveMeshShape::computeLocalInertiaTensor(etk::Matrix3x3& _tensor, float _mass) const {
|
||||||
|
|
||||||
// Default inertia tensor
|
// Default inertia tensor
|
||||||
// Note that this is not very realistic for a concave triangle mesh.
|
// Note that this is not very realistic for a concave triangle mesh.
|
||||||
// However, in most cases, it will only be used static bodies and therefore,
|
// However, in most cases, it will only be used static bodies and therefore,
|
||||||
@ -232,8 +137,6 @@ void ConcaveMeshShape::computeLocalInertiaTensor(etk::Matrix3x3& _tensor, float
|
|||||||
0, 0, _mass);
|
0, 0, _mass);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called when a overlapping node has been found during the call to
|
|
||||||
// DynamicAABBTree:reportAllShapesOverlappingWithAABB()
|
|
||||||
void ConvexTriangleAABBOverlapCallback::notifyOverlappingNode(int32_t _nodeId) {
|
void ConvexTriangleAABBOverlapCallback::notifyOverlappingNode(int32_t _nodeId) {
|
||||||
|
|
||||||
// Get the node data (triangle index and mesh subpart index)
|
// Get the node data (triangle index and mesh subpart index)
|
||||||
|
@ -79,10 +79,8 @@ namespace ephysics {
|
|||||||
ConcaveMeshShape(const ConcaveMeshShape& _shape) = delete;
|
ConcaveMeshShape(const ConcaveMeshShape& _shape) = delete;
|
||||||
/// Private assignment operator
|
/// Private assignment operator
|
||||||
ConcaveMeshShape& operator=(const ConcaveMeshShape& _shape) = delete;
|
ConcaveMeshShape& operator=(const ConcaveMeshShape& _shape) = delete;
|
||||||
/// Raycast method with feedback information
|
virtual bool raycast(const Ray& _ray, RaycastInfo& _raycastInfo, ProxyShape* _proxyShape) const override;
|
||||||
virtual bool raycast(const Ray& _ray, RaycastInfo& _raycastInfo, ProxyShape* _proxyShape) const;
|
virtual size_t getSizeInBytes() const override;
|
||||||
/// Return the number of bytes used by the collision shape
|
|
||||||
virtual size_t getSizeInBytes() const;
|
|
||||||
/// Insert all the triangles int32_to the dynamic AABB tree
|
/// Insert all the triangles int32_to the dynamic AABB tree
|
||||||
void initBVHTree();
|
void initBVHTree();
|
||||||
/// Return the three vertices coordinates (in the array outTriangleVertices) of a triangle
|
/// Return the three vertices coordinates (in the array outTriangleVertices) of a triangle
|
||||||
@ -93,16 +91,11 @@ namespace ephysics {
|
|||||||
public:
|
public:
|
||||||
/// Constructor
|
/// Constructor
|
||||||
ConcaveMeshShape(TriangleMesh* triangleMesh);
|
ConcaveMeshShape(TriangleMesh* triangleMesh);
|
||||||
/// Destructor
|
virtual void getLocalBounds(vec3& min, vec3& max) const override;
|
||||||
~ConcaveMeshShape();
|
virtual void setLocalScaling(const vec3& scaling) override;
|
||||||
/// Return the local bounds of the shape in x, y and z directions.
|
virtual void computeLocalInertiaTensor(etk::Matrix3x3& tensor, float mass) const override;
|
||||||
virtual void getLocalBounds(vec3& min, vec3& max) const;
|
|
||||||
/// Set the local scaling vector of the collision shape
|
|
||||||
virtual void setLocalScaling(const vec3& scaling);
|
|
||||||
/// Return the local inertia tensor of the collision shape
|
|
||||||
virtual void computeLocalInertiaTensor(etk::Matrix3x3& tensor, float mass) const;
|
|
||||||
/// Use a callback method on all triangles of the concave shape inside a given AABB
|
/// Use a callback method on all triangles of the concave shape inside a given AABB
|
||||||
virtual void testAllTriangles(TriangleCallback& callback, const AABB& localAABB) const;
|
virtual void testAllTriangles(TriangleCallback& callback, const AABB& localAABB) const override;
|
||||||
friend class ConvexTriangleAABBOverlapCallback;
|
friend class ConvexTriangleAABBOverlapCallback;
|
||||||
friend class ConcaveMeshRaycastCallback;
|
friend class ConcaveMeshRaycastCallback;
|
||||||
};
|
};
|
||||||
|
@ -38,76 +38,29 @@ ConvexMeshShape::ConvexMeshShape(const float* arrayVertices, uint32_t nbVertices
|
|||||||
recalculateBounds();
|
recalculateBounds();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructor to initialize with a triangle mesh
|
ConvexMeshShape::ConvexMeshShape(TriangleVertexArray* _triangleVertexArray, bool _isEdgesInformationUsed, float _margin):
|
||||||
/// This method creates an int32_ternal copy of the input vertices.
|
ConvexShape(CONVEX_MESH, _margin),
|
||||||
/**
|
m_minBounds(0, 0, 0),
|
||||||
* @param triangleVertexArray Array with the vertices and indices of the vertices and triangles of the mesh
|
m_maxBounds(0, 0, 0),
|
||||||
* @param isEdgesInformationUsed True if you want to use edges information for collision detection (faster but requires more memory)
|
m_isEdgesInformationUsed(_isEdgesInformationUsed) {
|
||||||
* @param margin Collision margin (in meters) around the collision shape
|
|
||||||
*/
|
|
||||||
ConvexMeshShape::ConvexMeshShape(TriangleVertexArray* triangleVertexArray, bool isEdgesInformationUsed, float margin)
|
|
||||||
: ConvexShape(CONVEX_MESH, margin), m_minBounds(0, 0, 0),
|
|
||||||
m_maxBounds(0, 0, 0), m_isEdgesInformationUsed(isEdgesInformationUsed) {
|
|
||||||
|
|
||||||
TriangleVertexArray::VertexDataType vertexType = triangleVertexArray->getVertexDataType();
|
|
||||||
TriangleVertexArray::IndexDataType indexType = triangleVertexArray->getIndexDataType();
|
|
||||||
unsigned char* verticesStart = triangleVertexArray->getVerticesStart();
|
|
||||||
unsigned char* indicesStart = triangleVertexArray->getIndicesStart();
|
|
||||||
int32_t vertexStride = triangleVertexArray->getVerticesStride();
|
|
||||||
int32_t indexStride = triangleVertexArray->getIndicesStride();
|
|
||||||
|
|
||||||
// For each vertex of the mesh
|
// For each vertex of the mesh
|
||||||
for (uint32_t v = 0; v < triangleVertexArray->getNbVertices(); v++) {
|
for (auto &it: _triangleVertexArray->getVertices()) {
|
||||||
|
m_vertices.push_back(it*m_scaling);
|
||||||
// Get the vertices components of the triangle
|
|
||||||
if (vertexType == TriangleVertexArray::VERTEX_FLOAT_TYPE) {
|
|
||||||
const float* vertices = (float*)(verticesStart + v * vertexStride);
|
|
||||||
|
|
||||||
vec3 vertex(vertices[0], vertices[1], vertices[2] );
|
|
||||||
vertex = vertex * m_scaling;
|
|
||||||
m_vertices.push_back(vertex);
|
|
||||||
}
|
|
||||||
else if (vertexType == TriangleVertexArray::VERTEX_DOUBLE_TYPE) {
|
|
||||||
const double* vertices = (double*)(verticesStart + v * vertexStride);
|
|
||||||
|
|
||||||
vec3 vertex(vertices[0], vertices[1], vertices[2] );
|
|
||||||
vertex = vertex * m_scaling;
|
|
||||||
m_vertices.push_back(vertex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we need to use the edges information of the mesh
|
// If we need to use the edges information of the mesh
|
||||||
if (m_isEdgesInformationUsed) {
|
if (m_isEdgesInformationUsed) {
|
||||||
|
|
||||||
// For each triangle of the mesh
|
// For each triangle of the mesh
|
||||||
for (uint32_t triangleIndex=0; triangleIndex<triangleVertexArray->getNbTriangles(); triangleIndex++) {
|
for (size_t iii=0; iii<_triangleVertexArray->getNbTriangles(); iii++) {
|
||||||
|
|
||||||
void* vertexIndexPointer = (indicesStart + triangleIndex * 3 * indexStride);
|
|
||||||
|
|
||||||
uint32_t vertexIndex[3] = {0, 0, 0};
|
uint32_t vertexIndex[3] = {0, 0, 0};
|
||||||
|
vertexIndex[0] = _triangleVertexArray->getIndices()[iii*3];
|
||||||
// For each vertex of the triangle
|
vertexIndex[1] = _triangleVertexArray->getIndices()[iii*3+1];
|
||||||
for (int32_t k=0; k < 3; k++) {
|
vertexIndex[2] = _triangleVertexArray->getIndices()[iii*3+2];
|
||||||
|
|
||||||
// Get the index of the current vertex in the triangle
|
|
||||||
if (indexType == TriangleVertexArray::INDEX_INTEGER_TYPE) {
|
|
||||||
vertexIndex[k] = ((uint32_t*)vertexIndexPointer)[k];
|
|
||||||
}
|
|
||||||
else if (indexType == TriangleVertexArray::INDEX_SHORT_TYPE) {
|
|
||||||
vertexIndex[k] = ((unsigned short*)vertexIndexPointer)[k];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add information about the edges
|
// Add information about the edges
|
||||||
addEdge(vertexIndex[0], vertexIndex[1]);
|
addEdge(vertexIndex[0], vertexIndex[1]);
|
||||||
addEdge(vertexIndex[0], vertexIndex[2]);
|
addEdge(vertexIndex[0], vertexIndex[2]);
|
||||||
addEdge(vertexIndex[1], vertexIndex[2]);
|
addEdge(vertexIndex[1], vertexIndex[2]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_numberVertices = m_vertices.size();
|
m_numberVertices = m_vertices.size();
|
||||||
recalculateBounds();
|
recalculateBounds();
|
||||||
}
|
}
|
||||||
@ -115,9 +68,12 @@ ConvexMeshShape::ConvexMeshShape(TriangleVertexArray* triangleVertexArray, bool
|
|||||||
// Constructor.
|
// Constructor.
|
||||||
/// If you use this constructor, you will need to set the vertices manually one by one using
|
/// If you use this constructor, you will need to set the vertices manually one by one using
|
||||||
/// the addVertex() method.
|
/// the addVertex() method.
|
||||||
ConvexMeshShape::ConvexMeshShape(float margin)
|
ConvexMeshShape::ConvexMeshShape(float _margin):
|
||||||
: ConvexShape(CONVEX_MESH, margin), m_numberVertices(0), m_minBounds(0, 0, 0),
|
ConvexShape(CONVEX_MESH, _margin),
|
||||||
m_maxBounds(0, 0, 0), m_isEdgesInformationUsed(false) {
|
m_numberVertices(0),
|
||||||
|
m_minBounds(0, 0, 0),
|
||||||
|
m_maxBounds(0, 0, 0),
|
||||||
|
m_isEdgesInformationUsed(false) {
|
||||||
|
|
||||||
}
|
}
|
||||||
// Return a local support point in a given direction without the object margin.
|
// Return a local support point in a given direction without the object margin.
|
||||||
|
@ -56,7 +56,13 @@ namespace ephysics {
|
|||||||
uint32_t _nbVertices,
|
uint32_t _nbVertices,
|
||||||
int32_t _stride,
|
int32_t _stride,
|
||||||
float _margin = OBJECT_MARGIN);
|
float _margin = OBJECT_MARGIN);
|
||||||
/// Constructor to initialize with a triangle vertex array
|
/**
|
||||||
|
* @brief Constructor to initialize with a triangle mesh
|
||||||
|
* This method creates an internal copy of the input vertices.
|
||||||
|
* @param _triangleVertexArray Array with the vertices and indices of the vertices and triangles of the mesh
|
||||||
|
* @param _isEdgesInformationUsed True if you want to use edges information for collision detection (faster but requires more memory)
|
||||||
|
* @param _margin Collision margin (in meters) around the collision shape
|
||||||
|
*/
|
||||||
ConvexMeshShape(TriangleVertexArray* _triangleVertexArray,
|
ConvexMeshShape(TriangleVertexArray* _triangleVertexArray,
|
||||||
bool _isEdgesInformationUsed = true,
|
bool _isEdgesInformationUsed = true,
|
||||||
float _margin = OBJECT_MARGIN);
|
float _margin = OBJECT_MARGIN);
|
||||||
|
@ -111,23 +111,27 @@ namespace ephysics {
|
|||||||
ConstraintSolverData m_constraintSolverData; //!< Constraint solver data used to initialize and solve the constraints
|
ConstraintSolverData m_constraintSolverData; //!< Constraint solver data used to initialize and solve the constraints
|
||||||
public :
|
public :
|
||||||
/// Constructor
|
/// Constructor
|
||||||
ConstraintSolver(const std::map<RigidBody*, uint32_t>& mapBodyToVelocityIndex);
|
ConstraintSolver(const std::map<RigidBody*, uint32_t>& _mapBodyToVelocityIndex);
|
||||||
/// Initialize the constraint solver for a given island
|
/// Initialize the constraint solver for a given island
|
||||||
void initializeForIsland(float dt, Island* island);
|
void initializeForIsland(float _dt, Island* _island);
|
||||||
/// Solve the constraints
|
/// Solve the constraints
|
||||||
void solveVelocityConstraints(Island* island);
|
void solveVelocityConstraints(Island* _island);
|
||||||
/// Solve the position constraints
|
/// Solve the position constraints
|
||||||
void solvePositionConstraints(Island* island);
|
void solvePositionConstraints(Island* _island);
|
||||||
/// Return true if the Non-Linear-Gauss-Seidel position correction technique is active
|
/// Return true if the Non-Linear-Gauss-Seidel position correction technique is active
|
||||||
bool getIsNonLinearGaussSeidelPositionCorrectionActive() const;
|
bool getIsNonLinearGaussSeidelPositionCorrectionActive() const {
|
||||||
|
return m_isWarmStartingActive;
|
||||||
|
}
|
||||||
/// Enable/Disable the Non-Linear-Gauss-Seidel position correction technique.
|
/// Enable/Disable the Non-Linear-Gauss-Seidel position correction technique.
|
||||||
void setIsNonLinearGaussSeidelPositionCorrectionActive(bool isActive);
|
void setIsNonLinearGaussSeidelPositionCorrectionActive(bool _isActive) {
|
||||||
|
m_isWarmStartingActive = _isActive;
|
||||||
|
}
|
||||||
/// Set the constrained velocities arrays
|
/// Set the constrained velocities arrays
|
||||||
void setConstrainedVelocitiesArrays(vec3* constrainedLinearVelocities,
|
void setConstrainedVelocitiesArrays(vec3* _constrainedLinearVelocities,
|
||||||
vec3* constrainedAngularVelocities);
|
vec3* _constrainedAngularVelocities);
|
||||||
/// Set the constrained positions/orientations arrays
|
/// Set the constrained positions/orientations arrays
|
||||||
void setConstrainedPositionsArrays(vec3* constrainedPositions,
|
void setConstrainedPositionsArrays(vec3* _constrainedPositions,
|
||||||
etk::Quaternion* constrainedOrientations);
|
etk::Quaternion* _constrainedOrientations);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user