2-D Collision detection!

Started by
0 comments, last by ViNcE989 22 years, 4 months ago
Hi, I know this might have already been made, but I really need it bad. I started, about a month ago, a game called OpenSS (Open-Side-Scroller), and I''m pretty much started. I''m at a point where I need one important thing : Collision detection! I''m not looking for a complex one, since OpenSS will be used for old-skool side scrollers, just like SMB3 on the Nes. So, all the physics are "done", it jumps (but doesn''t lands), accelerates on the X-Axis, but there''s NO collision detection! The way the game works is like this : There''s a bg layer (will be EZ to do) There''s a tile grid (where the brick blocks, ? Blocks, floor and the like) are located There''s a "sprite layer", where all sprites (char, allies (pals over the net), enemies, etc) will go So I first get the tiles in which the char is, and then I need to check if there''s a collision. I want to look at my code, here it is : (da sprite struct starts here) struct SPRITE_STRUCT { int SizeX; int SizeY; int TileTL; /* [-V989] In the next four lines, the vars are used to check if there is a collision */ int TileTR; /* [-V989] TL is for Top left, BR if for bottom right, and so on... */ int TileBL; int TileBR; int XDirection; int YDirection; int AnimFrame; int MaxFrames; /* [-V989] Will be something like "GetMaxFrames()" */ CHAR SubType; /* [-V989] ex Fire Mario, Super Mario, etc */ FLOAT fPosX; FLOAT fPosY; FLOAT fVelX; FLOAT fVelY; }; (da physics start here) VOID UpdateSprite( SPRITE_STRUCT* pSprite, FLOAT fTimeDelta ) { /* [-V989] Since, for the X axis, right is positive and left is negative... Hard to explain =:0( , but just try to think about it... */ pSprite->XDirection = KeyRight-KeyLeft; pSprite->YDirection = KeyDown-KeyUp; /* [-V989] Calculate the X momentum */ float MaxSpeed = 200; float AccelSpeed = 1; float BrakeSpeed = 6; float SlowDownSpeed = 3; float daifrule; /* [-V989] It uses an simple math rule, -*- and +*+ always give +, while the others give - */ if(pSprite->fVelX!=0) daifrule = pSprite->XDirection * pSprite->fVelX; else daifrule = pSprite->XDirection * pSprite->fVelX+1; /* [-V989] "fVelX+1" is there to make sure it''s not the velocity who makes the result a 0 */ if((KeyRight || KeyLeft)) { if(daifrule<0) /* [-V989] if they are not in the same way, it will result as a - */ pSprite->fVelX+=(pSprite->XDirection * (BrakeSpeed * (fTimeDelta * 100))); if(daifrule>0) /* [-V989] if they are in the same way, it will be a + */ pSprite->fVelX+=(pSprite->XDirection * (AccelSpeed * (fTimeDelta * 100))); } if(!KeyRight && !KeyLeft) /* [-V989] No key is pressed, slow down... */ { if(pSprite->fVelX > 0) pSprite->fVelX-=SlowDownSpeed * (fTimeDelta * 100); else if(pSprite->fVelX < 0) pSprite->fVelX+=SlowDownSpeed * fTimeDelta * 100; } /* [-V989] Make sure they are in a certain speed range */ if(pSprite->fVelX < 1 && pSprite->fVelX > 0) pSprite->fVelX = 0; if(pSprite->fVelX > -1 && pSprite->fVelX < 0) pSprite->fVelX = 0; if(pSprite->fVelX > MaxSpeed) pSprite->fVelX = MaxSpeed; if(pSprite->fVelX < -MaxSpeed) pSprite->fVelX = -MaxSpeed; /* [-V989] Calculate the Y momentum. It only uses basic physics, and might be made better someday, if needed... */ const int InitSpeed = 200; const float GravAccel = -9.8; /* [-V989] The earth is 9.8 , the moon is 1.6 */ if(KeyUp && pSprite->fVelY == 0) pSprite->fVelY = -InitSpeed; else if(KeyUp && pSprite->fVelY < 0) pSprite->fVelY -= ((GravAccel * fTimeDelta * 100) / 2); else if ((!KeyUp && pSprite->fVelY < 0) || (pSprite->fVelY > 0)) pSprite->fVelY -= (GravAccel * fTimeDelta * 100); /* [-V989] Temporary Debugging-related function, to reset the important vars that are being made...*/ if(KeySpace) { pSprite->fVelY = 0; pSprite->fVelX = 0; pSprite->fPosX = 320; pSprite->fPosY = 240; } /* [-V989] Basic Collision detection */ /* [-V989] Check if the sprite is within the screen boundaries */ if(pSprite->fPosY > SCREEN_HEIGHT - (int) TileDiam) { pSprite->fPosY = SCREEN_HEIGHT - (int) TileDiam; pSprite->fVelY = 0; KeyUp = 0; } /* [-V989] Check if the sprite is overlapping a tile, and if it is, then get "out" of the tile */ (physics code will be here) /* [-V989] Update the sprite(s) position(s) after checking if they are not overlapping a OBJ */ int checkall=1; if(checkall) pSprite->fPosX += pSprite->fVelX * fTimeDelta; if(checkall) pSprite->fPosY += pSprite->fVelY * fTimeDelta; /* [-V989] Will be used for the anims, might be implemented soon... */ // pSprite->MaxFrames=2; /* [-V989] "=2" will be replaced by something like "=GetMaxFrames(state,...) */ // Animcntr+=fTimeDelta; // if(Animcntr>1.000) Animcntr-=1.000; // for(int i = 1; i < pSprite->MaxFrames; i++) // { // if((Animcntr/i)>(1/pSprite->MaxFrames)) pSprite->AnimFrame = i; // } } (end of my code bits!) Sorry if it''s a long read, but I put it here so that you can see how it works... If you need help to read my code, just ask me, and I will explain it the best I can! =:0) - ViNcE989, founder and coder of OpenSS
My french board : http://vince989.webhop.org/
Advertisement

Programming a sprite library myself.

I built the collision detection algos around
this article by John Amato:

http://www.gamedev.net/reference/articles/article735.asp

It explains everything from rectangular detection to
Sprite on Sprite pixel level detection.

I however used BOOL bitmasks, and even the pixel level
detectors are fairly quick.

Guy
Adulthood is the vehicle for making you childhood dreams come true.

This topic is closed to new replies.

Advertisement