# Player forward movement

This topic is 2035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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;

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 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 on other sites

@Jason Z: I think this is not efficient, the heights doesn't necessarily have to be terrain, it could be any mesh.

##### 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 on other sites

@Jason Z: I'm using Bullet Physics.

##### 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.

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009398
• ### Who's Online (See full list)

There are no registered users currently online

×