Hi I am making a top-down 2D shooter in D3D.

So what I want to do is have my character rotate towards my mouse icons. So that means there are 2 vectors involved, pos and mousepos.

Now calculating the angle wasn't a problem but the problem comes when I try to rotate around my sprite towards the angle.

It rotates towards the right direction but when the sprite rotates it isn't stuck at it's position.

The center at which it is being rotated around looks to be the actual position, and this goes hand in hand with where it initially is drawn which also is wrong.

Lets say I paint it out at (10,10) (x,y) in pixels. Then I move it to (10,0). It is now outside of the picture but when I rotate it 180 degrees it is painted slightly below (10,0).

So I'm thinking that it is a logical error with my drawing function and would appreciate any additional insight.

This is how my drawing function looks like atm:

float	fix_x = 0.5f;
float  fix_y = fix_x;

Matrix transform, scaling, rotation, translation;
D3DXMatrixIdentity(& transform);

// fix for rotation
D3DXMatrixTranslation(& translation, -fix_x, -fix_y, 0);
transform		= translation;

D3DXMatrixScaling(& scaling, Surf->getScaleRateX(), Surf->getScaleRateY(), 1.0f);

D3DXMatrixRotationZ(& rotation, D3DXToRadian(Surf->getRotationAngle() ));

transform	= transform * scaling * rotation;

// move back after rotation
D3DXMatrixTranslation(& translation, +fix_x, +fix_y, 0);
transform	= transform * translation;

// Translation
Vector2D	hotspot = Surf->getHotspot(),
		position = Surf->getPosition();

float		x_Adjust = position.x,
		y_Adjust = position.y;

D3DXMatrixTranslation(& translation, x_Adjust, y_Adjust, 0);

transform		= transform * translation;

hr = m_Sprite->SetTransform(& transform);

if(FAILED(hr)  )
{
	m_lastError = hr;
	return false;
}

hr = m_Sprite->Draw(	Surf->getTexture(),
			PartOfPicture,
			& Surf->getHotspot(),
			NULL,
			D3DCOLOR_ARGB(Alpha, R, G, B)  );

if(FAILED(hr)  )
{
	m_lastError = hr;
	return false;
}

return true;

Okay so after some time of searching around now I'm stumped.

I create the texture with D3DX_DEFAULT_NONPOW2 (besides the point since my sprite is 32x32 either way) but still it won't rotate around my center.

I tried different ways of doing the Matrix such as D3DXTransformation2D function aswell as moving it before rotation/scaling and moving it back to finally move it where it belongs (the one you see now).

I also tried normal way of just straight off doing all of it at the same time, like this: transform = scale * rotation * translation.

Anyone who has a clue as to what I'm doing wrong?

Is there nobody here who can share me with their glimpse of wisdom? Getting really fed up with this same problem for days now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.