2d in D3D sprite animation?

Started by
8 comments, last by xegoth 20 years, 1 month ago
I''ve made a simple game with a space ship flying around the sun (yep with gravity and everything!) but now it''s time to add some animation, so my ship will actually blow up when it collides with the sun. The thing is, none of the books in my collection say anything about animating sprites. Should I just put all of my explosion in one texture and use different coordinates of the texture every frame to get the effect of animation? Is there an more elegant way to do it? Thanks in advance. I''m using C++/Direct3d 8.
Advertisement
That is the standard way of doing it. I cannot think of anything much better although you might be wise to have the explosion in a seperate texture so you can use it elsewhere
------------------------See my games programming site at: www.toymaker.info
Thanks trip I was about to put it in the same texture as my spaceship, which would be perfect except I didn''t consider I might want to use the same explosion elsewhere. If anyone knows of any other techniques for animation post away. Untill then I''ll go ahead with the idea I started with.
You could use particles to generate a random explosion. (Basically a bunch of sprites all over-laid and at random positions around a central point.)

But I''m a newbie so I''ve only read about such things.


If you just wanted to do a sprite animation I agree with you''re suggested method with one exception... I would store the animation as a list of rectangles so that instead of simply progressing linearly through the texture you can (for example) go "out-and-back" allowing you to double use some frames (for example at first the explosion would start small and grow big, and then back to small. I''m planning to do this with my pacman game since this will allow me to open pacman''s mouth with one set of frames and then close it with the same set of frames, just in reverse order.



"Good code will come and go, but bad code sticks around."
"Good code will come and go, but bad code sticks around."
Yes.. you should put all the animation frames in one texture and then translate the texture coordinates to swap between frames.

How are you displaying your images? Are you using textured quads and orthogonal projection, transformed vertices(xyzrhw), or D3DXSprite?
Thanks for the responses, I''m using D3DXSprite.
That sounds good...

i am assuming you pretty much know how to go about changing the texture coordinates for animation. If not, i could probably help somewhat. I am also developing a 2D game with directX 8.


Good luck, and let us know if you need any more advise!
I know how to do it, just not in a particularly clean or efficient way. also not sure how to set it up so I can easily change the speed of the animation. Right now the best I can figure out is how to get it to change animation frames each time the scene is rendered. So if you could outline a clean way to do animation using texture coordinates it''d definetly help.
here is the animation function from my sprite class

void CSprite::UpdateAnimation(float speed, float frameTime){  unsigned int totalFrames = m_frames.size();  // Check if the animation should be updated  if( m_animating )  {    // Increase the current frame by the fps and frame time.    m_fCurrentFrame += speed*frameTime;    // If the current frame is more than the total number of frames    if( m_fCurrentFrame > totalFrames )    {      // Check if it''s a one time animation      if( m_oneTime ) // If it is        m_animating = false; // Disable animation      else // If it''s not      {        // Set the current frame to 0        int isOverBy = (int)m_fCurrentFrame / totalFrames;        m_fCurrentFrame -= totalFrames * isOverBy;      }    }    // Convert the current frame to an integer    m_currentFrame = (int)m_fCurrentFrame;  }}
The best way i figured out to do it is to have a sprite object that handles all your animated sprites. Inside the object definition have something like:

struct sAnimation
{
int iFirstFrame; //First Frame in this animation sequence
int iLastFrame; //Last Frame in this animation sequence
int iFrameTime; //Amount of time in milliseconds between frames
};

You can now create an array of all possible animations for that sprite:

Animation = new sAnimation[totalAnimations];

then for each possible animation tell it what the last frame and first frame in the sequence are, and loop through them updating to the next frame once the previous frame has been up for iFrameTime (in milliseconds).


So lets say you set up a simple sprite that just have one animation sequence:

Animation = new sAnimation[1];

Animation[0].iFirstFrame = 0;
Animation[0].iLastFrame = 1;
Animation[0].iFrameTime = 100;

so the first frame is in position 0 and the last frame is in position 1 with 100 ms time before switching frames.

now we transfer the currentFrame number, that is to be displayed, into texture coordinates (ie. if we are displaying the first frame then the currentFrame number will be 0) and set the source rect for d3dxsprite with those texture coordinates.

Texture:
------------------------------
| | |
| Frame 0 | Frame 1 |
| | |
| | |
------------------------------

I hope that made some sence...



[edited by - OneBitWonder on March 21, 2004 1:06:55 AM]

This topic is closed to new replies.

Advertisement