方法一:获取顶点和索引的方法来画出碰撞膜
hkpRigidBody* createMesh(const hkVector4& position, hkReal scale)
{
hkpStorageExtendedMeshShape* extendedMesh = new hkpStorageExtendedMeshShape();
for(int numobj=0; numobj<m.Objects[numobj].numObjects; numobj++)
{
hkpSimpleMeshShape* meshShape = new hkpSimpleMeshShape( 0.05f );
{
int side=m.Objects[numobj].numVerts;
meshShape->m_vertices.setSize( side );
for (int j = 0; j < side; j++ )
{
hkVector4 vertex (m.Objects[numobj].Vertexes[j*3+0],
m.Objects[numobj].Vertexes[j*3+1],
m.Objects[numobj].Vertexes[j*3+2] );
//vertex.add4(position);
vertex.mul4(scale);
meshShape->m_vertices[j] = vertex ;
}
side=m.Objects[numobj].numFaces/3;
meshShape->m_triangles.setSize( side);
for (int j = 0; j < side; j++ )
{
meshShape->m_triangles[j].m_a = m.Objects[numobj].Faces[3*j+0];
meshShape->m_triangles[j].m_b = m.Objects[numobj].Faces[3*j+1];
meshShape->m_triangles[j].m_c = m.Objects[numobj].Faces[3*j+2];
}
}
hkpExtendedMeshShape::TrianglesSubpart part;
part.m_numTriangleShapes = meshShape->m_triangles.getSize();
part.m_numVertices = meshShape->m_vertices.getSize();
part.m_vertexBase = (float*)meshShape->m_vertices.begin();
part.m_stridingType = hkpExtendedMeshShape::INDICES_INT16;
part.m_vertexStriding = sizeof(hkVector4);
part.m_indexBase = meshShape->m_triangles.begin();
part.m_indexStriding = sizeof(hkpSimpleMeshShape::Triangle);
extendedMesh->addTrianglesSubpart( part );
}
hkpMoppCompilerInput mci;
mci.m_enableChunkSubdivision = false;
hkpMoppCode* code = hkpMoppUtility::buildCode( extendedMesh ,mci);
hkpMoppBvTreeShape* moppShape = new hkpMoppBvTreeShape(extendedMesh, code);
code->removeReference();
extendedMesh->removeReference();
hkpRigidBodyCinfo info;
info.m_friction = 10;
info.m_shape = extendedMesh;
info.m_motionType = hkpMotion::MOTION_FIXED;
info.m_position = position;
info.m_rotation = hkQuaternion(hkVector4(0,1,0),0);
hkpRigidBody *r=new hkpRigidBody(info);
m_world->addEntity(r);
return r;
}
int DrawRigidBody(hkpRigidBody *rbody,hkVector4 &color)
{
if(!rbody)return 0;
const hkpShape *shape=rbody->getCollidable()->getShape();
hkTransform t=rbody->getTransform();
hkpShapeDisplayBuilder::hkpShapeDisplayBuilderEnvironment env;
hkpShapeDisplayBuilder GeometryBuilder(env);
hkArray<hkDisplayGeometry*>* displayGeometries;
displayGeometries = new hkArray<hkDisplayGeometry*>;
GeometryBuilder.buildDisplayGeometries(shape, *displayGeometries );
for (int j=0; j<displayGeometries->getSize(); j++)
{
hkDisplayGeometry* geometry = (*displayGeometries)[j];
geometry->buildGeometry();
hkGeometry* mesh = geometry->getGeometry();
if(!mesh)exit(0);
//OpenGlRendering(mesh,t); //rendering using opengl....
geometry->removeReference();
}
delete displayGeometries;
return 1;
}
void InitHavok()
{
//other code...
car=CreateMesh(hkVector(0,0,0),0.05);
}
void Render()
{
//othercode...
DrawRigidBody(car,hkVector4(0,0,1));
}
方法二:直接获取aabb盒画线
hkpRigidBody *rb = xxx;
hkAabb aabbOut;
const hkpShape *pShape = rb->getCollidable()->getShape();
pShape->getAabb(rb->getTransform(), 0.0f, aabbOut);
//根据shape类型可以设置不同的颜色
hkpShapeType type = pShape->getType();
// 。。。。。。
//接下来就是根据hkAabb画线 aabbOut.m_min和aabbOut.m_max