Sign in to follow this  
Medo Mex

Player forward movement

Recommended Posts

I'm moving the player FORWARD, but I have a slight problem, 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?

Edited by Medo3337

Share this post


Link to post
Share on other sites

Once you have your newX, newY, and newZ values, you have to check them against the height of the terrain and update the newY value accordingly.  Make sure you do the correction immediately, before you generate the new transformation matrices.

 

Note that this will help with the current problem that you have, but the root of the issue is that your forward vector should be modified according to the slope of the terrain.  This will also help you with the relative ground speed seeming different between flat ground and sloped ground.

Share this post


Link to post
Share on other sites

@Medo3337: Then how do you propose would be more efficient?  You have to be able to know what elevation your camera should be at, so if it is testing against a mesh or the terrain, or both, then it is going to take some processing time.

 

The only time you wouldn't do this type of testing is if you had a physics API that was managing the height for you, and automatically taking into account the meshes and terrain around you.  I'm not really sure why you would think this is inefficient...

Share this post


Link to post
Share on other sites

@Jason Z: I'm using Bullet Physics.

Oh - well that is a very relevant piece of information!  In that case, you probably need to find an expert in Bullet Physics, and you should probably move this post to an appropriate sub-forum...  I don't think your question is with respect to DirectX - it is more of a physics API question.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this