2017-08-01 14:30:58 +02:00
|
|
|
#include "btInternalEdgeUtility.h"
|
|
|
|
|
|
|
|
#include "BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h"
|
2020-01-08 18:05:43 +01:00
|
|
|
#include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
#include "BulletCollision/CollisionShapes/btScaledBvhTriangleMeshShape.h"
|
|
|
|
#include "BulletCollision/CollisionShapes/btTriangleShape.h"
|
|
|
|
#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
|
|
|
|
#include "BulletCollision/NarrowPhaseCollision/btManifoldPoint.h"
|
|
|
|
#include "LinearMath/btIDebugDraw.h"
|
|
|
|
#include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
|
|
|
|
|
|
|
|
//#define DEBUG_INTERNAL_EDGE
|
|
|
|
|
|
|
|
#ifdef DEBUG_INTERNAL_EDGE
|
|
|
|
#include <stdio.h>
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //DEBUG_INTERNAL_EDGE
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
|
|
|
static btIDebugDraw* gDebugDrawer = 0;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btSetDebugDrawer(btIDebugDraw* debugDrawer)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
gDebugDrawer = debugDrawer;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
static void btDebugDrawLine(const btVector3& from, const btVector3& to, const btVector3& color)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
if (gDebugDrawer)
|
2019-01-03 14:26:51 +01:00
|
|
|
gDebugDrawer->drawLine(from, to, color);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //BT_INTERNAL_EDGE_DEBUG_DRAW
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
static int btGetHash(int partId, int triangleIndex)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
int hash = (partId << (31 - MAX_NUM_PARTS_IN_BITS)) | triangleIndex;
|
2017-08-01 14:30:58 +02:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
static btScalar btGetAngle(const btVector3& edgeA, const btVector3& normalA, const btVector3& normalB)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
const btVector3 refAxis0 = edgeA;
|
|
|
|
const btVector3 refAxis1 = normalA;
|
2017-08-01 14:30:58 +02:00
|
|
|
const btVector3 swingAxis = normalB;
|
|
|
|
btScalar angle = btAtan2(swingAxis.dot(refAxis0), swingAxis.dot(refAxis1));
|
2019-01-03 14:26:51 +01:00
|
|
|
return angle;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct btConnectivityProcessor : public btTriangleCallback
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
int m_partIdA;
|
|
|
|
int m_triangleIndexA;
|
|
|
|
btVector3* m_triangleVerticesA;
|
|
|
|
btTriangleInfoMap* m_triangleInfoMap;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex)
|
|
|
|
{
|
|
|
|
//skip self-collisions
|
|
|
|
if ((m_partIdA == partId) && (m_triangleIndexA == triangleIndex))
|
|
|
|
return;
|
|
|
|
|
|
|
|
//skip duplicates (disabled for now)
|
|
|
|
//if ((m_partIdA <= partId) && (m_triangleIndexA <= triangleIndex))
|
|
|
|
// return;
|
|
|
|
|
|
|
|
//search for shared vertices and edges
|
|
|
|
int numshared = 0;
|
2019-01-03 14:26:51 +01:00
|
|
|
int sharedVertsA[3] = {-1, -1, -1};
|
|
|
|
int sharedVertsB[3] = {-1, -1, -1};
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
///skip degenerate triangles
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar crossBSqr = ((triangle[1] - triangle[0]).cross(triangle[2] - triangle[0])).length2();
|
2017-08-01 14:30:58 +02:00
|
|
|
if (crossBSqr < m_triangleInfoMap->m_equalVertexThreshold)
|
|
|
|
return;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar crossASqr = ((m_triangleVerticesA[1] - m_triangleVerticesA[0]).cross(m_triangleVerticesA[2] - m_triangleVerticesA[0])).length2();
|
2017-08-01 14:30:58 +02:00
|
|
|
///skip degenerate triangles
|
2019-01-03 14:26:51 +01:00
|
|
|
if (crossASqr < m_triangleInfoMap->m_equalVertexThreshold)
|
2017-08-01 14:30:58 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf("triangle A[0] = (%f,%f,%f)\ntriangle A[1] = (%f,%f,%f)\ntriangle A[2] = (%f,%f,%f)\n",
|
|
|
|
m_triangleVerticesA[0].getX(),m_triangleVerticesA[0].getY(),m_triangleVerticesA[0].getZ(),
|
|
|
|
m_triangleVerticesA[1].getX(),m_triangleVerticesA[1].getY(),m_triangleVerticesA[1].getZ(),
|
|
|
|
m_triangleVerticesA[2].getX(),m_triangleVerticesA[2].getY(),m_triangleVerticesA[2].getZ());
|
|
|
|
|
|
|
|
printf("partId=%d, triangleIndex=%d\n",partId,triangleIndex);
|
|
|
|
printf("triangle B[0] = (%f,%f,%f)\ntriangle B[1] = (%f,%f,%f)\ntriangle B[2] = (%f,%f,%f)\n",
|
|
|
|
triangle[0].getX(),triangle[0].getY(),triangle[0].getZ(),
|
|
|
|
triangle[1].getX(),triangle[1].getY(),triangle[1].getZ(),
|
|
|
|
triangle[2].getX(),triangle[2].getY(),triangle[2].getZ());
|
|
|
|
#endif
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < 3; i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int j = 0; j < 3; j++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if ((m_triangleVerticesA[i] - triangle[j]).length2() < m_triangleInfoMap->m_equalVertexThreshold)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
sharedVertsA[numshared] = i;
|
|
|
|
sharedVertsB[numshared] = j;
|
|
|
|
numshared++;
|
|
|
|
///degenerate case
|
2019-01-03 14:26:51 +01:00
|
|
|
if (numshared >= 3)
|
2017-08-01 14:30:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
///degenerate case
|
2019-01-03 14:26:51 +01:00
|
|
|
if (numshared >= 3)
|
2017-08-01 14:30:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (numshared)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
case 0:
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
case 1:
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
//shared vertex
|
|
|
|
break;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
case 2:
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
//shared edge
|
|
|
|
//we need to make sure the edge is in the order V2V0 and not V0V2 so that the signs are correct
|
|
|
|
if (sharedVertsA[0] == 0 && sharedVertsA[1] == 2)
|
|
|
|
{
|
|
|
|
sharedVertsA[0] = 2;
|
|
|
|
sharedVertsA[1] = 0;
|
|
|
|
int tmp = sharedVertsB[1];
|
|
|
|
sharedVertsB[1] = sharedVertsB[0];
|
|
|
|
sharedVertsB[0] = tmp;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int hash = btGetHash(m_partIdA, m_triangleIndexA);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
btTriangleInfo* info = m_triangleInfoMap->find(hash);
|
|
|
|
if (!info)
|
|
|
|
{
|
|
|
|
btTriangleInfo tmp;
|
2019-01-03 14:26:51 +01:00
|
|
|
m_triangleInfoMap->insert(hash, tmp);
|
2017-08-01 14:30:58 +02:00
|
|
|
info = m_triangleInfoMap->find(hash);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int sumvertsA = sharedVertsA[0] + sharedVertsA[1];
|
|
|
|
int otherIndexA = 3 - sumvertsA;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 edge(m_triangleVerticesA[sharedVertsA[1]] - m_triangleVerticesA[sharedVertsA[0]]);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btTriangleShape tA(m_triangleVerticesA[0], m_triangleVerticesA[1], m_triangleVerticesA[2]);
|
|
|
|
int otherIndexB = 3 - (sharedVertsB[0] + sharedVertsB[1]);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btTriangleShape tB(triangle[sharedVertsB[1]], triangle[sharedVertsB[0]], triangle[otherIndexB]);
|
2017-08-01 14:30:58 +02:00
|
|
|
//btTriangleShape tB(triangle[0],triangle[1],triangle[2]);
|
|
|
|
|
|
|
|
btVector3 normalA;
|
|
|
|
btVector3 normalB;
|
|
|
|
tA.calcNormal(normalA);
|
|
|
|
tB.calcNormal(normalB);
|
|
|
|
edge.normalize();
|
|
|
|
btVector3 edgeCrossA = edge.cross(normalA).normalize();
|
|
|
|
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 tmp = m_triangleVerticesA[otherIndexA] - m_triangleVerticesA[sharedVertsA[0]];
|
2017-08-01 14:30:58 +02:00
|
|
|
if (edgeCrossA.dot(tmp) < 0)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
edgeCrossA *= -1;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
btVector3 edgeCrossB = edge.cross(normalB).normalize();
|
|
|
|
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 tmp = triangle[otherIndexB] - triangle[sharedVertsB[0]];
|
2017-08-01 14:30:58 +02:00
|
|
|
if (edgeCrossB.dot(tmp) < 0)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
edgeCrossB *= -1;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar angle2 = 0;
|
|
|
|
btScalar ang4 = 0.f;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
btVector3 calculatedEdge = edgeCrossA.cross(edgeCrossB);
|
|
|
|
btScalar len2 = calculatedEdge.length2();
|
|
|
|
|
|
|
|
btScalar correctedAngle(0);
|
|
|
|
//btVector3 calculatedNormalB = normalA;
|
|
|
|
bool isConvex = false;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (len2 < m_triangleInfoMap->m_planarEpsilon)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
angle2 = 0.f;
|
|
|
|
ang4 = 0.f;
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
calculatedEdge.normalize();
|
|
|
|
btVector3 calculatedNormalA = calculatedEdge.cross(edgeCrossA);
|
|
|
|
calculatedNormalA.normalize();
|
2019-01-03 14:26:51 +01:00
|
|
|
angle2 = btGetAngle(calculatedNormalA, edgeCrossA, edgeCrossB);
|
|
|
|
ang4 = SIMD_PI - angle2;
|
2017-08-01 14:30:58 +02:00
|
|
|
btScalar dotA = normalA.dot(edgeCrossB);
|
|
|
|
///@todo: check if we need some epsilon, due to floating point imprecision
|
2019-01-03 14:26:51 +01:00
|
|
|
isConvex = (dotA < 0.);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
correctedAngle = isConvex ? ang4 : -ang4;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
//alternatively use
|
2017-08-01 14:30:58 +02:00
|
|
|
//btVector3 calculatedNormalB2 = quatRotate(orn,normalA);
|
|
|
|
|
|
|
|
switch (sumvertsA)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
case 1:
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 edge = m_triangleVerticesA[0] - m_triangleVerticesA[1];
|
|
|
|
btQuaternion orn(edge, -correctedAngle);
|
|
|
|
btVector3 computedNormalB = quatRotate(orn, normalA);
|
2017-08-01 14:30:58 +02:00
|
|
|
btScalar bla = computedNormalB.dot(normalB);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (bla < 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
computedNormalB *= -1;
|
2017-08-01 14:30:58 +02:00
|
|
|
info->m_flags |= TRI_INFO_V0V1_SWAP_NORMALB;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_INTERNAL_EDGE
|
2019-01-03 14:26:51 +01:00
|
|
|
if ((computedNormalB - normalB).length() > 0.0001)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
printf("warning: normals not identical\n");
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //DEBUG_INTERNAL_EDGE
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
info->m_edgeV0V1Angle = -correctedAngle;
|
|
|
|
|
|
|
|
if (isConvex)
|
|
|
|
info->m_flags |= TRI_INFO_V0V1_CONVEX;
|
|
|
|
break;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
case 2:
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 edge = m_triangleVerticesA[2] - m_triangleVerticesA[0];
|
|
|
|
btQuaternion orn(edge, -correctedAngle);
|
|
|
|
btVector3 computedNormalB = quatRotate(orn, normalA);
|
|
|
|
if (computedNormalB.dot(normalB) < 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
computedNormalB *= -1;
|
2017-08-01 14:30:58 +02:00
|
|
|
info->m_flags |= TRI_INFO_V2V0_SWAP_NORMALB;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_INTERNAL_EDGE
|
2019-01-03 14:26:51 +01:00
|
|
|
if ((computedNormalB - normalB).length() > 0.0001)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
printf("warning: normals not identical\n");
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //DEBUG_INTERNAL_EDGE
|
2017-08-01 14:30:58 +02:00
|
|
|
info->m_edgeV2V0Angle = -correctedAngle;
|
|
|
|
if (isConvex)
|
|
|
|
info->m_flags |= TRI_INFO_V2V0_CONVEX;
|
2019-01-03 14:26:51 +01:00
|
|
|
break;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
case 3:
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 edge = m_triangleVerticesA[1] - m_triangleVerticesA[2];
|
|
|
|
btQuaternion orn(edge, -correctedAngle);
|
|
|
|
btVector3 computedNormalB = quatRotate(orn, normalA);
|
|
|
|
if (computedNormalB.dot(normalB) < 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
info->m_flags |= TRI_INFO_V1V2_SWAP_NORMALB;
|
2019-01-03 14:26:51 +01:00
|
|
|
computedNormalB *= -1;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
#ifdef DEBUG_INTERNAL_EDGE
|
2019-01-03 14:26:51 +01:00
|
|
|
if ((computedNormalB - normalB).length() > 0.0001)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
printf("warning: normals not identical\n");
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //DEBUG_INTERNAL_EDGE
|
2017-08-01 14:30:58 +02:00
|
|
|
info->m_edgeV1V2Angle = -correctedAngle;
|
|
|
|
|
|
|
|
if (isConvex)
|
|
|
|
info->m_flags |= TRI_INFO_V1V2_CONVEX;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
default:
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
// printf("warning: duplicate triangle\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-01-08 18:05:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
struct b3ProcessAllTrianglesHeightfield: public btTriangleCallback
|
|
|
|
{
|
|
|
|
btHeightfieldTerrainShape* m_heightfieldShape;
|
|
|
|
btTriangleInfoMap* m_triangleInfoMap;
|
|
|
|
|
|
|
|
|
|
|
|
b3ProcessAllTrianglesHeightfield(btHeightfieldTerrainShape* heightFieldShape, btTriangleInfoMap* triangleInfoMap)
|
|
|
|
:m_heightfieldShape(heightFieldShape),
|
|
|
|
m_triangleInfoMap(triangleInfoMap)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex)
|
|
|
|
{
|
|
|
|
btConnectivityProcessor connectivityProcessor;
|
|
|
|
connectivityProcessor.m_partIdA = partId;
|
|
|
|
connectivityProcessor.m_triangleIndexA = triangleIndex;
|
|
|
|
connectivityProcessor.m_triangleVerticesA = triangle;
|
|
|
|
connectivityProcessor.m_triangleInfoMap = m_triangleInfoMap;
|
|
|
|
btVector3 aabbMin, aabbMax;
|
|
|
|
aabbMin.setValue(btScalar(BT_LARGE_FLOAT), btScalar(BT_LARGE_FLOAT), btScalar(BT_LARGE_FLOAT));
|
|
|
|
aabbMax.setValue(btScalar(-BT_LARGE_FLOAT), btScalar(-BT_LARGE_FLOAT), btScalar(-BT_LARGE_FLOAT));
|
|
|
|
aabbMin.setMin(triangle[0]);
|
|
|
|
aabbMax.setMax(triangle[0]);
|
|
|
|
aabbMin.setMin(triangle[1]);
|
|
|
|
aabbMax.setMax(triangle[1]);
|
|
|
|
aabbMin.setMin(triangle[2]);
|
|
|
|
aabbMax.setMax(triangle[2]);
|
|
|
|
|
|
|
|
m_heightfieldShape->processAllTriangles(&connectivityProcessor, aabbMin, aabbMax);
|
|
|
|
}
|
|
|
|
};
|
2017-08-01 14:30:58 +02:00
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btGenerateInternalEdgeInfo(btBvhTriangleMeshShape* trimeshShape, btTriangleInfoMap* triangleInfoMap)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
//the user pointer shouldn't already be used for other purposes, we intend to store connectivity info there!
|
|
|
|
if (trimeshShape->getTriangleInfoMap())
|
|
|
|
return;
|
|
|
|
|
|
|
|
trimeshShape->setTriangleInfoMap(triangleInfoMap);
|
|
|
|
|
|
|
|
btStridingMeshInterface* meshInterface = trimeshShape->getMeshInterface();
|
|
|
|
const btVector3& meshScaling = meshInterface->getScaling();
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int partId = 0; partId < meshInterface->getNumSubParts(); partId++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
const unsigned char* vertexbase = 0;
|
2017-08-01 14:30:58 +02:00
|
|
|
int numverts = 0;
|
|
|
|
PHY_ScalarType type = PHY_INTEGER;
|
|
|
|
int stride = 0;
|
2019-01-03 14:26:51 +01:00
|
|
|
const unsigned char* indexbase = 0;
|
2017-08-01 14:30:58 +02:00
|
|
|
int indexstride = 0;
|
|
|
|
int numfaces = 0;
|
|
|
|
PHY_ScalarType indicestype = PHY_INTEGER;
|
|
|
|
//PHY_ScalarType indexType=0;
|
|
|
|
|
|
|
|
btVector3 triangleVerts[3];
|
2019-01-03 14:26:51 +01:00
|
|
|
meshInterface->getLockedReadOnlyVertexIndexBase(&vertexbase, numverts, type, stride, &indexbase, indexstride, numfaces, indicestype, partId);
|
|
|
|
btVector3 aabbMin, aabbMax;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int triangleIndex = 0; triangleIndex < numfaces; triangleIndex++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
unsigned int* gfxbase = (unsigned int*)(indexbase + triangleIndex * indexstride);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int j = 2; j >= 0; j--)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2021-09-29 15:47:08 +02:00
|
|
|
int graphicsindex;
|
|
|
|
switch (indicestype) {
|
|
|
|
case PHY_INTEGER: graphicsindex = gfxbase[j]; break;
|
|
|
|
case PHY_SHORT: graphicsindex = ((unsigned short*)gfxbase)[j]; break;
|
|
|
|
case PHY_UCHAR: graphicsindex = ((unsigned char*)gfxbase)[j]; break;
|
|
|
|
default: btAssert(0);
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
if (type == PHY_FLOAT)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
float* graphicsbase = (float*)(vertexbase + graphicsindex * stride);
|
2017-08-01 14:30:58 +02:00
|
|
|
triangleVerts[j] = btVector3(
|
2019-01-03 14:26:51 +01:00
|
|
|
graphicsbase[0] * meshScaling.getX(),
|
|
|
|
graphicsbase[1] * meshScaling.getY(),
|
|
|
|
graphicsbase[2] * meshScaling.getZ());
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
double* graphicsbase = (double*)(vertexbase + graphicsindex * stride);
|
|
|
|
triangleVerts[j] = btVector3(btScalar(graphicsbase[0] * meshScaling.getX()), btScalar(graphicsbase[1] * meshScaling.getY()), btScalar(graphicsbase[2] * meshScaling.getZ()));
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
aabbMin.setValue(btScalar(BT_LARGE_FLOAT), btScalar(BT_LARGE_FLOAT), btScalar(BT_LARGE_FLOAT));
|
|
|
|
aabbMax.setValue(btScalar(-BT_LARGE_FLOAT), btScalar(-BT_LARGE_FLOAT), btScalar(-BT_LARGE_FLOAT));
|
2017-08-01 14:30:58 +02:00
|
|
|
aabbMin.setMin(triangleVerts[0]);
|
|
|
|
aabbMax.setMax(triangleVerts[0]);
|
|
|
|
aabbMin.setMin(triangleVerts[1]);
|
|
|
|
aabbMax.setMax(triangleVerts[1]);
|
|
|
|
aabbMin.setMin(triangleVerts[2]);
|
|
|
|
aabbMax.setMax(triangleVerts[2]);
|
|
|
|
|
|
|
|
btConnectivityProcessor connectivityProcessor;
|
|
|
|
connectivityProcessor.m_partIdA = partId;
|
|
|
|
connectivityProcessor.m_triangleIndexA = triangleIndex;
|
|
|
|
connectivityProcessor.m_triangleVerticesA = &triangleVerts[0];
|
2019-01-03 14:26:51 +01:00
|
|
|
connectivityProcessor.m_triangleInfoMap = triangleInfoMap;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
trimeshShape->processAllTriangles(&connectivityProcessor, aabbMin, aabbMax);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 18:05:43 +01:00
|
|
|
|
|
|
|
void btGenerateInternalEdgeInfo(btHeightfieldTerrainShape* heightfieldShape, btTriangleInfoMap* triangleInfoMap)
|
|
|
|
{
|
|
|
|
|
|
|
|
//the user pointer shouldn't already be used for other purposes, we intend to store connectivity info there!
|
|
|
|
if (heightfieldShape->getTriangleInfoMap())
|
|
|
|
return;
|
|
|
|
|
|
|
|
heightfieldShape->setTriangleInfoMap(triangleInfoMap);
|
|
|
|
|
|
|
|
//get all the triangles of the heightfield
|
|
|
|
|
|
|
|
btVector3 aabbMin, aabbMax;
|
|
|
|
|
|
|
|
aabbMax.setValue(btScalar(BT_LARGE_FLOAT), btScalar(BT_LARGE_FLOAT), btScalar(BT_LARGE_FLOAT));
|
|
|
|
aabbMin.setValue(btScalar(-BT_LARGE_FLOAT), btScalar(-BT_LARGE_FLOAT), btScalar(-BT_LARGE_FLOAT));
|
|
|
|
|
|
|
|
b3ProcessAllTrianglesHeightfield processHeightfield(heightfieldShape, triangleInfoMap);
|
|
|
|
heightfieldShape->processAllTriangles(&processHeightfield, aabbMin, aabbMax);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
// Given a point and a line segment (defined by two points), compute the closest point
|
|
|
|
// in the line. Cap the point at the endpoints of the line segment.
|
2019-01-03 14:26:51 +01:00
|
|
|
void btNearestPointInLineSegment(const btVector3& point, const btVector3& line0, const btVector3& line1, btVector3& nearestPoint)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 lineDelta = line1 - line0;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
// Handle degenerate lines
|
2019-01-03 14:26:51 +01:00
|
|
|
if (lineDelta.fuzzyZero())
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
nearestPoint = line0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar delta = (point - line0).dot(lineDelta) / (lineDelta).dot(lineDelta);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
// Clamp the point to conform to the segment's endpoints
|
2019-01-03 14:26:51 +01:00
|
|
|
if (delta < 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
delta = 0;
|
2019-01-03 14:26:51 +01:00
|
|
|
else if (delta > 1)
|
2017-08-01 14:30:58 +02:00
|
|
|
delta = 1;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
nearestPoint = line0 + lineDelta * delta;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
bool btClampNormal(const btVector3& edge, const btVector3& tri_normal_org, const btVector3& localContactNormalOnB, btScalar correctedEdgeAngle, btVector3& clampedLocalNormal)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btVector3 tri_normal = tri_normal_org;
|
|
|
|
//we only have a local triangle normal, not a local contact normal -> only normal in world space...
|
|
|
|
//either compute the current angle all in local space, or all in world space
|
|
|
|
|
|
|
|
btVector3 edgeCross = edge.cross(tri_normal).normalize();
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar curAngle = btGetAngle(edgeCross, tri_normal, localContactNormalOnB);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (correctedEdgeAngle < 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
if (curAngle < correctedEdgeAngle)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar diffAngle = correctedEdgeAngle - curAngle;
|
|
|
|
btQuaternion rotation(edge, diffAngle);
|
|
|
|
clampedLocalNormal = btMatrix3x3(rotation) * localContactNormalOnB;
|
2017-08-01 14:30:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (correctedEdgeAngle >= 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
if (curAngle > correctedEdgeAngle)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar diffAngle = correctedEdgeAngle - curAngle;
|
|
|
|
btQuaternion rotation(edge, diffAngle);
|
|
|
|
clampedLocalNormal = btMatrix3x3(rotation) * localContactNormalOnB;
|
2017-08-01 14:30:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Changes a btManifoldPoint collision normal to the normal from the mesh.
|
2019-01-03 14:26:51 +01:00
|
|
|
void btAdjustInternalEdgeContacts(btManifoldPoint& cp, const btCollisionObjectWrapper* colObj0Wrap, const btCollisionObjectWrapper* colObj1Wrap, int partId0, int index0, int normalAdjustFlags)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
//btAssert(colObj0->getCollisionShape()->getShapeType() == TRIANGLE_SHAPE_PROXYTYPE);
|
|
|
|
if (colObj0Wrap->getCollisionShape()->getShapeType() != TRIANGLE_SHAPE_PROXYTYPE)
|
|
|
|
return;
|
|
|
|
|
2020-01-08 18:05:43 +01:00
|
|
|
|
|
|
|
btTriangleInfoMap* triangleInfoMapPtr = 0;
|
|
|
|
|
|
|
|
if (colObj0Wrap->getCollisionObject()->getCollisionShape()->getShapeType() == TERRAIN_SHAPE_PROXYTYPE)
|
|
|
|
{
|
|
|
|
btHeightfieldTerrainShape* heightfield = (btHeightfieldTerrainShape*)colObj0Wrap->getCollisionObject()->getCollisionShape();
|
|
|
|
triangleInfoMapPtr = heightfield->getTriangleInfoMap();
|
|
|
|
|
|
|
|
//#define USE_HEIGHTFIELD_TRIANGLES
|
|
|
|
#ifdef USE_HEIGHTFIELD_TRIANGLES
|
|
|
|
btVector3 newNormal = btVector3(0, 0, 1);
|
|
|
|
|
|
|
|
const btTriangleShape* tri_shape = static_cast<const btTriangleShape*>(colObj0Wrap->getCollisionShape());
|
|
|
|
btVector3 tri_normal;
|
|
|
|
tri_shape->calcNormal(tri_normal);
|
|
|
|
newNormal = tri_normal;
|
|
|
|
// cp.m_distance1 = cp.m_distance1 * newNormal.dot(cp.m_normalWorldOnB);
|
|
|
|
cp.m_normalWorldOnB = newNormal;
|
|
|
|
// Reproject collision point along normal. (what about cp.m_distance1?)
|
|
|
|
cp.m_positionWorldOnB = cp.m_positionWorldOnA - cp.m_normalWorldOnB * cp.m_distance1;
|
|
|
|
cp.m_localPointB = colObj0Wrap->getWorldTransform().invXform(cp.m_positionWorldOnB);
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
btBvhTriangleMeshShape* trimesh = 0;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
if (colObj0Wrap->getCollisionObject()->getCollisionShape()->getShapeType() == SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE)
|
2018-09-07 16:11:04 +02:00
|
|
|
{
|
|
|
|
trimesh = ((btScaledBvhTriangleMeshShape*)colObj0Wrap->getCollisionObject()->getCollisionShape())->getChildShape();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (colObj0Wrap->getCollisionObject()->getCollisionShape()->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
|
2018-09-07 16:11:04 +02:00
|
|
|
{
|
|
|
|
trimesh = (btBvhTriangleMeshShape*)colObj0Wrap->getCollisionObject()->getCollisionShape();
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 18:05:43 +01:00
|
|
|
if (trimesh)
|
|
|
|
{
|
|
|
|
triangleInfoMapPtr = (btTriangleInfoMap*)trimesh->getTriangleInfoMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
if (!triangleInfoMapPtr)
|
|
|
|
return;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int hash = btGetHash(partId0, index0);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
btTriangleInfo* info = triangleInfoMapPtr->find(hash);
|
|
|
|
if (!info)
|
|
|
|
return;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar frontFacing = (normalAdjustFlags & BT_TRIANGLE_CONVEX_BACKFACE_MODE) == 0 ? 1.f : -1.f;
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
const btTriangleShape* tri_shape = static_cast<const btTriangleShape*>(colObj0Wrap->getCollisionShape());
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 v0, v1, v2;
|
|
|
|
tri_shape->getVertex(0, v0);
|
|
|
|
tri_shape->getVertex(1, v1);
|
|
|
|
tri_shape->getVertex(2, v2);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
//btVector3 center = (v0+v1+v2)*btScalar(1./3.);
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 red(1, 0, 0), green(0, 1, 0), blue(0, 0, 1), white(1, 1, 1), black(0, 0, 0);
|
2017-08-01 14:30:58 +02:00
|
|
|
btVector3 tri_normal;
|
|
|
|
tri_shape->calcNormal(tri_normal);
|
|
|
|
|
|
|
|
//btScalar dot = tri_normal.dot(cp.m_normalWorldOnB);
|
|
|
|
btVector3 nearest;
|
2019-01-03 14:26:51 +01:00
|
|
|
btNearestPointInLineSegment(cp.m_localPointB, v0, v1, nearest);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
btVector3 contact = cp.m_localPointB;
|
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
|
|
|
const btTransform& tr = colObj0->getWorldTransform();
|
2019-01-03 14:26:51 +01:00
|
|
|
btDebugDrawLine(tr * nearest, tr * cp.m_localPointB, red);
|
|
|
|
#endif //BT_INTERNAL_EDGE_DEBUG_DRAW
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
bool isNearEdge = false;
|
|
|
|
|
|
|
|
int numConcaveEdgeHits = 0;
|
|
|
|
int numConvexEdgeHits = 0;
|
|
|
|
|
|
|
|
btVector3 localContactNormalOnB = colObj0Wrap->getWorldTransform().getBasis().transpose() * cp.m_normalWorldOnB;
|
2019-01-03 14:26:51 +01:00
|
|
|
localContactNormalOnB.normalize(); //is this necessary?
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
// Get closest edge
|
2019-01-03 14:26:51 +01:00
|
|
|
int bestedge = -1;
|
|
|
|
btScalar disttobestedge = BT_LARGE_FLOAT;
|
2017-08-01 14:30:58 +02:00
|
|
|
//
|
|
|
|
// Edge 0 -> 1
|
2019-01-03 14:26:51 +01:00
|
|
|
if (btFabs(info->m_edgeV0V1Angle) < triangleInfoMapPtr->m_maxEdgeAngleThreshold)
|
|
|
|
{
|
|
|
|
btVector3 nearest;
|
|
|
|
btNearestPointInLineSegment(cp.m_localPointB, v0, v1, nearest);
|
|
|
|
btScalar len = (contact - nearest).length();
|
|
|
|
//
|
|
|
|
if (len < disttobestedge)
|
|
|
|
{
|
|
|
|
bestedge = 0;
|
|
|
|
disttobestedge = len;
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
// Edge 1 -> 2
|
2019-01-03 14:26:51 +01:00
|
|
|
if (btFabs(info->m_edgeV1V2Angle) < triangleInfoMapPtr->m_maxEdgeAngleThreshold)
|
|
|
|
{
|
|
|
|
btVector3 nearest;
|
|
|
|
btNearestPointInLineSegment(cp.m_localPointB, v1, v2, nearest);
|
|
|
|
btScalar len = (contact - nearest).length();
|
|
|
|
//
|
|
|
|
if (len < disttobestedge)
|
|
|
|
{
|
|
|
|
bestedge = 1;
|
|
|
|
disttobestedge = len;
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
// Edge 2 -> 0
|
2019-01-03 14:26:51 +01:00
|
|
|
if (btFabs(info->m_edgeV2V0Angle) < triangleInfoMapPtr->m_maxEdgeAngleThreshold)
|
|
|
|
{
|
|
|
|
btVector3 nearest;
|
|
|
|
btNearestPointInLineSegment(cp.m_localPointB, v2, v0, nearest);
|
|
|
|
btScalar len = (contact - nearest).length();
|
|
|
|
//
|
|
|
|
if (len < disttobestedge)
|
|
|
|
{
|
|
|
|
bestedge = 2;
|
|
|
|
disttobestedge = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 upfix = tri_normal * btVector3(0.1f, 0.1f, 0.1f);
|
|
|
|
btDebugDrawLine(tr * v0 + upfix, tr * v1 + upfix, red);
|
|
|
|
#endif
|
|
|
|
if (btFabs(info->m_edgeV0V1Angle) < triangleInfoMapPtr->m_maxEdgeAngleThreshold)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
2019-01-03 14:26:51 +01:00
|
|
|
btDebugDrawLine(tr * contact, tr * (contact + cp.m_normalWorldOnB * 10), black);
|
2017-08-01 14:30:58 +02:00
|
|
|
#endif
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar len = (contact - nearest).length();
|
|
|
|
if (len < triangleInfoMapPtr->m_edgeDistanceThreshold)
|
|
|
|
if (bestedge == 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 edge(v0 - v1);
|
|
|
|
isNearEdge = true;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (info->m_edgeV0V1Angle == btScalar(0))
|
|
|
|
{
|
|
|
|
numConcaveEdgeHits++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool isEdgeConvex = (info->m_flags & TRI_INFO_V0V1_CONVEX);
|
|
|
|
btScalar swapFactor = isEdgeConvex ? btScalar(1) : btScalar(-1);
|
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
|
|
|
btDebugDrawLine(tr * nearest, tr * (nearest + swapFactor * tri_normal * 10), white);
|
|
|
|
#endif //BT_INTERNAL_EDGE_DEBUG_DRAW
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 nA = swapFactor * tri_normal;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btQuaternion orn(edge, info->m_edgeV0V1Angle);
|
|
|
|
btVector3 computedNormalB = quatRotate(orn, tri_normal);
|
|
|
|
if (info->m_flags & TRI_INFO_V0V1_SWAP_NORMALB)
|
|
|
|
computedNormalB *= -1;
|
|
|
|
btVector3 nB = swapFactor * computedNormalB;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar NdotA = localContactNormalOnB.dot(nA);
|
|
|
|
btScalar NdotB = localContactNormalOnB.dot(nB);
|
|
|
|
bool backFacingNormal = (NdotA < triangleInfoMapPtr->m_convexEpsilon) && (NdotB < triangleInfoMapPtr->m_convexEpsilon);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_INTERNAL_EDGE
|
2019-01-03 14:26:51 +01:00
|
|
|
{
|
|
|
|
btDebugDrawLine(cp.getPositionWorldOnB(), cp.getPositionWorldOnB() + tr.getBasis() * (nB * 20), red);
|
|
|
|
}
|
|
|
|
#endif //DEBUG_INTERNAL_EDGE
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (backFacingNormal)
|
|
|
|
{
|
|
|
|
numConcaveEdgeHits++;
|
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
numConvexEdgeHits++;
|
|
|
|
btVector3 clampedLocalNormal;
|
|
|
|
bool isClamped = btClampNormal(edge, swapFactor * tri_normal, localContactNormalOnB, info->m_edgeV0V1Angle, clampedLocalNormal);
|
|
|
|
if (isClamped)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (((normalAdjustFlags & BT_TRIANGLE_CONVEX_DOUBLE_SIDED) != 0) || (clampedLocalNormal.dot(frontFacing * tri_normal) > 0))
|
|
|
|
{
|
|
|
|
btVector3 newNormal = colObj0Wrap->getWorldTransform().getBasis() * clampedLocalNormal;
|
|
|
|
// cp.m_distance1 = cp.m_distance1 * newNormal.dot(cp.m_normalWorldOnB);
|
|
|
|
cp.m_normalWorldOnB = newNormal;
|
|
|
|
// Reproject collision point along normal. (what about cp.m_distance1?)
|
|
|
|
cp.m_positionWorldOnB = cp.m_positionWorldOnA - cp.m_normalWorldOnB * cp.m_distance1;
|
|
|
|
cp.m_localPointB = colObj0Wrap->getWorldTransform().invXform(cp.m_positionWorldOnB);
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btNearestPointInLineSegment(contact, v1, v2, nearest);
|
2017-08-01 14:30:58 +02:00
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
2019-01-03 14:26:51 +01:00
|
|
|
btDebugDrawLine(tr * nearest, tr * cp.m_localPointB, green);
|
|
|
|
#endif //BT_INTERNAL_EDGE_DEBUG_DRAW
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
2019-01-03 14:26:51 +01:00
|
|
|
btDebugDrawLine(tr * v1 + upfix, tr * v2 + upfix, green);
|
|
|
|
#endif
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (btFabs(info->m_edgeV1V2Angle) < triangleInfoMapPtr->m_maxEdgeAngleThreshold)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
2019-01-03 14:26:51 +01:00
|
|
|
btDebugDrawLine(tr * contact, tr * (contact + cp.m_normalWorldOnB * 10), black);
|
|
|
|
#endif //BT_INTERNAL_EDGE_DEBUG_DRAW
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar len = (contact - nearest).length();
|
|
|
|
if (len < triangleInfoMapPtr->m_edgeDistanceThreshold)
|
|
|
|
if (bestedge == 1)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
isNearEdge = true;
|
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
|
|
|
btDebugDrawLine(tr * nearest, tr * (nearest + tri_normal * 10), white);
|
|
|
|
#endif //BT_INTERNAL_EDGE_DEBUG_DRAW
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 edge(v1 - v2);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
isNearEdge = true;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (info->m_edgeV1V2Angle == btScalar(0))
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
numConcaveEdgeHits++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
bool isEdgeConvex = (info->m_flags & TRI_INFO_V1V2_CONVEX) != 0;
|
|
|
|
btScalar swapFactor = isEdgeConvex ? btScalar(1) : btScalar(-1);
|
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
|
|
|
btDebugDrawLine(tr * nearest, tr * (nearest + swapFactor * tri_normal * 10), white);
|
|
|
|
#endif //BT_INTERNAL_EDGE_DEBUG_DRAW
|
|
|
|
|
|
|
|
btVector3 nA = swapFactor * tri_normal;
|
|
|
|
|
|
|
|
btQuaternion orn(edge, info->m_edgeV1V2Angle);
|
|
|
|
btVector3 computedNormalB = quatRotate(orn, tri_normal);
|
|
|
|
if (info->m_flags & TRI_INFO_V1V2_SWAP_NORMALB)
|
|
|
|
computedNormalB *= -1;
|
|
|
|
btVector3 nB = swapFactor * computedNormalB;
|
|
|
|
|
|
|
|
#ifdef DEBUG_INTERNAL_EDGE
|
|
|
|
{
|
|
|
|
btDebugDrawLine(cp.getPositionWorldOnB(), cp.getPositionWorldOnB() + tr.getBasis() * (nB * 20), red);
|
|
|
|
}
|
|
|
|
#endif //DEBUG_INTERNAL_EDGE
|
|
|
|
|
|
|
|
btScalar NdotA = localContactNormalOnB.dot(nA);
|
|
|
|
btScalar NdotB = localContactNormalOnB.dot(nB);
|
|
|
|
bool backFacingNormal = (NdotA < triangleInfoMapPtr->m_convexEpsilon) && (NdotB < triangleInfoMapPtr->m_convexEpsilon);
|
|
|
|
|
|
|
|
if (backFacingNormal)
|
|
|
|
{
|
|
|
|
numConcaveEdgeHits++;
|
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
numConvexEdgeHits++;
|
|
|
|
btVector3 localContactNormalOnB = colObj0Wrap->getWorldTransform().getBasis().transpose() * cp.m_normalWorldOnB;
|
|
|
|
btVector3 clampedLocalNormal;
|
|
|
|
bool isClamped = btClampNormal(edge, swapFactor * tri_normal, localContactNormalOnB, info->m_edgeV1V2Angle, clampedLocalNormal);
|
|
|
|
if (isClamped)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (((normalAdjustFlags & BT_TRIANGLE_CONVEX_DOUBLE_SIDED) != 0) || (clampedLocalNormal.dot(frontFacing * tri_normal) > 0))
|
|
|
|
{
|
|
|
|
btVector3 newNormal = colObj0Wrap->getWorldTransform().getBasis() * clampedLocalNormal;
|
|
|
|
// cp.m_distance1 = cp.m_distance1 * newNormal.dot(cp.m_normalWorldOnB);
|
|
|
|
cp.m_normalWorldOnB = newNormal;
|
|
|
|
// Reproject collision point along normal.
|
|
|
|
cp.m_positionWorldOnB = cp.m_positionWorldOnA - cp.m_normalWorldOnB * cp.m_distance1;
|
|
|
|
cp.m_localPointB = colObj0Wrap->getWorldTransform().invXform(cp.m_positionWorldOnB);
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btNearestPointInLineSegment(contact, v2, v0, nearest);
|
2017-08-01 14:30:58 +02:00
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
2019-01-03 14:26:51 +01:00
|
|
|
btDebugDrawLine(tr * nearest, tr * cp.m_localPointB, blue);
|
|
|
|
#endif //BT_INTERNAL_EDGE_DEBUG_DRAW
|
2017-08-01 14:30:58 +02:00
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
2019-01-03 14:26:51 +01:00
|
|
|
btDebugDrawLine(tr * v2 + upfix, tr * v0 + upfix, blue);
|
|
|
|
#endif
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (btFabs(info->m_edgeV2V0Angle) < triangleInfoMapPtr->m_maxEdgeAngleThreshold)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
2019-01-03 14:26:51 +01:00
|
|
|
btDebugDrawLine(tr * contact, tr * (contact + cp.m_normalWorldOnB * 10), black);
|
|
|
|
#endif //BT_INTERNAL_EDGE_DEBUG_DRAW
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar len = (contact - nearest).length();
|
|
|
|
if (len < triangleInfoMapPtr->m_edgeDistanceThreshold)
|
|
|
|
if (bestedge == 2)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
isNearEdge = true;
|
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
|
|
|
btDebugDrawLine(tr * nearest, tr * (nearest + tri_normal * 10), white);
|
|
|
|
#endif //BT_INTERNAL_EDGE_DEBUG_DRAW
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 edge(v2 - v0);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (info->m_edgeV2V0Angle == btScalar(0))
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
numConcaveEdgeHits++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
bool isEdgeConvex = (info->m_flags & TRI_INFO_V2V0_CONVEX) != 0;
|
|
|
|
btScalar swapFactor = isEdgeConvex ? btScalar(1) : btScalar(-1);
|
|
|
|
#ifdef BT_INTERNAL_EDGE_DEBUG_DRAW
|
|
|
|
btDebugDrawLine(tr * nearest, tr * (nearest + swapFactor * tri_normal * 10), white);
|
|
|
|
#endif //BT_INTERNAL_EDGE_DEBUG_DRAW
|
|
|
|
|
|
|
|
btVector3 nA = swapFactor * tri_normal;
|
|
|
|
btQuaternion orn(edge, info->m_edgeV2V0Angle);
|
|
|
|
btVector3 computedNormalB = quatRotate(orn, tri_normal);
|
|
|
|
if (info->m_flags & TRI_INFO_V2V0_SWAP_NORMALB)
|
|
|
|
computedNormalB *= -1;
|
|
|
|
btVector3 nB = swapFactor * computedNormalB;
|
|
|
|
|
|
|
|
#ifdef DEBUG_INTERNAL_EDGE
|
|
|
|
{
|
|
|
|
btDebugDrawLine(cp.getPositionWorldOnB(), cp.getPositionWorldOnB() + tr.getBasis() * (nB * 20), red);
|
|
|
|
}
|
|
|
|
#endif //DEBUG_INTERNAL_EDGE
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar NdotA = localContactNormalOnB.dot(nA);
|
|
|
|
btScalar NdotB = localContactNormalOnB.dot(nB);
|
|
|
|
bool backFacingNormal = (NdotA < triangleInfoMapPtr->m_convexEpsilon) && (NdotB < triangleInfoMapPtr->m_convexEpsilon);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (backFacingNormal)
|
|
|
|
{
|
|
|
|
numConcaveEdgeHits++;
|
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
numConvexEdgeHits++;
|
|
|
|
// printf("hitting convex edge\n");
|
|
|
|
|
|
|
|
btVector3 localContactNormalOnB = colObj0Wrap->getWorldTransform().getBasis().transpose() * cp.m_normalWorldOnB;
|
|
|
|
btVector3 clampedLocalNormal;
|
|
|
|
bool isClamped = btClampNormal(edge, swapFactor * tri_normal, localContactNormalOnB, info->m_edgeV2V0Angle, clampedLocalNormal);
|
|
|
|
if (isClamped)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (((normalAdjustFlags & BT_TRIANGLE_CONVEX_DOUBLE_SIDED) != 0) || (clampedLocalNormal.dot(frontFacing * tri_normal) > 0))
|
|
|
|
{
|
|
|
|
btVector3 newNormal = colObj0Wrap->getWorldTransform().getBasis() * clampedLocalNormal;
|
|
|
|
// cp.m_distance1 = cp.m_distance1 * newNormal.dot(cp.m_normalWorldOnB);
|
|
|
|
cp.m_normalWorldOnB = newNormal;
|
|
|
|
// Reproject collision point along normal.
|
|
|
|
cp.m_positionWorldOnB = cp.m_positionWorldOnA - cp.m_normalWorldOnB * cp.m_distance1;
|
|
|
|
cp.m_localPointB = colObj0Wrap->getWorldTransform().invXform(cp.m_positionWorldOnB);
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_INTERNAL_EDGE
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 color(0, 1, 1);
|
|
|
|
btDebugDrawLine(cp.getPositionWorldOnB(), cp.getPositionWorldOnB() + cp.m_normalWorldOnB * 10, color);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //DEBUG_INTERNAL_EDGE
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
if (isNearEdge)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (numConcaveEdgeHits > 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if ((normalAdjustFlags & BT_TRIANGLE_CONCAVE_DOUBLE_SIDED) != 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
//fix tri_normal so it pointing the same direction as the current local contact normal
|
|
|
|
if (tri_normal.dot(localContactNormalOnB) < 0)
|
|
|
|
{
|
|
|
|
tri_normal *= -1;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
cp.m_normalWorldOnB = colObj0Wrap->getWorldTransform().getBasis() * tri_normal;
|
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 newNormal = tri_normal * frontFacing;
|
2017-08-01 14:30:58 +02:00
|
|
|
//if the tri_normal is pointing opposite direction as the current local contact normal, skip it
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar d = newNormal.dot(localContactNormalOnB);
|
|
|
|
if (d < 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//modify the normal to be the triangle normal (or backfacing normal)
|
2019-01-03 14:26:51 +01:00
|
|
|
cp.m_normalWorldOnB = colObj0Wrap->getWorldTransform().getBasis() * newNormal;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
// Reproject collision point along normal.
|
|
|
|
cp.m_positionWorldOnB = cp.m_positionWorldOnA - cp.m_normalWorldOnB * cp.m_distance1;
|
|
|
|
cp.m_localPointB = colObj0Wrap->getWorldTransform().invXform(cp.m_positionWorldOnB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|