Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

corrington_j

Dx9 sprite transformations

This topic is 5401 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 having some problems with transformations of sprites. When i rotate the sprite it also seems to move unexpectedly, as if the rotation point is not in the center. here is what I am doing.
bool cTexture::RenderTransformed(long destX, long destY,
								float scaleX, float scaleY, float rotation, 
								long srcX, long srcY,
								long width, long height)
{
	if ((width == -1) | (width > size.x))
		width = size.x;

	if ((height == -1) | (height > size.y))
		height = size.y;

	RECT rect;
	ID3DXSprite *sprite;

   sprite = graphics->GetSprite();

	rect.left	= srcX;
	rect.top	= srcY;
	rect.right	= rect.left + width;
	rect.bottom = rect.top + height;

	//save old transformation

	D3DXMATRIX oldTransform;
	sprite->GetTransform(&oldTransform);

	//set new transformation

	float centerX = static_cast<FLOAT>((destX + width) / 2.0);
	float centerY = static_cast<FLOAT>((destY + height) / 2.0);

	D3DXVECTOR2 scalingCenter(centerX, centerY);
	D3DXVECTOR2 scaling(scaleX, scaleY);

	D3DXVECTOR2 rotationCenter(centerX, centerY);

	D3DXVECTOR2 translation(static_cast<FLOAT>(destX), static_cast<FLOAT>(destY));

	D3DXVECTOR3 center(0, 0, 0);
	D3DXVECTOR3 position(static_cast<FLOAT>(destX), static_cast<FLOAT>(destY), 0);

	D3DXMATRIX transformationMatrix;
	D3DXMatrixTransformation2D(&transformationMatrix,
								&scalingCenter,							1.0f,							&scaling,							&rotationCenter,						rotation,							&translation);

	sprite->SetTransform(&transformationMatrix);
	
	if (FAILED(sprite->Draw(texture,
			&rect,
			¢er
                        &position,
			D3DCOLOR_RGBA( 255, 255, 255, 255))))
	     return false;

	//reset the transformation structure

	sprite->SetTransform(&oldTransform);

	return true;
}

Share this post


Link to post
Share on other sites
Advertisement
I belive you got some useless code there...
center shouldn''t be 0,0,0 but center(width/2,height/2,0) so you dont need to add x and y there.

Share this post


Link to post
Share on other sites
do you mean the scaling center or just center, i tried changing both around, and I am still getting the same results. Also what code is useless, I need help. please, someone must know how to do this.

Share this post


Link to post
Share on other sites
All you need to do is:


RenderTransformed(long destX, long destY,float scaleX,float scaleY, float rotation,long srcX, long srcY,long width, long height)
{
D3DXVECTOR2 scale(scaleX,scaleY), trans(X,Y);
D3DXVECTOR2 center(width/2,height/2);
sprite->Draw(texture,0,&scale,& center,rotation,&trans,0xFFFFFFFF)

return true;
}



Hope that helps becose I have one problem whit this my self so I dont dare to say anything else until its fixed.

[edited by - Craazer on March 1, 2004 6:15:44 PM]

Share this post


Link to post
Share on other sites
Ok, you must be using a different version of dx, because i am using 9, and sprite::Draw does not take 7 parameters, so the code you gave me does not work. DOes anyone have the code for dx 9 summer update. thanks a lot.

Share this post


Link to post
Share on other sites
Thats odd becose I am using DX 9.0 and according to the docs ID3DXSprite::Draw method has following defination:

HRESULT Draw( LPDIRECT3DTEXTURE9 pSrcTexture,
CONST RECT *pSrcRect,
CONST D3DXVECTOR2 *pScaling,
CONST D3DXVECTOR2 *pRotationCenter,
FLOAT Rotation,
CONST D3DVECTOR2 *pTranslation,
D3DCOLOR Color
);


So I dont know how you can have 5 par Draw in your use but asuming that you do need to use the settransformation im not much of a help becose I havent used it but...

  
// insted of this

float centerX = static_cast<FLOAT>((destX + width) / 2.0);
float centerY = static_cast<FLOAT>((destY + height) / 2.0);

// write this becose center needs to center of the texture

float centerX = static_cast<FLOAT>((width) / 2.0);
float centerY = static_cast<FLOAT>((height) / 2.0);


// And about the useless code im not sure what you are trying to do excatly but why are you using D3DXVECTOR3 insted of D3DXVECTOR2?


// And you have defined and inited center like this but you should use scalingCenter instead of center var becose 0,0,0 are in proper values


// plus your translation var wich doesnt seem to be in use




I really hope that helps becose there isn't much me to say but read the docs...

[edited by - Craazer on March 1, 2004 7:19:21 PM]

Share this post


Link to post
Share on other sites
Heres the code I used for handling rotating/flipping/scaling sprites. Its in VB.NET but shouldn''t be hard for you to convert. This was from a DX8 tutorial which I had to convert from DrawTransformed. The sprite class is inherited hence the lack of sprite object in the calls.



Public Sub BltSpriteEx(ByVal pDestRect As Rectangle, ByVal pSrcTexture As Texture, ByVal pSrcRect As Rectangle, ByVal dwFlags As mmdMirrorEnum, _
ByVal modulate As Integer, ByVal rotation As mmdRotateType)

Dim scaling As New Vector2(0, 0)
Dim rcenter As New Vector2(0, 0)
Dim trans As New Vector2(0, 0)

If Not IsNothing(pDestRect) Then
''// destination translation

trans.X = pDestRect.Left
trans.Y = pDestRect.Top
End If
''// rotate from the center of the scaled texture

rcenter.X = (pDestRect.Right - pDestRect.Left) / 2
rcenter.Y = (pDestRect.Bottom - pDestRect.Top) / 2
If Not IsNothing(pDestRect) And Not IsNothing(pSrcRect) Then
''// set the scale

scaling.X = (pDestRect.Right - pDestRect.Left) / (pSrcRect.Right - pSrcRect.Left)
scaling.Y = (pDestRect.Bottom - pDestRect.Top) / (pSrcRect.Bottom - pSrcRect.Top)
End If
If Not IsNothing(pSrcRect) And dwFlags <> mmdMirrorEnum.mmdMirrorNone Then
If (dwFlags And mmdMirrorEnum.mmdMirrorLeft) = mmdMirrorEnum.mmdMirrorLeft Then
''// flipping x

rcenter.X = -rcenter.X
scaling.X = -scaling.X
trans.X += pDestRect.Right - pDestRect.Left
End If
If (dwFlags And mmdMirrorEnum.mmdMirrorDown) = mmdMirrorEnum.mmdMirrorDown Then
''// flipping y

rcenter.Y = -rcenter.Y
scaling.Y = -scaling.Y
trans.Y += pDestRect.Bottom - pDestRect.Top
End If
End If

''// set the transform matrix

Transform = Matrix.Transformation2D(Nothing, Nothing, scaling, rcenter, rotation.Radians, trans)
''// draw the sprite

Draw(pSrcTexture, pSrcRect, Nothing, Nothing, modulate)

End Sub

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!