generating havok physic heightfield
alright. i am managed to almost. really almost. i still have slight difference in position of my generated height map from directx and the height which i supply to havok heightfield. i know this is my mistake.
ok this is what i did.
first. of course you need the raw file. the script to generate from it and display it.
you must have this kind of line on your program
// Allocate memory and read the data
m_pHeight = new UCHAR[m_numVertices];
heightStream.read( (char *)m_pHeight, m_numVertices );
heightStream.close();
and m_pHeight is UCHAR, means from 0 to 255. any other data type is fine. i think… so that m_pHeight is the one that i supplied to havok. as of the terrain, i wrote this new method of the terrain class. this will fill a variable member that has hkReal data storage. and it is copied from that UCHAR thing. i think they are the same. but i had some problem just by passing only the UCHAR, so i wrote this method. and it works like a
void CTerrain::initHKHeightFields(){
m_heightDatas = hkAllocate<hkReal>(m_numVertices, HK_MEMORY_CLASS_DEMO);
D3DXMATRIX tranform = m_scale * m_rotate * m_translate; ;
Vertex *tVertices = NULL;
m_vb.getVB()->Lock(0,sizeof( PositionTextured ), (void**) & tVertices,0 );
{
for (UINT j = 0; j < m_width; j++) {
for(UINT k = 0; k < m_width; k++){
float height = (
tVertices[j*m_width+k].m_x * tranform._12 +
tVertices[j*m_width+k].m_y * tranform._22 +
tVertices[j*m_width+k].m_z * tranform._32 +
1 * tranform._42);
//m_heightDatas[j*m_width+k] = height ;
m_heightDatas[j*m_width+k] = (hkReal)m_pHeight[j*m_width+k];
}
}
}
// unlock the buffer as we're done working with the vertices
m_vb.getVB()->Unlock();
}
and because i am following the sample, of course i have a class that get the data from the terrain, and supply it to the heightfield physic class
class MySampledHeightFieldShape: public hkpSampledHeightFieldShape
{
public:
MySampledHeightFieldShape( const hkpSampledHeightFieldBaseCinfo& ci, hkReal* datas )
: hkpSampledHeightFieldShape(ci),
m_datas(datas)
{
}
// Generate a rough terrain
HK_FORCE_INLINE hkReal getHeightAtImpl( int x, int z ) const
{
hkReal a = m_datas[x * m_zRes + z];
return a;
}
// This should return true if the two triangles share the edge p00-p11
// otherwise it should return false if the triangles share the edge p01-p10
HK_FORCE_INLINE hkBool getTriangleFlipImpl() const
{
return false;
}
virtual void collideSpheres( const CollideSpheresInput& input, SphereCollisionOutput* outputArray) const
{
hkSampledHeightFieldShape_collideSpheres(*this, input, outputArray);
}
private:
hkReal* m_datas;
};
this class had a little modification from the original source. they are the member variable, and getHeightAtImpl() method. hmm looks terrible.
next i wrote these lines by following the sample; this will create the physics that needed for the simulation.
hkpSampledHeightFieldBaseCinfo ci;
ci.m_xRes = m_terrain.getWidth();
ci.m_zRes = m_terrain.getWidth();
ci.m_maxHeight = 600.0f;
ci.m_minHeight = 0.0f;
ci.m_useProjectionBasedHeight = true;
ci.m_scale= hkVector4(
m_terrain.GetScaling()->_11,
m_terrain.GetScaling()->_22 ,
m_terrain.GetScaling()->_33
);
//initialize heightmap data in m_terrain CTerrain
m_terrain.initHKHeightFields();
MySampledHeightFieldShape* heightFieldShape = new MySampledHeightFieldShape( ci , m_terrain.getHKHeightFields() );
{
hkpRigidBodyCinfo rci;
rci.m_motionType = hkpMotion::MOTION_FIXED;
rci.m_shape = heightFieldShape;
rci.m_friction = 0.3f;
rci.m_position.set(
m_terrain.getTopLeft().x,
m_terrain.GetPosition().y,
m_terrain.getTopLeft().z
);
hkpRigidBody* body = new hkpRigidBody( rci );
mHavokWorld->addEntity(body);
body->removeReference();
// Just need to remove the reference we hold to the shape, and that is it.
heightFieldShape->removeReference();
}
//height map rigid////////
i spent a lot of time fixing the position and matching the position of the terrain and the height field.
the terrain and the heightfield physic are not sync and on different position
what i had to do is to create a simple terrain raw file. make sure they have different thing on the top left and right. and then i found out that i need to rotate my terrain 90 degree clockwise to match with the physic height field. what you can do beside that is of course, rotate the physic height field instead of the terrain. even that i still have to tweak a bit. and still i have left with no luck. but i m still working on it. but my assessment is due tomorrow. so i will submit it with this horrible bug. next… hmm i think that s all.. as of the vehicle i mentioned above in some of the posts.. they are on the same position. again, they are not on the same direction. so i had to modify my model to face 90 degree clock wise to match the physic. just want somebody who read this post aware of this small thing.
here is the original thread at havok forum





