Collision at incorrect height between boxes and btHeightfiel

khanhhh89
Posts: 2
Joined: Thu May 08, 2014 4:04 pm

Collision at incorrect height between boxes and btHeightfiel

Post by khanhhh89 »

I'm using btHeightfieldTerrainShape from BulletPhysics for collision between fired boxes and a terrain. But such boxes seem to collides at higher positions above the terrain as the following picture.


The following is the code for creating HeightField in Bullet

Code: Select all

    //Terrain information
    //width: 2049
    //height: 2049
    //pHeightData: scaled values in [-24.2, 24.2]
    //minHeight: -24.2
    //maxHeight: 24.2
    //upIndex: 1 (y axis)
    btHeightfieldTerrainShape*
		heightFieldShape = new btHeightfieldTerrainShape(
					width, height,
					pHeightData, 1.0f,
					minHeight, maxHeight,
					upIndex, PHY_ScalarType::PHY_FLOAT, flipQuadEdges);
    
    //Scale nothing
    btVector3 localScaling(1, 1, 1);
    localScaling[upIndex] = 1.0f;
    m_pGroundShape->setLocalScaling(localScaling);
    
    //create terrain object
    btTransform tr;
    tr.setIdentity();
    tr.setOrigin(btVector3(0, 0, 0));
    btRigidBody* pBody = LocalCreateRigidBody(0, tr, m_pGroundShape);
For the rendering part, I use tessellation based on the Hull and Domain shader in DirectX11. THe terrain is located at the origin in the world space, and the height data is as exact as the one I passed to btHeightfieldTerrainShape.

1.Vertex shader

Code: Select all

output.positionW   = input.positionL;
2.Hull Shader

Code: Select all

//e0,e1, ... are the centeres of each edge of the patch
    float3 e0 = 0.5f*(patch[0].positionW + patch[2].positionW);
    float3 e1 = 0.5f*(patch[0].positionW + patch[1].positionW);
    float3 e2 = 0.5f*(patch[1].positionW + patch[3].positionW);
    float3 e3 = 0.5f*(patch[2].positionW + patch[3].positionW);
    float3  c = 0.25f*(patch[0].positionW + patch[1].positionW + patch[2].positionW + patch[3].positionW);

	output.EdgeTess[0] = CalcTessFactor(e0);
	output.EdgeTess[1] = CalcTessFactor(e1);
	output.EdgeTess[2] = CalcTessFactor(e2);
	output.EdgeTess[3] = CalcTessFactor(e3);

	output.InsideTess[0] = CalcTessFactor(c);
	output.InsideTess[1] = output.InsideTess[0];
3.Domain Shader

Code: Select all

  //After tesslating vertecies from Vertex Shader
    //I used such vertices to get the height data.	
    dout.positionW = lerp(
		lerp(quad[0].positionW, quad[1].positionW, uv.x),
		lerp(quad[2].positionW, quad[3].positionW, uv.x),
		uv.y);
    dout.positionW.y = gTexHeightMap.SampleLevel(gSmpHeightMap, dout.texCoord, 0).r;
I have spent almost two days but couldn't find out the error. I hope to see your opinion about it. Thanks so much.
khanhhh89
Posts: 2
Joined: Thu May 08, 2014 4:04 pm

Re: Collision at incorrect height between boxes and btHeight

Post by khanhhh89 »

I have found out that the boxes collides with the terrain at both lower and higher position. In addition, I also think that if the problem is due to the height scale, all of the collision positions only should be higher or lower to the terrain. Therefore, the height scale may be not the reason.

Is there any possibility that the order in which the function

Code: Select all

ProcessAllTriangle 
in

Code: Select all

btHeightfieldTerrainShape 
sample the height for creating triangle for collision detection is different from the order I sample HeightMap in the Domain shader?

This is in ProcessAllTriangle:

Code: Select all

val = m_heightfieldDataFloat[(y*m_heightStickWidth)+x];
And this is in my domain shader code:

Code: Select all

	dout.texCoord = lerp(
		lerp(quad[0].texCoord, quad[1].texCoord, uv.x),
		lerp(quad[2].texCoord, quad[3].texCoord, uv.x), 
		uv.y);
	
        // Displacement mapping
	dout.positionW.y = gTexHeightMap.SampleLevel(gSmpHeightMap, dout.texCoord, 0).r;
Where texture coordinates is generated in C++ as following:

Code: Select all

 //m_NumPatchVertCols  is the number of patch in the column direction

        //du, dv is the length of each patch in height map texture scale.
	float du = 1.0f / (m_NumPatchVertCols - 1);
	float dv = 1.0f / (m_NumPatchVertRows - 1);

        for (UINT i = 0; i < m_NumPatchVertRows; ++i)
	{
            for (UINT j = 0; j < m_NumPatchVertCols; ++j)
            {
                  vertex.texcoords = Vector2f(j*du, i*dv);
            }
        }
It seems that Bullet and I sample the height map in the same way.
What could be the reason?
Thyrion
Posts: 6
Joined: Sun Mar 09, 2014 1:55 pm

Re: Collision at incorrect height between boxes and btHeight

Post by Thyrion »

i think u can only be sure the bullet terrain mesh fits your rendered terrain mesh, if u debug draw the bullet terrain...

seems it doesnt