Some changes in rendering, to get shadows for trimeshes
Add dynamic aabb tree (btDbvt) optimization for btCompoundShape/btCompoundCollisionAlgorithm Add btTransformAabb util, todo: deploy it throughout the codebase
This commit is contained in:
@@ -21,20 +21,7 @@ subject to the following restrictions:
|
||||
|
||||
void btBoxShape::getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const
|
||||
{
|
||||
btVector3 halfExtents = getHalfExtentsWithoutMargin();
|
||||
halfExtents += btVector3(getMargin(),getMargin(),getMargin());
|
||||
|
||||
|
||||
btMatrix3x3 abs_b = t.getBasis().absolute();
|
||||
btPoint3 center = t.getOrigin();
|
||||
btVector3 extent = btVector3(abs_b[0].dot(halfExtents),
|
||||
abs_b[1].dot(halfExtents),
|
||||
abs_b[2].dot(halfExtents));
|
||||
|
||||
aabbMin = center - extent;
|
||||
aabbMax = center + extent;
|
||||
|
||||
|
||||
btTransformAabb(getHalfExtentsWithoutMargin(),getMargin(),t,aabbMin,aabbMax);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -14,23 +14,29 @@ subject to the following restrictions:
|
||||
*/
|
||||
|
||||
#include "btCompoundShape.h"
|
||||
|
||||
|
||||
#include "btCollisionShape.h"
|
||||
|
||||
#include "BulletCollision/BroadphaseCollision/btDbvt.h"
|
||||
|
||||
btCompoundShape::btCompoundShape()
|
||||
:m_localAabbMin(btScalar(1e30),btScalar(1e30),btScalar(1e30)),
|
||||
m_localAabbMax(btScalar(-1e30),btScalar(-1e30),btScalar(-1e30)),
|
||||
m_aabbTree(0),
|
||||
m_collisionMargin(btScalar(0.)),
|
||||
m_localScaling(btScalar(1.),btScalar(1.),btScalar(1.))
|
||||
m_localScaling(btScalar(1.),btScalar(1.),btScalar(1.)),
|
||||
m_dynamicAabbTree(0)
|
||||
{
|
||||
void* mem = btAlignedAlloc(sizeof(btDbvt),16);
|
||||
m_dynamicAabbTree = new(mem) btDbvt();
|
||||
btAssert(mem==m_dynamicAabbTree);
|
||||
}
|
||||
|
||||
|
||||
btCompoundShape::~btCompoundShape()
|
||||
{
|
||||
if (m_dynamicAabbTree)
|
||||
{
|
||||
m_dynamicAabbTree->~btDbvt();
|
||||
btAlignedFree(m_dynamicAabbTree);
|
||||
}
|
||||
}
|
||||
|
||||
void btCompoundShape::addChildShape(const btTransform& localTransform,btCollisionShape* shape)
|
||||
@@ -60,68 +66,85 @@ void btCompoundShape::addChildShape(const btTransform& localTransform,btCollisio
|
||||
}
|
||||
|
||||
}
|
||||
if (m_dynamicAabbTree)
|
||||
{
|
||||
const btDbvtVolume bounds=btDbvtVolume::FromMM(localAabbMin,localAabbMax);
|
||||
int index = m_children.size()-1;
|
||||
child.m_node = m_dynamicAabbTree->insert(bounds,(void*)index);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void btCompoundShape::removeChildShapeByIndex(int childShapeIndex)
|
||||
{
|
||||
btAssert(childShapeIndex >=0 && childShapeIndex < m_children.size());
|
||||
if (m_dynamicAabbTree)
|
||||
{
|
||||
m_dynamicAabbTree->remove(m_children[childShapeIndex].m_node);
|
||||
}
|
||||
m_children.swap(childShapeIndex,m_children.size()-1);
|
||||
m_children.pop_back();
|
||||
|
||||
}
|
||||
|
||||
void btCompoundShape::removeChildShape(btCollisionShape* shape)
|
||||
{
|
||||
bool done_removing;
|
||||
|
||||
// Find the children containing the shape specified, and remove those children.
|
||||
do
|
||||
{
|
||||
done_removing = true;
|
||||
|
||||
for(int i = 0; i < m_children.size(); i++)
|
||||
{
|
||||
if(m_children[i].m_childShape == shape)
|
||||
{
|
||||
m_children.remove(m_children[i]);
|
||||
done_removing = false; // Do another iteration pass after removing from the vector
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
while (!done_removing);
|
||||
|
||||
recalculateLocalAabb();
|
||||
// Find the children containing the shape specified, and remove those children.
|
||||
//note: there might be multiple children using the same shape!
|
||||
for(int i = m_children.size()-1; i >= 0 ; i--)
|
||||
{
|
||||
if(m_children[i].m_childShape == shape)
|
||||
{
|
||||
m_children.swap(i,m_children.size()-1);
|
||||
m_children.pop_back();
|
||||
//remove it from the m_dynamicAabbTree too
|
||||
//m_dynamicAabbTree->remove(m_aabbProxies[i]);
|
||||
//m_aabbProxies.swap(i,m_children.size()-1);
|
||||
//m_aabbProxies.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
recalculateLocalAabb();
|
||||
}
|
||||
|
||||
void btCompoundShape::recalculateLocalAabb()
|
||||
{
|
||||
// Recalculate the local aabb
|
||||
// Brute force, it iterates over all the shapes left.
|
||||
m_localAabbMin = btVector3(btScalar(1e30),btScalar(1e30),btScalar(1e30));
|
||||
m_localAabbMax = btVector3(btScalar(-1e30),btScalar(-1e30),btScalar(-1e30));
|
||||
|
||||
//extend the local aabbMin/aabbMax
|
||||
for (int j = 0; j < m_children.size(); j++)
|
||||
{
|
||||
btVector3 localAabbMin,localAabbMax;
|
||||
m_children[j].m_childShape->getAabb(m_children[j].m_transform, localAabbMin, localAabbMax);
|
||||
for (int i=0;i<3;i++)
|
||||
{
|
||||
if (m_localAabbMin[i] > localAabbMin[i])
|
||||
m_localAabbMin[i] = localAabbMin[i];
|
||||
if (m_localAabbMax[i] < localAabbMax[i])
|
||||
m_localAabbMax[i] = localAabbMax[i];
|
||||
}
|
||||
}
|
||||
// Recalculate the local aabb
|
||||
// Brute force, it iterates over all the shapes left.
|
||||
m_localAabbMin = btVector3(btScalar(1e30),btScalar(1e30),btScalar(1e30));
|
||||
m_localAabbMax = btVector3(btScalar(-1e30),btScalar(-1e30),btScalar(-1e30));
|
||||
|
||||
//extend the local aabbMin/aabbMax
|
||||
for (int j = 0; j < m_children.size(); j++)
|
||||
{
|
||||
btVector3 localAabbMin,localAabbMax;
|
||||
m_children[j].m_childShape->getAabb(m_children[j].m_transform, localAabbMin, localAabbMax);
|
||||
for (int i=0;i<3;i++)
|
||||
{
|
||||
if (m_localAabbMin[i] > localAabbMin[i])
|
||||
m_localAabbMin[i] = localAabbMin[i];
|
||||
if (m_localAabbMax[i] < localAabbMax[i])
|
||||
m_localAabbMax[i] = localAabbMax[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///getAabb's default implementation is brute force, expected derived classes to implement a fast dedicated version
|
||||
|
||||
///getAabb's default implementation is brute force, expected derived classes to implement a fast dedicated version
|
||||
void btCompoundShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const
|
||||
{
|
||||
btVector3 localHalfExtents = btScalar(0.5)*(m_localAabbMax-m_localAabbMin);
|
||||
localHalfExtents += btVector3(getMargin(),getMargin(),getMargin());
|
||||
btVector3 localCenter = btScalar(0.5)*(m_localAabbMax+m_localAabbMin);
|
||||
|
||||
|
||||
btMatrix3x3 abs_b = trans.getBasis().absolute();
|
||||
|
||||
btPoint3 center = trans(localCenter);
|
||||
|
||||
btVector3 extent = btVector3(abs_b[0].dot(localHalfExtents),
|
||||
abs_b[1].dot(localHalfExtents),
|
||||
abs_b[2].dot(localHalfExtents));
|
||||
abs_b[1].dot(localHalfExtents),
|
||||
abs_b[2].dot(localHalfExtents));
|
||||
aabbMin = center-extent;
|
||||
aabbMax = center+extent;
|
||||
|
||||
@@ -134,9 +157,9 @@ void btCompoundShape::calculateLocalInertia(btScalar mass,btVector3& inertia) co
|
||||
ident.setIdentity();
|
||||
btVector3 aabbMin,aabbMax;
|
||||
getAabb(ident,aabbMin,aabbMax);
|
||||
|
||||
|
||||
btVector3 halfExtents = (aabbMax-aabbMin)*btScalar(0.5);
|
||||
|
||||
|
||||
btScalar lx=btScalar(2.)*(halfExtents.x());
|
||||
btScalar ly=btScalar(2.)*(halfExtents.y());
|
||||
btScalar lz=btScalar(2.)*(halfExtents.z());
|
||||
@@ -152,57 +175,57 @@ void btCompoundShape::calculateLocalInertia(btScalar mass,btVector3& inertia) co
|
||||
|
||||
void btCompoundShape::calculatePrincipalAxisTransform(btScalar* masses, btTransform& principal, btVector3& inertia) const
|
||||
{
|
||||
int n = m_children.size();
|
||||
int n = m_children.size();
|
||||
|
||||
btScalar totalMass = 0;
|
||||
btVector3 center(0, 0, 0);
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
center += m_children[k].m_transform.getOrigin() * masses[k];
|
||||
totalMass += masses[k];
|
||||
}
|
||||
center /= totalMass;
|
||||
principal.setOrigin(center);
|
||||
btScalar totalMass = 0;
|
||||
btVector3 center(0, 0, 0);
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
center += m_children[k].m_transform.getOrigin() * masses[k];
|
||||
totalMass += masses[k];
|
||||
}
|
||||
center /= totalMass;
|
||||
principal.setOrigin(center);
|
||||
|
||||
btMatrix3x3 tensor(0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
btVector3 i;
|
||||
m_children[k].m_childShape->calculateLocalInertia(masses[k], i);
|
||||
btMatrix3x3 tensor(0, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
btVector3 i;
|
||||
m_children[k].m_childShape->calculateLocalInertia(masses[k], i);
|
||||
|
||||
const btTransform& t = m_children[k].m_transform;
|
||||
btVector3 o = t.getOrigin() - center;
|
||||
|
||||
//compute inertia tensor in coordinate system of compound shape
|
||||
btMatrix3x3 j = t.getBasis().transpose();
|
||||
j[0] *= i[0];
|
||||
j[1] *= i[1];
|
||||
j[2] *= i[2];
|
||||
j = t.getBasis() * j;
|
||||
|
||||
//add inertia tensor
|
||||
tensor[0] += j[0];
|
||||
tensor[1] += j[1];
|
||||
tensor[2] += j[2];
|
||||
const btTransform& t = m_children[k].m_transform;
|
||||
btVector3 o = t.getOrigin() - center;
|
||||
|
||||
//compute inertia tensor of pointmass at o
|
||||
btScalar o2 = o.length2();
|
||||
j[0].setValue(o2, 0, 0);
|
||||
j[1].setValue(0, o2, 0);
|
||||
j[2].setValue(0, 0, o2);
|
||||
j[0] += o * -o.x();
|
||||
j[1] += o * -o.y();
|
||||
j[2] += o * -o.z();
|
||||
//compute inertia tensor in coordinate system of compound shape
|
||||
btMatrix3x3 j = t.getBasis().transpose();
|
||||
j[0] *= i[0];
|
||||
j[1] *= i[1];
|
||||
j[2] *= i[2];
|
||||
j = t.getBasis() * j;
|
||||
|
||||
//add inertia tensor of pointmass
|
||||
tensor[0] += masses[k] * j[0];
|
||||
tensor[1] += masses[k] * j[1];
|
||||
tensor[2] += masses[k] * j[2];
|
||||
}
|
||||
//add inertia tensor
|
||||
tensor[0] += j[0];
|
||||
tensor[1] += j[1];
|
||||
tensor[2] += j[2];
|
||||
|
||||
tensor.diagonalize(principal.getBasis(), btScalar(0.00001), 20);
|
||||
inertia.setValue(tensor[0][0], tensor[1][1], tensor[2][2]);
|
||||
//compute inertia tensor of pointmass at o
|
||||
btScalar o2 = o.length2();
|
||||
j[0].setValue(o2, 0, 0);
|
||||
j[1].setValue(0, o2, 0);
|
||||
j[2].setValue(0, 0, o2);
|
||||
j[0] += o * -o.x();
|
||||
j[1] += o * -o.y();
|
||||
j[2] += o * -o.z();
|
||||
|
||||
//add inertia tensor of pointmass
|
||||
tensor[0] += masses[k] * j[0];
|
||||
tensor[1] += masses[k] * j[1];
|
||||
tensor[2] += masses[k] * j[2];
|
||||
}
|
||||
|
||||
tensor.diagonalize(principal.getBasis(), btScalar(0.00001), 20);
|
||||
inertia.setValue(tensor[0][0], tensor[1][1], tensor[2][2]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -24,8 +24,8 @@ subject to the following restrictions:
|
||||
#include "btCollisionMargin.h"
|
||||
#include "LinearMath/btAlignedObjectArray.h"
|
||||
|
||||
class btOptimizedBvh;
|
||||
|
||||
//class btOptimizedBvh;
|
||||
struct btDbvt;
|
||||
|
||||
ATTRIBUTE_ALIGNED16(struct) btCompoundShapeChild
|
||||
{
|
||||
@@ -35,14 +35,15 @@ ATTRIBUTE_ALIGNED16(struct) btCompoundShapeChild
|
||||
btCollisionShape* m_childShape;
|
||||
int m_childShapeType;
|
||||
btScalar m_childMargin;
|
||||
struct btDbvtNode* m_node;
|
||||
};
|
||||
|
||||
SIMD_FORCE_INLINE bool operator==(const btCompoundShapeChild& c1, const btCompoundShapeChild& c2)
|
||||
{
|
||||
return ( c1.m_transform == c2.m_transform &&
|
||||
c1.m_childShape == c2.m_childShape &&
|
||||
c1.m_childShapeType == c2.m_childShapeType &&
|
||||
c1.m_childMargin == c2.m_childMargin );
|
||||
return ( c1.m_transform == c2.m_transform &&
|
||||
c1.m_childShape == c2.m_childShape &&
|
||||
c1.m_childShapeType == c2.m_childShapeType &&
|
||||
c1.m_childMargin == c2.m_childMargin );
|
||||
}
|
||||
|
||||
/// btCompoundShape allows to store multiple other btCollisionShapes
|
||||
@@ -55,7 +56,8 @@ ATTRIBUTE_ALIGNED16(class) btCompoundShape : public btCollisionShape
|
||||
btVector3 m_localAabbMin;
|
||||
btVector3 m_localAabbMax;
|
||||
|
||||
btOptimizedBvh* m_aabbTree;
|
||||
//btOptimizedBvh* m_aabbTree;
|
||||
btDbvt* m_dynamicAabbTree;
|
||||
|
||||
public:
|
||||
BT_DECLARE_ALIGNED_ALLOCATOR();
|
||||
@@ -66,11 +68,11 @@ public:
|
||||
|
||||
void addChildShape(const btTransform& localTransform,btCollisionShape* shape);
|
||||
|
||||
/** Remove all children shapes that contain the specified shape. */
|
||||
/// Remove all children shapes that contain the specified shape
|
||||
virtual void removeChildShape(btCollisionShape* shape);
|
||||
|
||||
|
||||
|
||||
void removeChildShapeByIndex(int childShapeindex);
|
||||
|
||||
|
||||
int getNumChildShapes() const
|
||||
{
|
||||
@@ -103,9 +105,9 @@ public:
|
||||
|
||||
///getAabb's default implementation is brute force, expected derived classes to implement a fast dedicated version
|
||||
virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const;
|
||||
|
||||
/** Re-calculate the local Aabb. Is called at the end of removeChildShapes.
|
||||
Use this yourself if you modify the children or their transforms. */
|
||||
|
||||
/** Re-calculate the local Aabb. Is called at the end of removeChildShapes.
|
||||
Use this yourself if you modify the children or their transforms. */
|
||||
virtual void recalculateLocalAabb();
|
||||
|
||||
virtual void setLocalScaling(const btVector3& scaling)
|
||||
@@ -118,7 +120,7 @@ public:
|
||||
}
|
||||
|
||||
virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const;
|
||||
|
||||
|
||||
virtual int getShapeType() const { return COMPOUND_SHAPE_PROXYTYPE;}
|
||||
|
||||
virtual void setMargin(btScalar margin)
|
||||
@@ -137,9 +139,9 @@ public:
|
||||
//this is optional, but should make collision queries faster, by culling non-overlapping nodes
|
||||
void createAabbTreeFromChildren();
|
||||
|
||||
const btOptimizedBvh* getAabbTree() const
|
||||
btDbvt* getDynamicAabbTree()
|
||||
{
|
||||
return m_aabbTree;
|
||||
return m_dynamicAabbTree;
|
||||
}
|
||||
|
||||
///computes the exact moment of inertia and the transform from the coordinate system defined by the principal axes of the moment of inertia
|
||||
|
||||
@@ -18,6 +18,7 @@ subject to the following restrictions:
|
||||
|
||||
#include "LinearMath/btPoint3.h"
|
||||
#include "LinearMath/btMatrix3x3.h"
|
||||
#include "LinearMath/btAabbUtil2.h"
|
||||
#include "btConvexInternalShape.h"
|
||||
|
||||
|
||||
@@ -46,28 +47,7 @@ public:
|
||||
|
||||
//lazy evaluation of local aabb
|
||||
btAssert(m_isLocalAabbValid);
|
||||
|
||||
btAssert(m_localAabbMin.getX() <= m_localAabbMax.getX());
|
||||
btAssert(m_localAabbMin.getY() <= m_localAabbMax.getY());
|
||||
btAssert(m_localAabbMin.getZ() <= m_localAabbMax.getZ());
|
||||
|
||||
|
||||
btVector3 localHalfExtents = btScalar(0.5)*(m_localAabbMax-m_localAabbMin);
|
||||
localHalfExtents+= btVector3(margin,margin,margin);
|
||||
btVector3 localCenter = btScalar(0.5)*(m_localAabbMax+m_localAabbMin);
|
||||
|
||||
btMatrix3x3 abs_b = trans.getBasis().absolute();
|
||||
|
||||
btPoint3 center = trans(localCenter);
|
||||
|
||||
btVector3 extent = btVector3(abs_b[0].dot(localHalfExtents),
|
||||
abs_b[1].dot(localHalfExtents),
|
||||
abs_b[2].dot(localHalfExtents));
|
||||
|
||||
aabbMin = center-extent;
|
||||
aabbMax = center+extent;
|
||||
|
||||
|
||||
btTransformAabb(m_localAabbMin,m_localAabbMax,margin,trans,aabbMin,aabbMax);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user