Archived

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

collision detection: array loop o_O

This topic is 5000 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

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!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
what''s the tag for posting codes again? i tried [ code ] (without the spaces), but apperently it didn''t work... anyhoo, i forgot, here''s my program:

http://www.geocities.com/antigatez/mmclone.htm

Share this post


Link to post
Share on other sites
[ source ] code [/source]

Actually [ code ] does work, it keeps the original formatting wheras the source tags place your code inside a source box.

[edited by - tiffany_smith on April 7, 2004 11:51:41 PM]

Share this post


Link to post
Share on other sites