Hi. I initially started up a thread at the Game Dev. section of the forums but it seems rather dead so I thought I'd try my luck here.

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. So I calculate the angle and then rotate the sprite towards the mousepos. It rotates towards the right direction but when the sprite rotates it isn't centered.

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).

I initially create my texture with the D3DXCreateTextureFromFileEx function with width and height at D3DX_DEFAULT_NONPOW2 so that isn't something causing the off-centre behaviour.

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

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.

This is how my drawing function looks like atm:

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

D3DXMatrixScaling(& scaling, Surf->getScaleRateX(), Surf->getScaleRateY(), 1.0f); 
D3DXMatrixRotationZ(& rotation, D3DXToRadian(Surf->getRotationAngle() ));

Vector2D hotspot = Surf->getHotspot(),
position = Surf->getPosition();
 
float	x_Adjust = position.x + hotspot.x,
	y_Adjust = position.y + hotspot.y;
 
D3DXMatrixTranslation(& translation, position.x, position.y, 0);
 
transform = scaling * rotation * 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;

Admin, perhaps it would serve this user best, if you move this thread
to game development forum.

Admin, perhaps it would serve this user best, if you move this thread
to game development forum.

The reason I posted it here (aswell) is because there was nobody even posting for 12 days :(

I don't mean to abuse, but here it took me 15 minutes until someone actually replied to my thread.

" when the sprite rotates it isn't centered. "

elaborate a little bit more.

You don't want it to rotate around its center?

" when the sprite rotates it isn't centered. "

elaborate a little bit more.

You don't want it to rotate around its center?

I do, but the center is way off in comparison to the picture. I have the center at (16,16) and the picture is (32,32) pixels. When I rotate it, the center is outside of the picture.

translate it it into the center then rotate, then translate it back

translate it it into the center then rotate, then translate it back

Bump.
Sorry, I was busy IRL so haven't had any access to internet.

I tried what you suggested already, no dice.
I will show a picture to further show my issues since there seems to be some misunderstandings. Can't do it yet though, still don't have internet at home.

Hey, took some pictures and put it onto USB.

So here's a few screendumps about unexpected behaviour:
http://img178.imageshack.us/i/bugsu.jpg/

This is two ways I tried to count the degrees between the vectors.

Way 1:

// In player.h

Vector2D mouse_Vector;
mouse_Vector.x = float(my_Game->getInput()->getMouseX() );
mouse_Vector.y = float(my_Game->getInput()->getMouseY() );

// Normalize
Vector2D Vector;
Vector.x = mouse_Vector.x - my_Position.x;
Vector.y = mouse_Vector.y - my_Position.y;

Object::UpdateDirection(Vector);
// In object.h

void Object :: UpdateDirection(Vector2D Vector)
{
	float x = Vector.x;
	float y = Vector.y;

	// Top quadrant
	if(x >= 0 && y <= 0)
		my_Angle = abs(atan(y / x) );

	if(x <= 0 && y <= 0) 
		my_Angle = D3DX_PI - abs(atan(y / x));

	// Bottom quadrant
	if(x >= 0 && y >= 0)
		my_Angle = 2 * D3DX_PI - abs(atan(y / x));

	if(x <= 0 && y >= 0)
		my_Angle = D3DX_PI + abs(atan(y / x));

	// to degrees
	my_Angle	= my_Angle * (180 / D3DX_PI);
}

Way 2:

// In player.h

Vector2D mouse_Vector;
mouse_Vector.x = float(my_Game->getInput()->getMouseX() );
mouse_Vector.y = float(my_Game->getInput()->getMouseY() );

Object::UpdateDirection(mouse_Vector);
// In object.h

void Object :: UpdateDirection(Vector2D Vector)
{
	my_Angle = float(atan2(Vector.y, Vector.x) - atan2(my_Position.y, my_Position.x) );

	// to degrees
	my_Angle	= my_Angle * (180 / D3DX_PI);
}

the center is way off in comparison to the picture. I have the center at (16,16) and the picture is (32,32) pixels. When I rotate it, the center is outside of the picture.

Bump. Still have this issue, everything else is working as intended.

When the picture rotates it is still not centered, even though I move it to the hotspot before rotation.

Anyone please give me some feedback, starting to loose patience.

Some new pics: http://img514.imageshack.us/i/bugtest.jpg/

The circle with no | inside of it is the mouse. As you can see, the mouse is placed roughly at (32,32) on all the pictures.

The position of my player remains unchanged yet the players hotspot seems to be like 32 pixels off when rotation happens.

What I really want is for the position to remain and player actually be drawed at that position after rotation.

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.