The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Collision detection for platform game
Cross-Maker


Joined: 16 Sep 2011
Posts: 15
Location: PerĂº
Hi, i'm developing a platform game and i have some troubles with collision detection between player-map.

Where can i find a good tutorial for collision detection? If someone knows an useful tutorial for this kind of games, please post it.

Thanks.
necron


Joined: 10 Dec 2011
Posts: 33
You'll want to go to the sdl site and click libs and the select ...other? as catagory I'd say. There should be libs that include collision detection.
Wow, I looked and there are lots of broken links. Try demos but the idea is this:

SDL_Rect *temprc, *dest; double left,right,top,bottom;
//bring a rect in and break it down so it's easy to understand
left = temprc.x; right = temprc.x + temprc.w; top = temprc.y; bottom = temprc.y + temprc.h;
//if part of the box is in the other box the it did collide
if (dest.y > top && dest.y < bottom) {if (dest.x > left && dest.x < right) {return 1;}}
return 0; //bam!
//this is a non functional example code^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
BenoitRen


Joined: 26 Feb 2010
Posts: 41
Location: Belgium
At the risk of stating the obvious, Lazy Foo has a good tutorial on this: http://lazyfoo.net/SDL_tutorials/lesson17/index.php.
macrofeet


Joined: 29 Apr 2012
Posts: 20
Maybe this will help another way would be compaire locations within a 2d array

//Main Code

class GameObject
{
private:
public:
bool IsAlive;
short int LImageHeight; // image height
short int LImageLength; //image length/width
float XPos;// image X pos on Screen
float YPos; // image Y pos on Screen
};


GameObject PlayerShip; aka A
GameObject Asteroid; aka B

how to call

if (PlayerShip.IsAlive)
{
CollisionCheck(PlayerShip,Asteroid,0,0,0,0);
{
//Big Explode goes here
}
}


bool CollisionCheck(GameObject A, GameObject B, short AReductionX,short AReductionY,short BReductionX,short BReductionY )
{ //The sides of the rectangles
unsigned int LeftA, LeftB;
unsigned int RightA, RightB;
unsigned int TopA, TopB;
unsigned int BottomA, BottomB;
LeftA = A.XPos+AReductionX;
RightA = A.XPos+A.LImageLength-AReductionX;
TopA= A.YPos+AReductionY;
BottomA = A.YPos+ A.LImageHeight-AReductionY;
LeftB = B.XPos+BReductionX;
RightB = B.XPos+ B.LImageLength-BReductionX;
TopB = B.YPos+BReductionY;
BottomB = B.YPos+ B.LImageHeight-BReductionY;
//Collision Boxes for Tweaking and displaying hit points
/* rectangleRGBA(screen,
LeftB, TopB,
RightB, BottomB,
0, 255, 0, 255);
rectangleRGBA(screen,
LeftA, TopA,
RightA, BottomA,
0, 255, 0, 255); */

if( (BottomA <= TopB) || (TopA >= BottomB) || (RightA <= LeftB) || (LeftA >= RightB) )
return false;
else
{
return true;
}
}
riksweeney


Joined: 17 Jan 2012
Posts: 38
Try this, I use it in my game and it works fine.

http://www.parallelrealities.co.uk/2011/10/intermediate-game-tutorial-4-tile-based.html