Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

collision detection

This topic is 3342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

well I am still working on a breakout game using opengl and c++.I need to know how to detect how a ball bounces off one of the top bricks.let me know if you need more information or some code.

Share this post


Link to post
Share on other sites
Advertisement
Here's the code I use


inline double SqrDist(double x1, double y1, double x2, double y2)
{
return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
//Can call SqrDist(x,0,0,0) to square a number
}


inline bool RectCircle(double left, double top, double right, double bot, double circx, double circy, double radius)
{
assert(right>=left);
assert(bot>=top);
assert(radius>=0);

//Check if circle is too far away
if (circx<=left-radius) {return false;}
if (circx>=right+radius) {return false;}
if (circy<=top-radius) {return false;}
if (circy>=bot+radius) {return false;}

//Unless the circle is near the corners, it's a hit

if (circx<=left && circy<=top)
{
if (SqrDist(circx,circy,left,top) > (radius * radius)) {return false;}
}
if (circx<=left && circy>=bot)
{
if (SqrDist(circx,circy,left,bot) > (radius * radius)) {return false;}
}
if (circx>=right && circy<=top)
{
if (SqrDist(circx,circy,right,top) > (radius * radius)) {return false;}
}
if (circx>=right && circy>=bot)
{
if (SqrDist(circx,circy,right,bot) > (radius * radius)) {return false;}
}

return true;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by phil67rpgI need to know how to detect how a ball bounces off one of the top bricks.


What do you mean exactly by that?

Do you want to know how to detect the collision?
Do you want to know how to reflect the ball back towards the bottom?

Do you have the code that works for bouncing the ball off of the walls?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!