1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
mTerrainGroup->loadAllTerrains(true);
// ### ADD Physics to terrain ###
Ogre::TerrainGroup::TerrainIterator ti = mTerrainGroup->getTerrainIterator();
while(ti.hasMoreElements()) {
Ogre::Terrain* terrain = ti.getNext()->instance;
Ogre::uint16 numCols, numRows;
numRows = numCols = terrain->getSize();
float *terrainHeights = terrain->getHeightData();
physx::PxHeightFieldSample* samples = (physx::PxHeightFieldSample*) new physx::PxHeightFieldSample[numRows*numCols];//physx::PxHeightFieldSample* samples = (physx::PxHeightFieldSample*)malloc(sizeof(physx::PxHeightFieldSample)*(numRows*numCols));
const physx::PxReal heightScale = (terrain->getMaxHeight() / 65536.0f / 2.f);;
for (Ogre::uint16 row = 0; row < numRows; row++) {
for (Ogre::uint16 column = 0; column < numCols; column++) {
samples[row + (column * numRows)].height = (physx::PxI16) (terrainHeights[row + (column * numRows)] / heightScale);
samples[row + (column * numRows)].materialIndex0=0;
samples[row + (column * numRows)].materialIndex1=0;
}
}
physx::PxHeightFieldDesc heightFieldDesc;
heightFieldDesc.format = physx::PxHeightFieldFormat::eS16_TM;
heightFieldDesc.nbColumns = numCols;
heightFieldDesc.nbRows = numRows;
heightFieldDesc.samples.data = samples;
heightFieldDesc.samples.stride = sizeof(physx::PxHeightFieldSample);
physx::PxPhysics *physics = Api::getSingletonPtr()->getPhysics();
physx::PxHeightField* heightField = physics->createHeightField(heightFieldDesc);
physx::PxTransform pose = physx::PxTransform::createIdentity();
Ogre::Vector3 centerPosition = terrain->getPosition ();
centerPosition.y = 0;
pose.p = physx::PxVec3(centerPosition.x,centerPosition.y,centerPosition.z);
physx::PxRigidStatic* hfActor = physics->createRigidStatic(pose);
physx::PxHeightFieldGeometry hfGeom(heightField, physx::PxMeshGeometryFlags(), heightScale, 1, 1);
physx::PxMaterial* material = physics->createMaterial(0.5f, 0.5f, 0.1f); //static friction, dynamic friction, restitution
physx::PxShape* shape = hfActor->createShape(hfGeom,*material);
physx::PxFilterData collFilterData;
collFilterData.word0=1;
collFilterData.word1=1;
shape->setSimulationFilterData(collFilterData);
mPhysXScene->addActor(*hfActor);
}
// ### /ADD Physics to terrain ###
mTerrainGroup->freeTemporaryResources(); |
Partager