Rotate DXSprite to cursor, frame of reference.

Started by
0 comments, last by JimmyDeemo 15 years, 5 months ago
I have a little demo running, and I’m trying to make a little top down shooter where the player sprite rotates to face the cursor (i.e. where to fire). I have the rotation calculations ok, but my problem is that how I’m positioning the DXSprite is not in the same frame of reference to the mouse position. Here's how I’m getting the cursor position;

void CInputHandler::UpdateMouseInfo(HWND hWnd, int screenWidth, int screenHeight)
{
	//Get the current position
	GetCursorPos(&m_mousePos);
	ScreenToClient( hWnd, &m_mousePos );

	//Center
	POINT center;
	center.x = screenWidth/2;
	center.y = screenHeight/2;

	m_mousePosFromCenter.x = m_mousePos.x - screenWidth/2;
	m_mousePosFromCenter.y = m_mousePos.y - screenHeight/2;
}


To position the sprite I’m using the D3DXSprite function SetTransform, and it works when both the sprite and the cursor are near the centre of the screen. But as we get further away the become out of sync and the sprite starts rotating around a point that’s not the cursor. I know it’s because an x value of 300 for the mouse is a different position to 300 x pos for the sprite. But I’m confused as to how i can alter the values. Is it because I’m using the D3DXSPRITE_OBJECTSPACE flag when drawing the sprite? I think i should probably be using pixels for the position, but I’m not sure how to do that. Any help would be much appreciated. [Edited by - JimmyDeemo on November 24, 2008 5:54:20 AM]
Advertisement
Suddenly thought that this might be better off in the DirectX section. If a mod comes across this and thinks so also. Please move it. Thanks.

Edit: I think what I’m looking for is the mouse position in object space. Or to draw the sprite using screen coordinates. I have tried it without the D3DXSPRITE_OBJECTSPACE flag and it doesn’t render anything. I think that’s because oh how my camera is set up. Perhaps my problem is that i modified this from a 3D demo i had and its a bit silly doing something in 2d using a 3d methods i guess.

[Edited by - JimmyDeemo on November 25, 2008 3:21:17 AM]

This topic is closed to new replies.

Advertisement