4 #include "BlMotorizedJoint.h"
5 #include "BlRigidBody.h"
6 #include "BlClassFactory.h"
7 #include "BlSimulator.h"
8 #include "BlOsgGeometry.h"
15 #pragma region CreateGeometry_Code
17 btHeightfieldTerrainShape BULLET_PORT *CreateBtHeightField(osg::HeightField *osgHeightField,
float fltSegWidth,
float fltSegLength,
float &fltMinHeight,
float &fltMaxHeight, btScalar **aryHeightData)
20 fltMaxHeight = -10000;
22 int iCols = osgHeightField->getNumColumns();
23 int iRows = osgHeightField->getNumRows();
25 btScalar * heightfieldData =
new btScalar[iCols*iRows];
28 for(
int iOsgRow=0, iBtRow=(iRows-1); iOsgRow<iRows; iOsgRow++, iBtRow--)
29 for(
int iOsgCol=0, iBtCol=(iCols-1); iOsgCol<iCols; iOsgCol++,iBtCol--)
31 float fltHeight = osgHeightField->getHeight(iOsgCol, iOsgRow);
32 heightfieldData[(iBtRow*iCols) + iOsgCol] = fltHeight;
33 if(fltHeight > fltMaxHeight)
34 fltMaxHeight = fltHeight;
35 if(fltHeight < fltMinHeight)
36 fltMinHeight = fltHeight;
40 btHeightfieldTerrainShape* heightFieldShape =
new btHeightfieldTerrainShape(iCols, iRows, heightfieldData, 1, fltMinHeight, fltMaxHeight, 1, PHY_FLOAT ,
false);
42 btVector3 localScaling(fltSegWidth, 1, fltSegLength);
43 heightFieldShape->setLocalScaling(localScaling);
44 heightFieldShape->setUseDiamondSubdivision(
true);
46 *aryHeightData = heightfieldData;
48 return heightFieldShape;
51 btConvexHullShape BULLET_PORT *OsgMeshToConvexHull(osg::Node *lpNode,
bool bOptimize,
float fltMargin)
53 btConvexHullShape *originalConvexShape = osgbCollision::btConvexHullCollisionShapeFromOSG(lpNode);
56 originalConvexShape->setMargin(fltMargin);
58 btConvexHullShape* simplifiedConvexShape = NULL;
62 btShapeHull* hull =
new btShapeHull(originalConvexShape);
63 btScalar margin = originalConvexShape->getMargin();
65 hull->buildHull(margin);
67 hull->buildHull(fltMargin);
69 simplifiedConvexShape =
new btConvexHullShape((
const btScalar *) hull->getVertexPointer(),hull->numVertices());
73 if(simplifiedConvexShape && originalConvexShape->getNumVertices() > simplifiedConvexShape->getNumVertices())
75 if(originalConvexShape)
76 delete originalConvexShape;
78 return simplifiedConvexShape;
82 if(simplifiedConvexShape)
83 delete simplifiedConvexShape;
85 return originalConvexShape;
108 float BULLET_PORT OsgConvexHullVolume( osg::Node* node)
110 btConvexHullShape *lpHull = OsgConvexShrunkenHullCollisionShape(node);
115 btShapeHull sh( lpHull );
116 sh.buildHull( btScalar( 0. ) );
117 int nVerts( sh.numVertices () );
118 int nIdx( sh.numIndices() );
119 if( (nVerts <= 0) || (nIdx <= 0) )
122 const btVector3* bVerts( sh.getVertexPointer() );
123 const unsigned int* bIdx( sh.getIndexPointer() );
125 osg::ref_ptr<osg::Vec3Array> v(
new osg::Vec3Array);
128 for( idx = 0; idx < (
unsigned int)nVerts; idx++ )
129 ( *v )[ idx ] = osgbCollision::asOsgVec3( bVerts[ idx ] );
132 float fltXSum = 0, fltYSum=0, fltZSum=0;
133 for( idx = 0; idx < (
unsigned int)nVerts; idx++ )
135 fltXSum += (*v)[idx][0];
136 fltYSum += (*v)[idx][1];
137 fltZSum += (*v)[idx][2];
142 osg::Vec3 vCenterPoint( (fltXSum/nVerts), (fltYSum/nVerts), (fltZSum/nVerts));
144 float fltTotalVolume = 0;
147 for( idx = 2; idx < (
unsigned int)nIdx; idx+=3 )
149 unsigned int iIdx1 = bIdx[ idx-2 ];
150 osg::Vec3 v1 = (*v)[iIdx1];
152 unsigned int iIdx2 = bIdx[ idx-1 ];
153 osg::Vec3 v2 = (*v)[iIdx2];
155 unsigned int iIdx3 = bIdx[ idx-0 ];
156 osg::Vec3 v3 = (*v)[iIdx3];
160 fltTotalVolume += p.Volume();
167 return fltTotalVolume;
182 btConvexHullShape* OsgConvexShrunkenHullCollisionShape( osg::Node* node )
184 btConvexHullShape *lpHull = OsgMeshToConvexHull(node,
true, 0);
186 const btVector3 *aryPoints = lpHull->getUnscaledPoints();
187 int iVerts = lpHull->getNumVertices();
190 btAlignedObjectArray<btVector3> aryVerts;
191 for(
int iIdx=0; iIdx<iVerts; iIdx++ )
193 btVector3 vVert = aryPoints[iIdx];
194 aryVerts.push_back(btVector3(vVert[0], vVert[1], vVert[2]) );
199 btAlignedObjectArray<btVector3> aryPlaneEquations;
201 btGeometryUtil::getPlaneEquationsFromVertices(aryVerts, aryPlaneEquations);
203 btConvexHullShape *lpConvexShape =
new btConvexHullShape();
204 float fltMargin = lpConvexShape->getMargin();
206 lpConvexShape->setMargin(0);
208 int sz = aryPlaneEquations.size();
209 bool tooSmall =
false;
210 for (
int i=0 ; i<sz ; ++i) {
211 if ((aryPlaneEquations[i][3] += lpConvexShape->getMargin()) >= 0) {
219 btGeometryUtil::getVerticesFromPlaneEquations(aryPlaneEquations, aryVerts);
222 sz = aryVerts.size();
223 for (
int i=0 ; i<sz ; ++i) {
224 lpConvexShape->addPoint(aryVerts[i]);
227 return lpConvexShape;
233 btVector3 Vec3AnimatToBullet(
const CStdFPoint &vPoint)
235 btVector3 vVal(vPoint.x, vPoint.y, vPoint.z);
239 CStdFPoint Vec3BulletToAnimat(
const btVector3 &vPoint)
241 CStdFPoint vVal(vPoint[0], vPoint[1], vPoint[2]);
Classes for implementing the cm-labs vortex physics engine for AnimatLab.