Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

AdolphousC

Sprite Collisions

This topic is 5561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Heres a question that hopefully will be really easy to answer. If I have 3 or more sprites on a screen, asssuming one of them is the character that I am controlling, and the other two are creatures, if I walk into a creature, how do I know which sprite creature it is? Would I check each sprite on the screen and see which one I am in contact with? Or is there a better way? if I need to check every one of them, what if I have a large number of sprites, like 1000 ( okay maybe in practicle for a game, but its possible ) ** I was thinking about setting up an array, and filling that array with codes, like 1 would be a creature, 0 a place to walk, ect... but even when I move into that grid where the 1/creature is, how do I know which creature I am being attacked by... thanks

Share this post


Link to post
Share on other sites
Advertisement
There are a bunch of ways to do it. Checking each enemy one at a time certainly works, but isn''t very efficient. Another solution (the one you''re thinking of, from what I''ve read) is to maintain a 2D array representing the screen. Basically, you break the sceen up into a checkerboard, with squares that are any size you want. Then, any time a sprite is moved, you update its position in the grid. For example, if you have the screen split into quadrants, then your array would be 2x2. All monster in the upper-right quadrant would be in location [0][1] in your grid. Then, collision detection is a matter of checking only those monsters in the same grid location as your character. The more you divide the screen, the fewer monsters you have to check. Either way you do it, you DO have to have a way of uniquely identifying the object on your screen. Instead of an array of 1''s and 0''s, like you suggested, you could store pointers to your monsters in the 2D array. Note: if you have a list of monsters in one location in the grid, then it''s not technically 2D anymore, but that''s a problem you can work on. So, in conclusion, if you break the screen into a grid, you can determine the general location of your character. Based on that information, you check for collision with every monster in the same general area, eliminating a lot of checking with monsters on the other side of the map. Be aware of errors with this method, though. A sprite whose upper-left corner is in one grid location might be present in other grid locations, if it''s too close to the edge of the grid segment. Hope this helps, and be sure to read up on pointers for determining which monster is which.

Share this post


Link to post
Share on other sites
Hmm, not heard of the grid method... it seems to me that with a bounding box check (a couple of > and < commands) checking each monster would be faster than that because you''d have to update the grid positions. Would that be faster than one ''if'' command?

Generally in my experience all monsters are checked. With a pool game, every ball is checked against every other... Lots of looping but really this kind of ''if'' statement is pretty high speed nowadays. Graphics then AI normally causes the biggest slowdowns.

One speed tip is avoid for...next loops if possible. If you want to loop 5 times, duplicate the code 5 times instead. Much faster.

Mark
Sound Effects For Game Developers
http://www.indiesfx.co.uk

Share this post


Link to post
Share on other sites
For large number of monsters, eliminating a significant percentage during collision testing will definitely increase your program''s speed. It''s the same idea behind drawing the wall in front of a player in an FPS, rather than the monster behind the wall, and then the wall on top; why execute code that isn''t necessary? Anyway, one if() block every time a sprite moves, plus collision detection on a small percentage of the sprites onscreen, takes less time than NUM_MONSTERS + NUM_CHARACTERS collision detections each time a single sprite moves.

Share this post


Link to post
Share on other sites
Either use an Array, or linked lists, or stacks. I like to use Arrays/stacks combo.



- Rob Loach
Current Project: Upgrade to .NET and DirectX 9
Percent Complete: X%

Share this post


Link to post
Share on other sites
Well, I used this idea in my turn base strategy:

Each player had and array of units and array of buildings (both made with a special class)!

I had two MAP arrays, one holding the terrain stuff (char) and the other one - units and buildings (custom calss; array looked like this: UnitMap[32][20]).
Each of the variable in Unitmap had 3 variables:
int type (0- empty place, 1-building, 2 unit)
int team (which player's unit this is?)
int place (now that was the most important thing!)

Now when I had the attack coordinates, I put them in the Unitmap (UnitMap[attackcoordX][attackcoordY]) and could receive the first two variables to check if the target can be attacked and the place varaible... to get the place in player's unit array which holds the unit!

so when I add unit, I add it to players unit array (for example in place: Units[24]), then input all stats of the unit into this array (remember it's a class!) and then input the input the same info into the UnitMap like:

Units[24].id = 1 //id = type of a unit (soldier, heavy, sniper etc.)
Units[24].hp =30; //duh
Units[24].attakcpower =10; // duh

//and

Unitmap[CreateCoordX][CreatCoordY].type=1; // 1 means unit
Unitmap[CreateCoordX][CreatCoordY].team=1; //1 means player 1
Unitmap[CreateCoordX][CreatCoordY].place=24; //which place in the unit array


this way when I attacked something, I could easily find it in players array of units (I have the team so I know which player's array, I have the type so I know if I should look in building or unit array and I have the place in which the unit is)

Pretty simple but works well, hope I dodn't confuse you !


[edited by - Koobazaur on April 3, 2003 2:51:55 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!