2D Mouse Aim Rotation Help

Started by
4 comments, last by load_bitmap_file 18 years, 4 months ago
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 );   




Advertisement
You might want to try using atan2() instead of your code for figuring the proper facing angle.
theta = atan2(mouseY - armY, mouseX - armX)
thats it? lol.. thanks ill try tomorrow
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]
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?

This topic is closed to new replies.

Advertisement