Dx9 sprite transformations

Started by
7 comments, last by corrington_j 20 years, 1 month ago
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,
			&center
                        &position,
			D3DCOLOR_RGBA( 255, 255, 255, 255))))
	     return false;

	//reset the transformation structure

	sprite->SetTransform(&oldTransform);

	return true;
}
Advertisement
can anyone help. please.
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.
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.
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]
thanks so much, do i need to set transform, or not.
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.
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 thisfloat 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 texturefloat 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]
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.TopEnd If''// rotate from the center of the scaled texturercenter.X = (pDestRect.Right - pDestRect.Left) / 2rcenter.Y = (pDestRect.Bottom - pDestRect.Top) / 2If 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 IfIf 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 IfEnd If''// set the transform matrixTransform = Matrix.Transformation2D(Nothing, Nothing, scaling, rcenter, rotation.Radians, trans)''// draw the spriteDraw(pSrcTexture, pSrcRect, Nothing, Nothing, modulate)End Sub

This topic is closed to new replies.

Advertisement