Player Falling on Terrain Heights Instead of Moving

Started by
2 comments, last by Steve_Segreto 11 years, 1 month ago

When the player move on the terrain heights (mountain), the player fall on the terrain instead of moving normally, which make the player appear like they are able to fly, see the attached picture.

Here is the code that I'm using for moving forward:


D3DXMATRIX matRot;
D3DXMatrixRotationYawPitchRoll(&matRot, D3DXToRadian(rotationX), D3DXToRadian(rotationY), D3DXToRadian(rotationZ));

D3DXMATRIX ZUnitVec;
D3DXVECTOR3 vecForward;
D3DXMatrixTranslation(&ZUnitVec, 0.0f, 0.0f, 1.0f);  // Matrix holding position of vector pointing to positive Z
D3DXMATRIX m = ZUnitVec * matRot;
vecForward = D3DXVECTOR3(m._41,m._42,m._43); // Get vector from the matrix

newX -= (elapsedTime * speed) * vecForward.x;
if (rotationY != 1.0f)
    newY -= (elapsedTime * speed) * vecForward.y;
newZ -= (elapsedTime * speed) * vecForward.z;

How do I modify the above code to make the player move on the terrain normally, without flying?

I'm using Bullet Physics.

Advertisement

Yep, that's typically what it looks like when you just move an object in two dimensions. In your case it appears like the player object moves along the (x,z) plane.

Try incorporating an alternative up vector besides (0,1,0) into your calculations, so the first push isn't straight away from the slope, but rather along the slope, then there will be less "falling", "skipping" or "flying" behavior. :)

Tried to change the line:


D3DXMatrixTranslation(&ZUnitVec, 0.0f, 0.0f, 1.0f);

To:


D3DXMatrixTranslation(&ZUnitVec, 0.0f, 1.0f, 1.0f);

Solved the problem, but that doesn't seem to work correctly with other meshes (anything other than characters), should I use a different code for other meshes?

By other meshes I assume you mean horses and vehicles, which are longer/wider than they are tall?

Basically you have to work at the four corners of the oriented bounding box and find the up vector based on that. Reimer Grootjans covers the algorithm in one of his XNA programming recipes.

http://shiba.hpe.sh.cn/jiaoyanzu/WULI/soft/XnaRecipes/XNARecipes.pdf

Section 4-17.

Correctly Tilt a Model Corresponding to the

Terrain Underneath

This topic is closed to new replies.

Advertisement