Sign in to follow this  
waterboy4800

Drawing a Sprite in a 3D World

Recommended Posts

waterboy4800    122
Hey there, Been searching all day wondering how to do the following, hopefully some one can help me. I’m using a direct X .x file to load in a mesh as my terrain for a playing field in my game. The game is a 2d side scrolling shooter with a 3D walking area to give it some depth and I need at least one 3d object in the game to get marks for that portion. When I press left and right keys the sprite will move as well as the camera which points at the 3d mesh. My Problem is that sprites are using screen space coordinates and I need them to be using world space coordinates so I can do collision detection and move them with the camera without them running off the screen. Anyone have any suggestions? Thanks Here is what I’m looking for. for the sprite to follow the camera as i move pan on the x and y of the terrain. http://img491.imageshack.us/my.php?image=progress9gi.jpg

Share this post


Link to post
Share on other sites
Armadon    1091
You could look into the sprite flags. They give you the ability to place the sprite in object space, this means that the current transformation matrices set on the device will be used to do the transformations on the sprite.

SpriteFlags.ObjectSpace
Specifies no modification of the world, view, and projection transforms. The transforms currently set to the device are used to transform sprites when the batched sprites are drawn (that is, when Sprite.Flush or Sprite.End is called). If this option is not specified, the world, view, and projection transforms are modified so that sprites are drawn in screen-space coordinates.

I hope this helps.
Take care.

Share this post


Link to post
Share on other sites
waterboy4800    122
Hey there. That worked.

But i ran into another problem, Its that its massive, i need to scale it down to 0.01f to make it look normal, any idea why this is? and its upside down.

here is some code not scaled

heres the pic
http://img491.imageshack.us/my.php?image=problem24bk.jpg

void CAnimatedSprite::OnFrameRender( IDirect3DDevice9* pdev )
{
m_spriteRect.top = (( (m_currentFrame - 1) / m_rowsInAnimation)) * m_nFrameHeight;
m_spriteRect.left = (( (m_currentFrame - 1) % m_rowsInAnimation ) * m_nFrameWidth);
m_spriteRect.bottom = m_spriteRect.top + m_nFrameHeight;
m_spriteRect.right = m_spriteRect.left + m_nFrameWidth;

m_sprite->Begin( D3DXSPRITE_ALPHABLEND | D3DXSPRITE_OBJECTSPACE);

// Texture being used is 64 by 64:
D3DXVECTOR2 spriteCentre=D3DXVECTOR2(32.0f,32.0f);
D3DXVECTOR2 trans = D3DXVECTOR2(0.0f,0.0f);
float rotation = 0.0f; //timeGetTime()/500.0f;

D3DXMATRIX mat;
D3DXMatrixIdentity(&mat);
D3DXVECTOR2 scaling(1.00f,1.00f);
D3DXMatrixTransformation2D(&mat,NULL,0.0,&scaling,NULL,rotation,&trans);
m_sprite->SetTransform(&mat);

m_sprite->Draw ( m_texture,
&m_spriteRect,
NULL,
NULL,
D3DCOLOR_COLORVALUE( 1.0f, 1.0f, 1.0f, 1.0f )
);
m_sprite->End();
}


Scaled code
scaled Pic
http://img491.imageshack.us/my.php?image=problem0uq.jpg

void CAnimatedSprite::OnFrameRender( IDirect3DDevice9* pdev )
{
m_spriteRect.top = (( (m_currentFrame - 1) / m_rowsInAnimation)) * m_nFrameHeight;
m_spriteRect.left = (( (m_currentFrame - 1) % m_rowsInAnimation ) * m_nFrameWidth);
m_spriteRect.bottom = m_spriteRect.top + m_nFrameHeight;
m_spriteRect.right = m_spriteRect.left + m_nFrameWidth;

m_sprite->Begin( D3DXSPRITE_ALPHABLEND | D3DXSPRITE_OBJECTSPACE);

// Texture being used is 64 by 64:
D3DXVECTOR2 spriteCentre=D3DXVECTOR2(32.0f,32.0f);
D3DXVECTOR2 trans = D3DXVECTOR2(0.0f,0.0f);
float rotation = 0.0f; //timeGetTime()/500.0f;

D3DXMATRIX mat;
D3DXMatrixIdentity(&mat);
D3DXVECTOR2 scaling(0.01f,0.01f);
D3DXMatrixTransformation2D(&mat,NULL,0.0,&scaling,NULL,rotation,&trans);
m_sprite->SetTransform(&mat);

m_sprite->Draw ( m_texture,
&m_spriteRect,
NULL,
NULL,
D3DCOLOR_COLORVALUE( 1.0f, 1.0f, 1.0f, 1.0f )
);
m_sprite->End();
}



Am i missing something still?

[Edited by - Coder on November 10, 2005 11:28:05 PM]

Share this post


Link to post
Share on other sites
waterboy4800    122
I understand why its so large its due the size of the texture in pixels is getting converted to world space cordinates.

Anyone know how to fix this problem now?




[Edited by - waterboy4800 on November 10, 2005 7:33:30 PM]

Share this post


Link to post
Share on other sites
Armadon    1091
You could solve this in a number of ways and one or 2 ways would be to reduce the texture to the correct size or to introduce a scaling transformation matrix.

i.e.

Device.Transform.World = Matrix.Scaling(0.2f, 0.2f, 0.2f) * Matrix.Translation(0.0f, 0.0f, 0.0f);

using(Sprite sprite = new Sprite(renderer.Device))
{
sprite.Begin(SpriteFlags.ObjectSpace);
sprite.Draw(texture, new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.0f, 0.0f), Color.White.ToArgb());
sprite.End();
}



I hope this helps.
Take care.

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