Sign in to follow this  
Aiwendil

Mirroring Sprites

Recommended Posts

Hi everyone, I'm working on a simple 2D side scrolling game, and I was trying to make it so that the player is mirrored when the cursor moves behind him. The player is represented by three different sprites, one for the body and one for each arm. I got the body to work right, and it mirrors properly, but I can't seem to get the arms to work. Here is the code:
void Player::draw() //draws the body
{
	mCenter = D3DXVECTOR3(64, 64, 0);

	D3DXMATRIX scaling;
	D3DXMatrixScaling(&scaling, mScale, 1.0f, 0.0f);

	D3DXMATRIX rotation, translation, combined;
	D3DXMatrixTranslation(&translation, mPos.x, mPos.y, 0.0f);
	D3DXMatrixRotationZ(&rotation, mRotation);
	mWorldMat = scaling * rotation * translation;

	mSprite->SetTransform(&mWorldMat);

	mSprite->Begin(D3DXSPRITE_ALPHABLEND);
	mSprite->Draw(mTexture[(int)mFrameToDraw], NULL, &mCenter, 0, D3DCOLOR_ARGB(255, 255, 255, 255));
	mSprite->End();
}

void Player::drawRightArm()
{
	if (!mCrouched)
	{
		mRightArmPos.x = mPos.x - 14.0f;
		mRightArmPos.y = mPos.y - 25.0f;
		mCenter = D3DXVECTOR3(5, 15, 0);
	}
	else if (mCrouched)
	{
		mRightArmPos.x = mPos.x - 4.0f;
		mRightArmPos.y = mPos.y + 15.0f;
		mCenter = D3DXVECTOR3(5, 15, 0);
	}

	D3DXMATRIX scaling;
	D3DXMatrixScaling(&scaling, mArmScale, 1.0f, 0.0f);
	if (mArmScale < 1)
	{
		mRightArmPos.x += 30.0f;
	}

	D3DXMATRIX rotation, translation, combined;
	D3DXMatrixTranslation(&translation, mRightArmPos.x, mRightArmPos.y, 0.0f);
	D3DXMatrixRotationZ(&rotation, mRightArmRotation);
	mWorldMat = rotation * translation;

	mSprite->SetTransform(&mWorldMat);

	mSprite->Begin(D3DXSPRITE_ALPHABLEND);
	mSprite->Draw(mTexture[15], NULL, &mCenter, 0, D3DCOLOR_ARGB(255, 255, 255, 255));
	mSprite->End();
}


The code for drawing the left arm is the same as the right arm, except it used mLeftArmRotation and mLeftArmPosition instead. I check to see if the cursor is behind the player using:
POINT pt;
GetCursorPos(&pt);
ScreenToClient(app.mWindowHandle, &pt);

if (pt.x < mPos.x)
{
	mScale = -1.0f; //scales the body
	mArmScale = -1.0f; //scales the arms
}
else
{
	mArmScale = 1.0f;
	mScale = 1.0f;
}


Does anyone have an idea as to why the arms don't mirror?

Share this post


Link to post
Share on other sites
You never use the scaling matrix when calculating your world matrix for the arms.

Body (Player::draw()):
Quote:

mWorldMat = scaling * rotation * translation;


Arm(Player::drawRightArm()):
Quote:

mWorldMat = rotation * translation;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this