2d collistion detection
i was wondering what the easyest way is to write collition detection in c++ WIN API for just a simple game of pong.
Check the X value of the ball to the inner X value of the paddles, then check to see if the Y value is within the location of the paddle. Its not the prettiest method but its about the simplest i can think of
see im having some problems witht that maybe imy just not updating the ball position correctly but that method is giving me some problems how would i store the location of the ball (in terms of the window) becuase i would need to store this in a variable to do the check
make a class or struct that represents a ball.
class Ball{ public: Ball(int x,int y){xPos = x; yPos = y;} ~Ball(){} int Get_X(){return xPos;} int Get_Y(){return yPos;} int Set_X(int x){xPos = x;} int Set_Y(int y){yPos = y;} private: int xPos; int yPos;};
This is how i did mine.
But like the previous poster said use a struct\class, and yeah im pretty sure this aint the best way to do it.
[EDIT] i find it a bit strange for a board of this caliber that focuses mostly on code not to have a CODE block option right on the post screen.
[Edited by - jsloan on September 12, 2004 2:18:26 AM]
But like the previous poster said use a struct\class, and yeah im pretty sure this aint the best way to do it.
bool DDraw::CheckCollision(int iSprite1,int iSprite2){ bool foundX; //for each X pixel of sprite 2 for (int iX=DDsprite[iSprite2].posX;iX<DDsprite[iSprite2].width;iX++) { //loop through all X pixels of sprite 1 and compare for (int i=DDsprite[iSprite1].posX;i<DDsprite[iSprite1].width;i++) { //if match found if (i == iX) { foundX = true; } } } if (foundX) { //for each Y pixel of sprite 2 for (int iY=DDsprite[iSprite2].posY;iY<DDsprite[iSprite2].height;iY++) { //loop through all Y pixels of sprite 1 for match for (int k=DDsprite[iSprite1].posY;k<DDsprite[iSprite1].height;k++) { //if match found if (k == iY) { //sprites have collided return true; } } } } return false;}
[EDIT] i find it a bit strange for a board of this caliber that focuses mostly on code not to have a CODE block option right on the post screen.
[Edited by - jsloan on September 12, 2004 2:18:26 AM]
Quote:Original post by jsloan
[EDIT] i find it a bit strange for a board of this caliber that focuses mostly on code not to have a CODE block option right on the post screen.
Good point. Anyway use [source] // code to be formated goes here [/source] or [code] // short snippets go here [/code] tags to accomplish that task.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement