collision detection: array loop o_O

Started by
10 comments, last by pimple 20 years ago
haya all... i''m working on this plattformer and i started it out by getting a block on the screen... then i made that block move left and right at my will, and finally i made it jump realistically (and even move right/left while jumping!)... heh, pretty easy basic stuff... (btw, this is a 2d game... a mega man clone) now, here''s the question... i''m ready to start adding some plattforms, but i''m not too sure how i can do so very effectivelly... when i did my pac man clone, i had an array of "blocks", where each block had a set of coordinates... so since there weren''t that many blocks on the screen, i looped through every block''s coords (once a frame) and see if my pac man''s coords was colliding with them... althought that did work quite smoothly, i don''t think that''s a very effective way of doing it, specially if you have hundreds of little "blocks" on the screen, like i plan on doing... so, what''s a good way to have, first of all, all of the blocks set on the screen, and also, what''s the best way to check for collision in such case... thanks!
---Current project - Duck Hunt 2: Free at Last!http://www.duckhunt2.cjb.net
Advertisement
I dont know how effective this is, because I never finished my project, but, when I was working on my platformer I split up the level into ''sectors'', or something, and only checked againsts platforms that are in the same ''sector'' as the player.

Maybe someone else has another idea, or can explain mine better...

Later,
Lord Hen

"I am free of all prejudices. I hate everyone equally." - W. C. Fields

[edited by - Lord Hen on December 2, 2008 2:32:49 AM]
okay, that sounds good, but a little elaboration would help... thanks...
---Current project - Duck Hunt 2: Free at Last!http://www.duckhunt2.cjb.net
If every logical "background tile" on screen is potentially a block... don''t loop through those to see if they intersect the player; instead, check the player''s location and see which background tiles intersect that. (Do some arithmetic to figure that out). Then see whether there''s anything solid in that area.
i don''t get it...
---Current project - Duck Hunt 2: Free at Last!http://www.duckhunt2.cjb.net
anyone? i seem to be having bad luck with google... can''t find anything helpful anywhere...
---Current project - Duck Hunt 2: Free at Last!http://www.duckhunt2.cjb.net
He''s saying make a "tile based" game. It''s not quite as cool, but for static background objects, it''s fast. Keep you entire world as a 2D array of blocks. Each block can have properties: open, closed, land on but pass through from the bottom, breaks when hit, etc.

Your player is at pixel 1000x 700y. So go and look if the tile(s) that would be there are open or closed. Divide by the tile size (say 32) = 31x 21y. Now look that up in your array:

if ( tiles[32][21].status == CLOSED ) {
// do whatever
}
ooohh... gotcha... heh, sounds dynamic and just what i was looking for, i guess... seems a little too much to code, but i don''t mind that at all... thanks again for all of you who took the time to help! god bless everyone!
---Current project - Duck Hunt 2: Free at Last!http://www.duckhunt2.cjb.net
hi again...
i''ve done what you guys suggested, and it seems to work pretty good... the only problem that i''ve been having is that some times my character goes through the floor and stuff (mostly when falling from high heights)... this is what i''ve done, pretty much:

class tile:   int X,       Y,       Width,       Height;   char cStatus;tile World[SCREEN_WIDTH][SCREEN_HEIGHT];void collisionDetection(mmX, mmY){   if(mmX > 0 && mmY > 0 && mmX < SCREEN_WIDTH && mmY < SCREEN_HEIGHT)   {      if(World[mmX, mmY].cStatus == ''S'' || World[mmX + 24, mmY].cStatus == ''S'' || World[mmX + 24, mmY + 24].cStatus == ''S'' || World[mmX, mmY + 24].cStatus == ''S'')      {      /* ''S'' means tile SOLID; four tests above test for collision for all four corners of sprite */         if(World[bottomLeft].cStatus == ''S'' || World[bottomRight].cStatus == ''S'' && rest of World[...].cStatus == ''O'')         {             /* by this i know it collided from underneath, so change MegaMan.Y accordingly */             MegaMan.Y = World[mmX, mmY].Y - MegaMan.Height;         }      }   }} 


something like that... then i check if only the two right and left corners have collided and change MegaMan.X accordingly, and the same for the upper two corners and MegaMan.Y... in theory everything should be working fine, but it works funkily... so i set message boxes to pop up every time any of the collisions collided, but for some reason, a certain if statemetn would be triggered when the statement cleared didn''t apply... that was so weird...

anyhoo, maybe i''m just overlooking something, eventhough i''ve been carefully looking at it for two days now, and even used C# builder''s debug tools, but the if statement still returned true when three of the arguments in it returned false... funky...

anyhoo, the reason i''m posting is that i''m sure you guys know of much better collision detection methods... if anyone could help me out yet again, i''d be so very grateful... thanks!
---Current project - Duck Hunt 2: Free at Last!http://www.duckhunt2.cjb.net
what''s the tag for posting codes again? i tried (without the spaces), but apperently it didn''t work... anyhoo, i forgot, here''s my program:<br><br>http://www.geocities.com/antigatez/mmclone.htm
---Current project - Duck Hunt 2: Free at Last!http://www.duckhunt2.cjb.net

This topic is closed to new replies.

Advertisement