Sign in to follow this  

Gun following camera

This topic is 4337 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 am trying to get my gun to follow my first-person shooter cam in DX9. First, I'll explain what's happening and then next I'll show some code. Well, what's going on is that the gun follows the camera rotating fine, _but_ I only see the barrel of the gun and nothing else. Here's the code: Here I'm setting eye point to player's position and then lookat to the rotation angle of the player. This works fine....
	D3DXVECTOR3 eyePt = m_player.GetPos();
	D3DXVECTOR3 lookatPt = D3DXVECTOR3( sinf( m_player.GetAngle() ) + eyePt.x, eyePt.y, cosf( m_player.GetAngle() ) + eyePt.z );
	D3DXVECTOR3 upVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );



Now, to have the gun follow I am doing this:
	m_weapon.SetAngle( m_player.GetAngle() );

	D3DXVECTOR3 gunPos = lookatPt;
	gunPos.y -= 0.5f;    // y offset
       // gunPos.z += 1.0f;  // z offset to bring gun into view...eek!  gun no longer follows player rotating the camera
	m_weapon.SetPos( gunPos );
	m_weapon.Render();



With this code, the gun follows along as I rotate the camera, _however_ I can only see the beginning of the gun and nothing else. So I thought I would put a gunPos.z += 1.0f; in there to move the gun more into sight. NOW I see all of the gun but it no longer follows when I rotate. Maybe it's getting late and I'm tired, but I can't figure this out :) Any help is appreciated. Thanks! PS -- If you need more info, let me know or if you have a completely easier way to do this, I'd be glad. Also, setting gunPos = lookatPt is something I really didn't think about too much, but I think it's right...

Share this post


Link to post
Share on other sites
I didn't spend too much time looking at your code, but here are a few thoughts. What is your near clip plane? Part of the problem might be that your gun is too close to the camera and is getting clipped. Also, it looks like you're just adding z. That would only move the camera forward in camera space if the camera were looking down the Z axis. If the camera is looking elsewhere, like down the x axis, that might move the gun left or right. You may also be having problems with the line that moves the camera down along the y axis if the camera is not looking "level". If you angle up or down, then you may end up moving the camera forward or back relative to the camera.

What you want to do is take the transformations that you want to apply to the camera, like down and forward, and then multiply the vector by the rotation matrix for your camera. That's my best guess as to what's going on.

--Vic--

Share this post


Link to post
Share on other sites
After looking at the code snippets, here is what I guess you are doing:

The eye position, i.e. camera position in world space, is set as the player's position.
The lookat position, i.e. camera relative position to world space, is set as some rotation and translation from eye position.
The weapon position is some further translation from the lookat position.

I don't know how to write out and explain clearly but here is my attempt.

If you sure the above is what you want and want to stick with this way, you are setting the world and view matrix in D3D in correctly.

Or, if you do setting the world and view matrix in D3D correctly, then you need to rewrite your orientation setting for the weapon. And I think your lookat position's rotation is not correct and need some inspection as well.

Share this post


Link to post
Share on other sites
Woot, I got it! Thanks for the response. What I ended up doing was in my rendering, I simply moved the camera a little by:


// set up the 3D arena camera
D3DXVECTOR3 eyePt = m_player.GetPos();
eyePt.z -= 1.8f * cosf( m_player.GetAngle() ); // added
eyePt.x -= 1.65f * sinf( m_player.GetAngle() ); // added
D3DXVECTOR3 lookatPt = D3DXVECTOR3( sinf( m_player.GetAngle() ) + eyePt.x, eyePt.y, cosf( m_player.GetAngle() ) + eyePt.z );
D3DXVECTOR3 upVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
CCamera::Instance()->Setup3DCamera( m_displayDevice, eyePt, lookatPt, upVec, 0.5f, 1000.0f ); // modified near clip plane to 0.5f





Works well! Hope this helps someone.

NOTE -- adjusting the eyePt.z and .x to different values (1.85, 1.65, respectively), looks weird, but what happens is that when I rotate, the gun follows fine, but it bobs a bit back and forth, which feels really nice. If I had them adjusted the same, it's pretty much perfect gun rotation.

[Edited by - mcguile25 on January 27, 2006 11:21:21 AM]

Share this post


Link to post
Share on other sites

This topic is 4337 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.

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