Jump to content
  • Advertisement
Sign in to follow this  
Medo Mex

Adjusting Weapon In Front of The Camera

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to adjust a weapon infront of FPS camera, I tried to set the gun mesh position to appear infront of the camera, but I couldn't get the gun to rotate correctly with the camera.

 

// Code to get the screen position in the 3D world 'screenOrigin'
gun->setPosition(screenPosition.x, screenPosition.y, screenPosition.z - 1.0f);
gun->lookAt(camera->position - camera->look);

How do I make the weapon rotate correctly with the camera?

 

Share this post


Link to post
Share on other sites
Advertisement

I figured out that I could set the view and projection matrix to identity, now I have two problems:

 

I'm trying to make the weapon appear alittle bit from the side, the following code works to get the gun infront of the camera but when the change the rotation, sometimes I don't get to see the gun or I see a part of it.

D3DXMATRIX matView, matCustom;
D3DXMatrixIdentity(&matView);
m_d3dDevice->SetTransform(D3DTS_PROJECTION, &matView);

D3DXMatrixTranslation(&matCustom, 0.0f, 0.0f, 8.0f);
D3DXMatrixRotationY(&matCustom, D3DXToRadian(290.0));
D3DXMatrixMultiply(&matView, &matView, &matCustom);
device->SetTransform(D3DTS_VIEW, &matView);

Another problem is that the gun is intersecting the terrain and other meshes.

Edited by Medo3337

Share this post


Link to post
Share on other sites

When you multiply some matrix with identity matrix you get the same matrix, so there is no point in doing that.

 

To make your gun follow camera, to act as in FPS games, try this:

1. You model your gun so that it is at origin and pointing forward, and when export make sure that it is in coordinate system that you use in your directx project (usually left handed).

 

gun.jpg

 

2.

const D3DXMATRIX& camView = ActiveCamera->View; // whaterver is your method to obtain view matrix
    D3DXMATRIX viewInverse;
    D3DXMatrixInverse(&viewInverse, NULL, &camView);

    D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); // rotation axis
    D3DXMATRIX gunWorld, gunOffTransl, gunOffRot;
    D3DXMatrixTranslation(&gunOffTransl, 1.0f, -1.0f, 3.0f); // set acording to your visual pleasing
    D3DXMatrixRotationAxis(&gunOffRot, &up, D3DXToRadian(-12.0f)); // set acording to your visual pleasing
    gunWorld = gunOffRot * gunOffTransl * viewInverse;
    d3d9device->SetTransform(D3DTS_WORLD, &gunWorld);
    GunMesh->DrawSubset(0);

 

If you wold set your "gunWorld" as "viewInverse" it will follow camera and point straight forward, so we add "offset" matrices to move/rotate it a bit so it doesn't cover whole screen.

 

3.

Another problem is that the gun is intersecting the terrain and other meshes.

 

When you model your meshes take care of proportion. How many units is 1 meter? It is up to you how/why did you decide this. 

So if gun is intersecting terrain you have 2 choices, in modeling program scale your gun down or scale your terrain up and move camera a bit up from ground.

Share this post


Link to post
Share on other sites

The player could be holding a long sniper rifle, which WILL intersect other meshes (for example: it will intersect the walls, doors, etc...).

Share this post


Link to post
Share on other sites

It is up to you how to interact on those circumstance. Picture yourself in reality holding a long sniper rifle approaching the wall. What would you do?

Share this post


Link to post
Share on other sites

Use identity for view, but keep the projection matrix.

 

But if you want the depth. Just use the player transform matrix

 

GunWorldTransf = PlayerWorld * GunOffsetMatrix;

 

 

 gunWorld = gunOffRot * gunOffTransl * viewInverse;

 

Do not multiply viewInverse... just set it as a D3DTS_VIEW;

Edited by imoogiBG

Share this post


Link to post
Share on other sites

Some FPS games I have seen use a separate layer for the gun mesh so it is always drawn on top of the other scenery. One way I can think of doing that is by clearing the z-buffer before drawing the gun layer, probably an easier way to do that though (could render to a separate target as well and composite it on the image after the world has been drawn).

Share this post


Link to post
Share on other sites

Some FPS games I have seen use a separate layer for the gun mesh so it is always drawn on top of the other scenery. One way I can think of doing that is by clearing the z-buffer before drawing the gun layer, probably an easier way to do that though (could render to a separate target as well and composite it on the image after the world has been drawn).

Or just set D3DRS_ZENABLE to false smile.png

Edited by imoogiBG

Share this post


Link to post
Share on other sites
1. scale gun.
2. rotate gun relative to camera/player. (IE pointing forward, up, over the shoulder, down at rest, etc.)
3. translate gun relative to camera/player (IE if the camera is at the player's head, you want the gun down, forward, and right, or left for left handed characters).
4. rotate the gun by the player/camera's rotation matrix
5. translate the gun by the player/camera's translation matrix
6. draw.
 
things like long rifles sticking into walls should be handled as a unit having a larger collision radius or bounding box when that weapon is drawn and ready. or you can make the character automatically draw the weapon to their chest if they're too close to the wall. this is what one does in the real world. go outside and try it with a stick.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!