Sign in to follow this  
waterboy4800

2D Mouse Aim Rotation Help

Recommended Posts

waterboy4800    122
Making a 2d game and trying to rotate a gun in our characters hand based on the position of the mouse. He should rotate in a complete circle. What i tried doing was taking the new and old mouse pos and the players position and try to find the angle of difference but it did not produce the intended results. Any ideas on how to do this? Thanks Here is the code i had so far.
D3DXVECTOR3 A = g_pMouse->GetPos(); // Old Mouse Position
g_pMouse->Update();
D3DXVECTOR3 B = g_pMouse->GetPos();	 //New Mouse Position

static float angle = 0;

	//Get Difference in angle between new and old mouse position (A & B)
	if (A != B)
	{		
		D3DXVECTOR3 C;
		
		D3DVIEWPORT9 viewport;
		pd3dDevice->GetViewport(&viewport);
		D3DXMATRIX proj, view, world;
		pd3dDevice->GetTransform(D3DTS_WORLD, &world);
		pd3dDevice->GetTransform(D3DTS_VIEW, &view);
		pd3dDevice->GetTransform(D3DTS_PROJECTION, &proj);

            //Get Player Position and convert to screen cordinates (VECTOR C)
		D3DXVec3Project(	&C,
							&g_player.getPos(),
							&viewport,
							&proj,
							&view,
							&world
						);

		float a, b, c, dx, dy;
		dx = ( B.x - C.x );
		dy = ( B.y - C.y );
		a  = sqrt( dx*dx + dy*dy );
		dx = ( A.x - C.x );
		dy = ( A.y - C.y );
		b  = sqrt( dx*dx + dy*dy );
		dx = ( B.x - A.x );
		dy = ( B.y - A.y );
		c  = sqrt(dx*dx + dy*dy);	
		float dir = 1;
		if (A.y > B.y)
			dir = -1;

		angle = (acos ( ( a*a + b*b - c*c ) / (2*a*b) ));
      }

g_player.update( fTime, fElapsedTime, angle );   




Share this post


Link to post
Share on other sites
waterboy4800    122
Hey there, just had some time to test it out and got better results, but its not senstive enough, if i circle my charater with the cursor id like the gun to rotate a complete 360. Right now it doesnt seem posible to rotate it 360 degrees based on mouse movements unless i go way way way of the screen on an angle.

Thanks for the help so far!



D3DXVECTOR3 mouse = g_pMouse->GetPos(); //New Mouse Position

D3DXVECTOR3 arm;

D3DVIEWPORT9 viewport;
pd3dDevice->GetViewport(&viewport);
D3DXMATRIX proj, view, world;
pd3dDevice->GetTransform(D3DTS_WORLD, &world);
pd3dDevice->GetTransform(D3DTS_VIEW, &view);
pd3dDevice->GetTransform(D3DTS_PROJECTION, &proj);

D3DXVec3Project( &arm,
&g_player.getPos(),
&viewport,
&proj,
&view,
&world
);

float angle = atan2(mouse.y - arm.y, mouse.x - arm.x);

g_player.update( fTime, fElapsedTime, angle );
[/soruce]

Share this post


Link to post
Share on other sites
Quote:
Original post by waterboy4800
Hey there, just had some time to test it out and got better results, but its not senstive enough, if i circle my charater with the cursor id like the gun to rotate a complete 360. Right now it doesnt seem posible to rotate it 360 degrees based on mouse movements unless i go way way way of the screen on an angle.

Thanks for the help so far!


*** Source Snippet Removed ***


Hmm, that's odd. Try printing out the angle onscreen (it'll be a good idea to convert to degrees first) and estimate if the angle value is correct. If so, I would guess the actual rotation of the arm is the problem?

Share this post


Link to post
Share on other sites

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