View Full Version : Collision direction

1st November 2013, 17:12
I'm trying to make a simple game. I have some items in QGraphicsView, and I'm trying to make a ball to bounce off of them. Here's the problem: I can detect when a collision happens, but I don't know in which direction the ball should bounce. For that I would need to find out the tangent of the surface of the item. The items can be of any shape, and the collision should look physically correct. Any help appreciated.

1st November 2013, 22:06
you will need to know the bounding shape. Is this splined/smooth? or straight line polygons?

2nd November 2013, 09:12
I'm sorry, I didn't fully understand the question. Most of the items are circles. In addition, there's one item that restricts the game area, it has curves which can be practically any shape. It can be thought of as a rectangle with rounded corners, and the ball must stay within it. Is there any way to get the cutting points of the shapes of these 2 colliding items? In that case, the angle of the tangent would be (y2-y1)/(x2-x1).

2nd November 2013, 23:06
get the path() and use toFillPolygon to get a polygon representation of the item shape. The rest is up to you to look for algorithms. You need to match the collision point to somewhere on the path boundary path, and then determine the tangent. I don't think Qt will help you here.

Added after 9 minutes:

You might like to look into cinder.

3rd November 2013, 21:03
The best approach is external library. Box2d will do this quite well. Once I've used it with QGraphicsView and results where very good.

4th November 2013, 14:35
Thanks for the advice, but because this is a course project, I'm not allowed to use external libraries.

4th November 2013, 15:00
Maybe QPainterPath::angleAtPercent() will be of use.