Advertisement

Collision Detection

Started by February 07, 2000 03:00 PM
1 comment, last by Zenroth 24 years, 10 months ago
I have been working on using line collision checks,(Drawing a line from a objects oldx and y cordinates to its new one''s and checking each point in between.) int collisionlinecheck(paddle &ptr){ int minx,maxx,miny,maxy; if (GameBall.ox < GameBall.x) { minx = GameBall.ox; maxx = GameBall.x; } else { minx = GameBall.x; maxx = GameBall.ox; } if (GameBall.oy < GameBall.y) { miny = GameBall.oy; maxy = GameBall.y; } else { miny = GameBall.y; maxy = GameBall.oy; } int x = minx; int y = miny; for (minx; x <= maxx; x++) for (miny; y <= maxy; y++) if (!(x >=ptr.x[0] && x<=ptr.x[0]+ptr.w)) return 0; if ((y>=ptr.y[0]) && (y<=ptr.y[0]+25)){ if(ptr.num==2)GameBall.xv +=0.5;else GameBall.xv -=0.5; GameBall.speed+=0.5; GameBall.xv=-GameBall.xv; GameBall.yv-=3; GameBall.x+=GameBall.xv*GameBall.speed; return 1;} if ((y>=ptr.y[1]) && (y<=ptr.y[1]+25)){ if(ptr.num==2)GameBall.xv +=0.5;else GameBall.xv -=0.5; GameBall.speed+=0.5; GameBall.xv=-GameBall.xv; GameBall.x+=GameBall.xv*GameBall.speed; return 1;} if ((y>=ptr.y[2]) && (y<=ptr.y[2]+25)){ if(ptr.num==2)GameBall.xv +=0.5;else GameBall.xv -=0.5; GameBall.speed+=0.5; GameBall.xv=-GameBall.xv; GameBall.yv+=3; GameBall.x+=GameBall.xv*GameBall.speed; return 1;} return 0;} Now ill give a explanation of the above code. I have the object im checking against(a paddle) split into 3 differnt sections for collision checks.(So I have 3 differnt angles to refract from.) ox is the balls oldx ,oy is the oldy. Oh and num is what side the paddle is on left or right(2 for being left.) The problem is that the ball will pass through the paddle on the right of the screen alot of times. Which baffles me so any help would be very much appreciated.
Juz skimming through the code

The two for loops, do they need bracketing?
Advertisement
I''ll just clean up your code a bit. =)

int collisionlinecheck(paddle &ptr){  int minx,maxx,miny,maxy;	  if (GameBall.ox < GameBall.x)   {     minx = GameBall.ox;     maxx = GameBall.x;   }  else   {     minx = GameBall.x;     maxx = GameBall.ox;   }	  if (GameBall.oy < GameBall.y)   {     miny = GameBall.oy;     maxy = GameBall.y;   }  else   {     miny = GameBall.y;     maxy = GameBall.oy;   }  for (int x = minx; x <= maxx; x++)  {    for (int y = miny; y <= maxy; y++)    {      if (!(x >=ptr.x[0] && x<=(ptr.x[0]+ptr.w)))         return 0;      if (y>=ptr.y[0] && y<=(ptr.y[0]+25))      {        if(ptr.num==2)          GameBall.xv +=0.5;        else          GameBall.xv -=0.5;        GameBall.speed+=0.5;        GameBall.xv=-GameBall.xv;        GameBall.yv-=3;        GameBall.x+=GameBall.xv*GameBall.speed;                return 1;      }      if( y>=ptr.y[1] && y<=(ptr.y[1]+25))      {        if(ptr.num==2)          GameBall.xv +=0.5;        else          GameBall.xv -=0.5;        GameBall.speed+=0.5;        GameBall.xv=-GameBall.xv;        GameBall.x+=GameBall.xv*GameBall.speed;				        return 1;      }      if( y>=ptr.y[2] && y<=(ptr.y[2]+25) )      {        if(ptr.num==2)          GameBall.xv +=0.5;        else          GameBall.xv -=0.5;        GameBall.speed+=0.5;        GameBall.xv=-GameBall.xv;        GameBall.yv+=3;		        GameBall.x+=GameBall.xv*GameBall.speed;				        return 1;      }    }  }  return 0;}


I also added the brackets that Void pointed out as missing.

I think I found your problem. In your if statements you have forgotten to use parenteses. The compare operators have lower priority than the aritmetic ones. Also the logic operators have lower priorities than compare operators. You should check on operator priorities in your manual.

I have added the paranteses that I mentioned to the code above so you might want to give it a try now.

I hope that helped =)

This topic is closed to new replies.

Advertisement