PDA

View Full Version : Collision direction



Erkki2
1st November 2013, 16: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.

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

Erkki2
2nd November 2013, 08: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).

amleto
2nd November 2013, 22: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.

MarekR22
3rd November 2013, 20: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.

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

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