| Collision detection for platform game |
|
necron
|
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
|
At the risk of stating the obvious, Lazy Foo has a good tutorial on this: http://lazyfoo.net/SDL_tutorials/lesson17/index.php.
|
|||||||||||
|
|
||||||||||||
|
macrofeet
|
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
|
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 |
|||||||||||
|
|
||||||||||||

