Public Group

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.

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 on other sites
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 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?

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• Forum Statistics

• Total Topics
633768
• Total Posts
3013740
×