Scaling collision objects

Started by
0 comments, last by Aluthreney 11 years ago

How do I scale Bullet Physics collision objects [to fit in Irrlicht terrain mesh]?

This is the setup of my terrain mesh:


ITexture *texture=driver->getTexture("./assets/map_image_resize.jpg");

ITerrainSceneNode *terrain=smgr->addTerrainSceneNode(texture->getName(), 0 , -1, vector3df(0.0f, 0.0f,0.0f),
    vector3df(0.0f, 0.0f, 0.0f), vector3df(1.0f, 1.0f, 1.0f));

terrain->setMaterialFlag(EMF_LIGHTING, false);
terrain->setMaterialTexture(0, driver->getTexture("./assets/map_texture.jpg"));
terrain->setPosition(vector3df(0.0f, 0.0f, 0.0f));

terrain->setScale(vector3df(4.0f, 1.0f, 4.0f));

My question originated here. Since the heightmap image is only 512x512px and I wanted the mesh to be 2048x2048 I had to scale the scene node [last line in the code above]. However I used the ITexture *texture as the reference for creating the collision object shape[code below].


int arraySize=texture->getSize().Height;
f32 heightData[arraySize*arraySize];

I think it's safe to assume that the collision shape is 512x512 like the height map image, but it's hard to say for sure since there isn't any visible output. The following code is my feeble attempt at trying to answer to my own question, but it didn't work as expected:


btCollisionShape *groundShape=new btHeightfieldTerrainShape(arraySize, arraySize, heightDataPtr, 1.f, minHeight, maxHeight, 1, PHY_FLOAT, false);

groundShape->setLocalScaling(btVector3(4.0f, 1.0f, 4.0f));

I'll add the whole source code in a reply post in order to keep everything organized.

P.S. I'm posting this in the beginners section because this seems like a simple problem to resolve...for someone with more knowledge pertaining to Bullet Physics.

Aluthreney -- the King of sheep.

Advertisement

 /// Adding terrain mesh to scene.
ITexture *texture=driver->getTexture("./assets/map_image_resize.jpg");
ITerrainSceneNode *terrain=smgr->addTerrainSceneNode(texture->getName(), 0 , -1, vector3df(0.0f, 0.0f, 0.0f), vector3df(0.0f, 0.0f, 0.0f), vector3df(1.0f, 1.0f, 1.0f));
    
terrain->setMaterialFlag(EMF_LIGHTING, false);
terrain->setMaterialTexture(0, driver->getTexture("./assets/map_texture.jpg"));
terrain->setPosition(vector3df(0.0f, 0.0f, 0.0f));
terrain->setScale(vector3df(4.0f, 1.0f, 4.0f));

 /// Messing around with btHeightfieldTerrainShape.
vector3df scale=terrain->getScale();
aabbox3d<f32> box=terrain->getBoundingBox();
const vector3df size=box.getExtent()/scale;
f32 minHeight=((ITerrainSceneNode*)terrain)->getHeight(0, 0);
f32 maxHeight=minHeight;

int arraySize=texture->getSize().Height;
f32 heightData[arraySize*arraySize];
const f32 stepWidthX=size.X/arraySize;
const f32 stepWidthZ=size.Z/arraySize;
u32 runVal=0;

vector3df minEdge=box.MinEdge/scale;
vector3df maxEdge=box.MaxEdge/scale;

 //This cycle gives me the min and max height as well as the height value of each point of my terrain.
for(f32 z=minEdge.Z; z<maxEdge.Z; z+=stepWidthZ)
{
    for(f32 x=minEdge.X; x<maxEdge.X; x+=stepWidthX)
    {
        const f32 curVal=((ITerrainSceneNode*)terrain)->getHeight(x, z);

        heightData[runVal++]=curVal;

        if(curVal>maxHeight){maxHeight=curVal;}
        if(curVal<minHeight){minHeight=curVal;}
    }
}

f32 *heightDataPtr=heightData;

 /// Working with Bullet.
  // Collision Objects.
btCollisionShape *groundShape=new btHeightfieldTerrainShape(arraySize, arraySize, heightDataPtr, 1.f, minHeight, maxHeight, 1, PHY_FLOAT, false);
groundShape->setLocalScaling(btVector3(4.0f, 1.0f, 4.0f));
btCollisionShape *sphereShape= new btSphereShape(10);

  // Ground Motion State.
btTransform transform;
transform.setIdentity();
transform.setOrigin(btVector3(0.0f, 0.0f, 0.0f));
btDefaultMotionState *groundMotionState=new btDefaultMotionState(transform);

  // Ground Rigid Body.
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0));
btRigidBody *groundRigidBody=new btRigidBody(groundRigidBodyCI);
groundRigidBody->setUserPointer((void *)terrain);

  // Adding ground rigid body to the world.
m_dynamicsWorld->addRigidBody(groundRigidBody);

  // Sphere Motion State.
btDefaultMotionState *sphereMotionState=new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(100, 400, 100)));

  // Adding mass to the sphere object.
btScalar mass=1;
btVector3 sphereInertia(0, 0, 0);
sphereShape->calculateLocalInertia(mass, sphereInertia);

  // Sphere RigidBody.
btRigidBody::btRigidBodyConstructionInfo sphereRigidBodyCI(mass, sphereMotionState, sphereShape, sphereInertia);
btRigidBody *sphereRigidBody= new btRigidBody(sphereRigidBodyCI);

  // Creating irrlicht sphere.
ISceneNode *irrSphere=smgr->addSphereSceneNode(10);
irrSphere->setMaterialFlag(EMF_LIGHTING, false);
irrSphere->setMaterialTexture(0, driver->getTexture("./assets/ice_texture.jpg"));

sphereRigidBody->setUserPointer((void *)irrSphere);

  // Adding sphere rigid body to the world.
m_dynamicsWorld->addRigidBody(sphereRigidBody);

Aluthreney -- the King of sheep.

This topic is closed to new replies.

Advertisement