•      Sign In
• Create Account

## Player forward movement

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Medo3337  Members

891
Like
0Likes
Like

Posted 23 March 2013 - 08:46 AM

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?

#### Attached Thumbnails

Edited by Medo3337, 24 March 2013 - 04:50 AM.

### #2Jason Z  Members

6417
Like
0Likes
Like

Posted 23 March 2013 - 10:25 AM

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.

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

### #3Medo3337  Members

891
Like
0Likes
Like

Posted 23 March 2013 - 12:27 PM

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

### #4Jason Z  Members

6417
Like
0Likes
Like

Posted 23 March 2013 - 01:37 PM

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

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

### #5Medo3337  Members

891
Like
0Likes
Like

Posted 23 March 2013 - 02:22 PM

@Jason Z: I'm using Bullet Physics.

### #6Jason Z  Members

6417
Like
0Likes
Like

Posted 23 March 2013 - 02:43 PM

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

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.