Collision detection problem!!!

Started by
4 comments, last by knealeaj 19 years, 10 months ago
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/
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.
--------------------------------------------------------Life would be so much easier if we could just get the source code.
sorry about the code being windows!!Could anyone give me a source example of the method Venerable Vampire is talking about.

Thanks
Hasnt someone got the source for such amethod or got a link to learn it????Thanks
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.

Everything is better with Metal.

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

This topic is closed to new replies.

Advertisement