How did you implement...

Started by
4 comments, last by EbonySeraph 21 years, 3 months ago
Im working on a project which I plan on turning into some 2d platformer and I was wondering. Of those that have tried, and preferably succeeded, how did you: -Store the game objects that need collision detection(i.e. sprites, and surfaces). -Actually do the collision detection between animated sprites and the ground/platforms. -Animate the sprite(i.e., know the first rect in loop, the rest is all in the same row with the same dimensions) I have idea''s for how to do all of them and could probably do them. But I prefer to know how others did it so I could do it right the first time. "Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
"Ogun's Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
Advertisement
There''s no right or wrong way as long as it works.
Something I''ve used to keeping track of sprite animation frames:


  struct AnimDef{    int first; // image index # of first frame in animation     int last;  // image index # of last frame in animation    int delay; // ms delay between each frame}struct Sprite{    Image*  image;         // pointer to custom Image data    int     size_x;        // width    int     size_y;        // height    int     current_anim;  // which animation is this sprite in    int     current_frame; // which image index is this sprite in    int     timer;         // animation timer    AnimDef anims[16];     // up to 16 defined animations}  


All the image frames for a given sprite were the same size (say 32x32 pixels) and stored in a column in a bitmap. So, say the sprite was 32x32 and the total images I made was 20, the bitmap for that sprite would be 32 pixels wide by 640 pixels high.

For each loop of the game, I''d add the deltatime for the previous loop to each sprite''s animation timer. If this value was > anims[current_anim].delay, I''d increment the sprite''s current_frame, and roll the timer over. If the sprite''s current_frame was > anims[current_anim].last, I''d make it = anims[current_anim].first instead. Pretty simple system and it worked just fine.

To render the sprite, I just blitted the appropriate 32x32 frame from the sprites image (internal Image format) by multiplying the current_frame by the size_y to get the y offset.



collisions can be easily done by using good old boundry rectangles, but some people get really picky and use pixel collisions. I could see a polygon collision solution as well.

sprite animation can be complex depending on what you want. it can be done with a series of frames or pieces of frames that belong to their own memory locations.
in other words you can have a stick figure with a frame per frame action or the stick figure''s head, arm,legs, hands..etc can be their own sprites with different frames in them.. this way would also make things look for real, but it will be plenty of more code to write.

I hope you were talking about 2D =)

The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.

?Have a nice day!?

If you are using a 2d table to store the level you can translate the character you wish to test collision against to a location in the 2d table by using the formula x_screenlocation/tilesize = array locationx and same for y youll have to add an offset if your map is bigger then the screen but its trivial to implement. Im pretty sure thats the formula but if im wrong pleased correct me. I have found this be the easiest way to check for collision for characters against a 2d map tile. Of course there are some special cases where this might not work but working around them is easy and its faster then testing for collision as if the tile was a rect. Depending on the game you are creating this might not work so sorry if i just wasted your time. Anyway hope this helps a little. -Kris

enhanced code on contact
i c teh code.
1st take a look at this thread (last post):
http://www.gamedev.net/community/forums/topic.asp?topic_id=126804

It''s how I did the animation (if you need hints).

As for collision detection, I use a pixel-by-pixel check. Basically, after I load a bitmap, I then get the measurements of the bitmap, scan the bitmap for the color, store in a bitmask structure where there is a pixel and where there isn''t (ie, the transparent color). Then, I 1st do a rectangle check between bitmaps on the screen that can collide, and if they overlap, then I do a per-pixel check.

Pretty simple, but works very well.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement