Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

knealeaj

Collision detection problem!!!

This topic is 5280 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

Hi again guys! I am making a breakout clone in C++. I cannot get the collision right. The ball bounces of the top and bottom of the blocks fine but when it comes to hitting the sides i just cant get it right. could you please look at my code (relevant code is in Levels.cpp)and tell me whats up. Thanks my code is at http://members.lycos.co.uk/knealeaj/files/

Share this post


Link to post
Share on other sites
Advertisement
You seem to be trying to compare the distances between the edges. This *can* work, but consider trying a different method.
Use 2D geometry that you learned in high school
You have a rectangle, a circle, and a direction that the circle is traveling, right? The direction is a 2D vector. Take each edge of the rectangle, and compute a line equation or a ray equation. Then for each edge find the closest point on the circle to the infinte line/ray. Make a new ray that starts at that point and has the same direction as the circle motion. Then intersect that ray with the edge. Do this for all 4 edges and take the closest one. Now you have the edge it collided with, because you can just store what edge has the closest intersection value on the circle direction/ray thing. Since you know what edge you have, you can bounce accordingly.
Hope this makes some sense and you can fix it!

BTW, have you considered DROPPING YOUR GAY WINDOZE CODE and writing in something portable? Like SDL? Or even Java? Come on, breakout can be writting in ANYTHING. But I can''t run WINDOZE code. So help a poor Mac user out?

--------------------------------------------------------
Life would be so much easier if we could just get the source code.

Share this post


Link to post
Share on other sites
sorry about the code being windows!!Could anyone give me a source example of the method Venerable Vampire is talking about.

Thanks

Share this post


Link to post
Share on other sites
it''s complicated.

you have several solutions, though. Do a moving sphere versus segment collisions, and find the first intersection asnd bounce the sphere on it, or you can approximate the ball to a point for the moment, and do a segment/box intersection test.

the segment would be made of the current position of the ball, and the target position of the ball. Then you intersect the ball with that box, find the side of the box intersected, and reflect off that.

http://www.gamedev.net/community/forums/topic.asp?topic_id=227445

I''ve got lots of code, but it would be more confusing than helping. Unless you are ready for some vector maths.

Share this post


Link to post
Share on other sites
you could use regions, from the api, just give one of the creatergns the points and it will create a region for you,

then just compare if the two regions touch, which requires a little messing around with combine region etc

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!