Public Group

# FPS player rotations

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

## Recommended Posts

I'm working on a FPS and am having issues making my gun/arms rotate with my camera. Right now I'm just rendering a MD5 gun and arms model to the camera position and offsetting it's position a little so it's more to the right side of the screen. The MD5 gun had a strange orientation when I first loaded it in, so I did some rotations on it to make it render in the correct position. I'm just having issues figuring out how to rotate it with the camera now. Any help would be appreciated. Also for some reason the model doesn't scale at all when I try to add a scale matrix.

EDIT: using DirectX10

 void CPlayer::Update(D3DXVECTOR3 currentPos, D3DXVECTOR3 currentLook, D3DXVECTOR3 currentUp, D3DXVECTOR3 currentRight, D3DXMATRIX world, D3DXMATRIX view, D3DXMATRIX proj) { m_Pos = currentPos; //camera position m_Look = currentLook; //camera look m_Right = currentRight; //camera right m_Up = currentUp; //camera up //initialize world project matrix to the world matrix m_WVP = world; //rotations to put the model in the right position since it is //offset when loaded in. D3DXMatrixRotationX(&m_playerRotX, 1.57079633); D3DXMatrixRotationY(&m_playerRotY, -1.57079633); D3DXMATRIX playerScale; D3DXMatrixScaling(&playerScale, 0.05f, 0.05, 0.05f); //move the gun with the camera D3DXMatrixTranslation(&m_playerTrans, m_Pos.x, m_Pos.y-0.1, m_Pos.z+0.5); m_playerTrans = playerScale * m_playerRotX * m_playerRotY * m_playerTrans; //finish setting up the world view projection matrix for the shader D3DXMatrixMultiply(&m_WVP, &m_WVP, &m_playerTrans); //D3DXMatrixInverse(&view, NULL, &view); D3DXMatrixMultiply(&m_WVP, &m_WVP, &view); D3DXMatrixMultiply(&m_WVP, &m_WVP, &proj); } 

##### Share on other sites
Ok, I've been working on this issue and I can now make the camera yaw perfectly. However when I add a pitch so the gun can be aimed up and down, the transform messes up slightly. When you aim up the gun rotates in a circular counter-clockwise fashion, instead of just pitching up.

 void CPlayer::Draw(ID3D10Device* device, D3DXVECTOR3 currentPos, D3DXVECTOR3 currentLook, D3DXVECTOR3 currentUp, D3DXVECTOR3 currentRight, D3DXMATRIX world, D3DXMATRIX view, D3DXMATRIX proj) { m_Pos = currentPos; //camera position m_Look = currentLook; //camera look m_Right = currentRight; //camera right m_Up = currentUp; //camera up ////initialize world project matrix to the world matrix m_WVP = world; //set the players gun angles by adding the offset + angle //The offset represents the amount the model was rotated in order //to correct the oreintation after loading it in. D3DXMatrixRotationX(&m_playerRotX, 1.57079633 + m_gunUpAng); D3DXMatrixRotationY(&m_playerRotY, -1.57079633 + m_gunAng); D3DXMATRIX playerScale; D3DXMatrixScaling(&playerScale, 0.05f, 0.05, 0.05f); //move the gun with the camera D3DXMatrixTranslation(&m_playerTrans, m_Pos.x, m_Pos.y, m_Pos.z); m_playerTrans = playerScale * m_playerRotX * m_playerRotY * m_playerTrans; //finish setting up the world view projection matrix for the shader D3DXMatrixMultiply(&m_WVP, &m_WVP, &m_playerTrans); D3DXMatrixMultiply(&m_WVP, &m_WVP, &view); D3DXMatrixMultiply(&m_WVP, &m_WVP, &proj); m_ml.Render(device, m_WVP); } 

This is just the input to change the gun angles with the camera.
 GetCamera().pitch(p.y * (.005f)); player.m_gunUpAng += (p.y * (.005f)); GetCamera().rotateY(p.x * (.005f)); player.m_gunAng += (p.x * (.005f)); 

In the below screenshot it shows what happens when you try to pitch the model. If i would keep trying to pitch the gun up it would eventually circle all the way back around to it's starting position.

##### Share on other sites
It looks like you are rotating the gun around the wrong axis (the z axis). Since you have to turn your model by 90 degrees on one axis and 90 degrees on another axis that might be the problem. Can you either edit or load the model for the gun in such a way that it starts out pointing the same direction as your camera?

##### Share on other sites
Your camera has a local-to-world matrix. You can make the camera to be the parent of the first-person (weapon, arms, etc) model. Then you only set local-to-parent matrix for the weapon. This way the weapon will automatically follow the camera. When you multiply the child and parent transformations, you get the local-to-world for the weapon.

This is the usual approach for 3d transformations.

fake edit: Camera's local space is the view-space, so it's local-to-world transformation can be said to be view-to-world transformation.

##### Share on other sites

It looks like you are rotating the gun around the wrong axis (the z axis). Since you have to turn your model by 90 degrees on one axis and 90 degrees on another axis that might be the problem. Can you either edit or load the model for the gun in such a way that it starts out pointing the same direction as your camera?

Yes I came to a similar conclusion, however if I can help it I would rather not modify the .md5mesh file itself. I'd have to find a exporter and importer for MD5 and then it would most likely cause problems for the mesh animations. If there is a way to keep those changes in code and get it to work I'd rather do that.

Your camera has a local-to-world matrix. You can make the camera to be the parent of the first-person (weapon, arms, etc) model. Then you only set local-to-parent matrix for the weapon. This way the weapon will automatically follow the camera. When you multiply the child and parent transformations, you get the local-to-world for the weapon.

This is the usual approach for 3d transformations.

fake edit: Camera's local space is the view-space, so it's local-to-world transformation can be said to be view-to-world transformation. [/quote]
I didn't really understand how to implement this. I didn't really get the parts on setting the local-to-parent matrix for the weapon. Would it allow me to make the rotation work on the correct access like was explained in the post above?

##### Share on other sites

I didn't really understand how to implement this. I didn't really get the parts on setting the local-to-parent matrix for the weapon. Would it allow me to make the rotation work on the correct access like was explained in the post above?

Every object has a local space. Object-space, view-space, light-space. These are all depending of the type of item in the world. World space is a "null" item space, where every other item is a child. So all items has local-to-parent transformation, and if the parent is the world, then it's the same as local-to-world. You can create an hierarchy of item transformations, where each item holds only local-to-parent transformation. Now, when you want to know the local-to-world transformation for a specific item, you just multiply all matrices to the root (the world). End result is, that if you transform an item, then all its children automatically follows. Having items as parent-child forms a tree which you see in all 3D modelling software. It simplifies the transformations, and gives many optimization opportunities.

Matrix multiplication combines the transformations. I don't have any links to provide, but I'm sure there's a bunch of material to be found. Try "hierarchical transformations" or such.

##### Share on other sites
thanks, I'll be looking that up. Like i was saying above when I load in the MD5 model it is offset and I have to do some rotations on it in order to make it sit in the right position before I can make it follow the camera. Will using a hierarchical transformation structure allow me to make the gun follow the camera correctly even after doing the rotations to account for the gun offset? When I remove the offset from the model... it renders and moves with the camera perfect. Although the model is in the wrong position. I basically just want a way to avoid importing the .md5mesh model file and moving it in order to get rid of the offset in the code.

##### Share on other sites

Like i was saying above when I load in the MD5 model it is offset and I have to do some rotations on it in order to make it sit in the right position before I can make it follow the camera. Will using a hierarchical transformation structure allow me to make the gun follow the camera correctly even after doing the rotations to account for the gun offset? When I remove the offset from the model... it renders and moves with the camera perfect. Although the model is in the wrong position. I basically just want a way to avoid importing the .md5mesh model file and moving it in order to get rid of the offset in the code.

I don't know anything about MD5. I'm sure fellows here will talk about it. I know about .3DS which has axises in a bit weird right handed orientation, and transforming to D3D left handed is some swaps all around the data. I think you're going the hard way when manually doing this.

Will using a hierarchical transformation structure allow me to make the gun follow the camera correctly even after doing the rotations to account for the gun offset?

Yes.

I'm drunk atm, please someone else carry on, explain this better, shoot me down, or give better approach.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633665
• Total Posts
3013241
×