• Advertisement
Sign in to follow this  

Rotating Sprite Problem

This topic is 3315 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to rotate a sprite but I can't get it to work properly, everything seems to rotate around the screen's corner.

void Direct3D::draw_sprite(Object &object)
{
	float world_coordinates_per_pixel = 1/32.0f;
	SpriteInstance sprite_instance = object.get_sprite_instance();
	LPDIRECT3DTEXTURE9 texture = sprite_instance.get_sprite()->get_texture();
	D3DXMATRIX matrix;

	D3DXMatrixOrthoOffCenterLH(&matrix, 
							   0, 20, 
							   0, 15,
							   0, 1);
	d3d_device->SetTransform(D3DTS_PROJECTION, &matrix);

	D3DXMatrixScaling(&matrix,
					  world_coordinates_per_pixel,
					  -world_coordinates_per_pixel,
					  1);
	d3d_device->SetTransform(D3DTS_VIEW, &matrix);

	D3DXMatrixRotationZ(&matrix,
						object.object_body->GetAngle());
	d3d_sprite->SetTransform(&matrix);

	RECT frame = get_frame(sprite_instance);

	b2Vec2 translation_scale = b2Vec2(1.0f/world_coordinates_per_pixel,
									 -1.0f/world_coordinates_per_pixel);

	D3DXVECTOR3 center;
	center = get_center(sprite_instance.get_size()/2); 

	D3DXVECTOR3 position;
	position.x = object.object_body->GetPosition().x;
	position.y = object.object_body->GetPosition().y;
	position.z = 1.0f;

	position.x *= translation_scale.x;
	position.y *= translation_scale.y;
	position.z *= 1;

    d3d_sprite->Draw(texture, &frame, &center, &position, 0xFFFFFFFF);
}

RECT Direct3D::get_frame(SpriteInstance &sprite)
{
	RECT frame;

	frame.left = sprite.get_column() * sprite.get_size();
	frame.right = frame.left + sprite.get_size();
	frame.top = sprite.get_row() * sprite.get_size();
	frame.bottom = frame.top + sprite.get_size();

	return frame;
}

D3DXVECTOR3 Direct3D::get_center(float center)
{
	return D3DXVECTOR3(center,center,0);
}


Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement