Sprite is mirrored vertically...what's wrong with my camera?

Started by
4 comments, last by MJP 16 years ago
Hi all, I'm writing a 2D, top-down game. I'm using ID3DXSprite to draw sprites, but instead of using screen coordinates, I'm using a 3D camera in "object space." I think this will be more useful in the long run, but I'm struggling to get the camera set up correctly: right now, my test texture is mirrored vertically. Here's a picture: Orientation problem And here's the camera set-up/drawing code. mSprite is my ID3DXSprite*, and gDevice is the IDirect3DDevice9*.

D3DXMATRIX V;
D3DXVECTOR3 pos(0.0f, 0.0f, -500.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXMatrixLookAtLH(&V, &pos, &target, &up);
gDevice->SetTransform(D3DTS_VIEW, &V);

D3DXMATRIX P;
float width  = (float)mClientWidth;
float height = (float)mClientHeight;
D3DXMatrixPerspectiveFovLH(&P,
    D3DX_PI * 0.25f,
    width/height,
    1.0f,
    5000.0f);

gDevice->SetTransform(D3DTS_PROJECTION, &P);


// Clear the back and depth buffers, and send the start signals.
gDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 0, 255), 1.0f, 0);
gDevice->BeginScene();

mSprite->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE);
gDevice->SetRenderState(D3DRS_ALPHATESTENABLE, true);

mSprite->Draw(mTex, 0, 0, 0, D3DCOLOR_XRGB(255, 255, 255));

// End the scene.
gDevice->SetRenderState(D3DRS_ALPHATESTENABLE, false);
mSprite->End();
gDevice->EndScene();

// Present the back buffer.
gDevice->Present(0, 0, 0, 0);

Is there something wrong with the way I'm setting up the camera?
Advertisement
Perhaps D3DXSprite expects +Y to be down (as in screen coordinates) rather than up? If you change your up vector to be (0,-1,0) instead of (0,1,0), it will probably work.
Quote:Original post by BeanDog
Perhaps D3DXSprite expects +Y to be down (as in screen coordinates) rather than up? If you change your up vector to be (0,-1,0) instead of (0,1,0), it will probably work.


Hmm, that does indeed fix the up-down problem, but now it's mirroring things across a left-right axis.
If you use traditional view and projection transforms, your sprites will come out mirrored vertically (as you can see in your screenshot). I get around it like this:

// matTransform contains the the sprite's translation, rotation, and scaling// Flip the sprite around the x-axis, so it's not mirrored upside downD3DXMATRIXA16 matRotation;D3DXMatrixRotationX( &matRotation, D3DX_PI );D3DXMATRIXA16 matTranslation;D3DXMatrixTranslation( &matTranslation, 0, static_cast<FLOAT>( m_sdTextureDesc.Height ), 0 );matTransform = matRotation * matTranslation * matTransform;// Set the transformation matrix.  This will be applied to the// four corner vertices of the sprite.CheckForDXFailure( m_pSpriteInterface->SetTransform( &matTransform ) );
MJP -- that works, thank you so much! I was really banging my head against the wall on this one.

Can I ask you about your code? Why did you include the translation matrix? Is there some side effect of the rotation that it counters?
Quote:Original post by Sol Blue
MJP -- that works, thank you so much! I was really banging my head against the wall on this one.

Can I ask you about your code? Why did you include the translation matrix? Is there some side effect of the rotation that it counters?


When you rotate the sprite, it ends up being shifted downwards. The translation corrects that. Picture holding a playing card in your hand with your thumb underneath, and then flipping it so that your thumb stays in the same spot.

This topic is closed to new replies.

Advertisement