how can i add physics to a body(terrain)

whw

28-10-2008 01:02:50

how can i add physics to a body(terrain)?
this is my code

NxOgre::Material* material=mPhysiX->mNxScene->createMaterial("terrain_materail");
material->setAll(0,0.5,0.5);
NxOgre::ShapeParams sp;
sp.setToDefault();
Body* terrainBody=mPhysiX->mNxScene->createBody("terrain", new NxOgre::Cube(40,10,86,sp), Vector3(0,-22,0), "model: terrain.mesh", "static:yes,group:head_one_group");

NoodlesOnMyBack

28-10-2008 03:45:57

If you download ETM,call this method after you create the map to create the terrain actor, source from Kungfoomasta:

void Physics::createTerrainActor()
{
NxHeightFieldDesc mNxHeightFieldDesc;

ET::TerrainManager * tMgr = mTerrain->getTerrainManager();
mNxHeightFieldDesc.nbColumns = (NxU32)tMgr->getTerrainInfo().getHeight();
mNxHeightFieldDesc.nbRows = (NxU32)tMgr->getTerrainInfo().getWidth();
mNxHeightFieldDesc.verticalExtent = -1000;
mNxHeightFieldDesc.convexEdgeThreshold = 0;

//Allocate storage for data
mNxHeightFieldDesc.samples = new NxU32[mNxHeightFieldDesc.nbColumns * mNxHeightFieldDesc.nbRows];
mNxHeightFieldDesc.sampleStride = sizeof(NxU32);

NxU8* currentByte = (NxU8*)mNxHeightFieldDesc.samples;
ET::TerrainInfo i = tMgr->getTerrainInfo();
std::vector<float> heightData = i.getHeightmapData();

for (NxU32 row = 0; row < mNxHeightFieldDesc.nbRows; row++)
{
for (NxU32 column = 0; column < mNxHeightFieldDesc.nbColumns; column++)
{
NxHeightFieldSample* currentSample = (NxHeightFieldSample*)currentByte;

//Transform ETM array of floats into NxHeightFieldSample
currentSample->height = static_cast<NxI16>(((i.at(row,column)) * 65535) - 32768);
currentSample->materialIndex0 = 0;
currentSample->materialIndex1 = 0;

currentSample->tessFlag = 0;

currentByte += mNxHeightFieldDesc.sampleStride;
}
}
NxHeightField *mHeightField;
mHeightField = mWorld->getSDK()->createHeightField(mNxHeightFieldDesc);

delete[] mNxHeightFieldDesc.samples;

Ogre::AxisAlignedBox aab = tMgr->getTerrainInfo().getExtents(); // getDimensions();
Ogre::Vector3 s = aab.getSize();

NxVec3 size;
size.x = s.x;
size.y = s.y;
size.z = s.z;

NxHeightFieldShapeDesc heightFieldShapeDesc;
heightFieldShapeDesc.heightField = mHeightField;
heightFieldShapeDesc.shapeFlags = NX_SF_FEATURE_INDICES | NX_SF_VISUALIZATION;
heightFieldShapeDesc.heightScale = size.y / 65536.0f;
heightFieldShapeDesc.rowScale = size.x / NxReal(mNxHeightFieldDesc.nbRows-1);
heightFieldShapeDesc.columnScale = size.z / NxReal(mNxHeightFieldDesc.nbColumns-1);

heightFieldShapeDesc.materialIndexHighBits = 0;
heightFieldShapeDesc.holeMaterial = 2;
heightFieldShapeDesc.localPose.t = NxVec3(0,0,0);
Ogre::Vector3 offset = tMgr->getTerrainInfo().getOffset();
heightFieldShapeDesc.localPose.t = NxVec3(offset.x,0,offset.z); // AABB1

NxActorDesc actorDesc;
actorDesc.shapes.pushBack(&heightFieldShapeDesc);
actorDesc.density = 1.0f;
actorDesc.globalPose.t = NxVec3(0,size.y/2,0);

mScene->getNxScene()->createActor(actorDesc);

return;
}


But if you are using static regular meshes, convert them to .nxs files and load them.

whw

28-10-2008 09:15:41

thank you very much,i will try your code

betajaen

28-10-2008 09:54:39

Bad way, Bad bad way.

Use ManualHeightfield if you want to do something like that.

whw

29-10-2008 02:24:45

hi beatjaen i have confused this problem a long time,i read most of the post about the terrain in NxOgre,people about create terrain through Heightfield have differ way.can you give me standard way? thanks

betajaen

29-10-2008 09:15:34

It's all in the wiki.

You do not have to do it via PhysX. ManualHeightfield is there for a reason.