PDA

View Full Version : determining how to bounce the ball



Binji
24th December 2007, 02:01
Well first of all im still a newbie at Qt so i hope im not asking anything blatantly obvious. That said let's move to the problem...
Im writing a game similar to Pong (bouncing the ball around the screen and into blocks) and i dont know how to implement ball's collision detection.
First of all if using the collidingItems method i dont know how to determine if the ball hit the block or the paddle (i have to treat them differently).
Second i dont know how to figure where the ball hit the object so i know how to bounce it off it properly (in the right direction).

Im using QGraphicsItems on a QGraphicsScene.

Thanks for your help:)

P.S. If it makes a difference i use Qt Jambi but it still looked like the right forum for this

high_flyer
24th December 2007, 10:52
First of all if using the collidingItems method i dont know how to determine if the ball hit the block or the paddle (i have to treat them differently).
You are getting a list of items as return value.
Check the list for either block or paddle.

Second i dont know how to figure where the ball hit the object so i know how to bounce it off it properly (in the right direction).
Hmm... for some reason I was sure a signal is emitted, but I don't see that in the docs...
If you are using custom items (subclasses of QGraphicsItem) then you can build a timer in the item that will periodically check if it is colliding.
Hmm... not so good, as you just might miss a short collision...
So have a flag set when colliding, and check the collision flag with the timer maybe...
If not, you can do that on a higher level with collidingItems().

EDIT:
actually, you wont miss collisions with a timer, since you are advancing your items, so you can do that every time you advance them.

aamer4yu
24th December 2007, 11:52
When you get a list of items, u can check to which class it belongs using dynamic cast,,, or u can use a extra variable in the class.
For the graphics item inherit the class something like -

class MyGraphicsItem: public QGraphicsItem
{
Q_OBJECT; // in case you need signal slots...

enum itemType {type1 = UserType+1,type2,...};
itemType m_type;
int type() const /// this is a virtual function of QGraphicsItem ,,, refer documention.
{
return m_type;
}
};

hope this helps :)

wysota
24th December 2007, 13:49
When you get a list of items, u can check to which class it belongs using dynamic cast,,, or u can use a extra variable in the class.

qgraphicsitem_cast() is the way to go.

Binji
26th December 2007, 19:04
Thanks i (think i) understand how to detect objects now.
How about the second half of the question? Any ideas? Maybe if i try translating current position of the ball to the hit item's coordinate system and see if it does? Or is there a better way?

wysota
26th December 2007, 20:53
QGraphicsItem::collidingItems() returns a list of items colliding with a particular item.

Binji
26th December 2007, 23:31
QGraphicsItem::collidingItems() returns a list of items colliding with a particular item.
I meant how to determine how to bounce the ball off the hit object. Or am i missing something?

wysota
26th December 2007, 23:37
Hmmm... what do you mean "how"? You mean physics?

BTW. Have you seen this?
http://doc.trolltech.com/qq/qq20-jambi.html

Binji
29th December 2007, 11:08
I have seen that example but except for some code for working with the scene there isnt really anything i need.
And to explain what i need... right now i have a ball object which has x and y velocity. So every time a time trigger triggers i move it by that values. But now i dont know whether the ball hit something by its x axis (where i have to invert the x velocity) or y axis (where i have to invert the y velocity).
One idea i had is to move the ball only by x or y every time i have a time trigger event and then check for collision but im not sure if its such a good idea.
So if you have a better idea it would really mean a lot:)
Thanks

edit: damn im dumb.. if i get a list of items colliding with the ball i can simply check which coordinates equals those of the ball. :o

wysota
29th December 2007, 12:24
edit: damn im dumb.. if i get a list of items colliding with the ball i can simply check which coordinates equals those of the ball. :o

They might not be "equal". Collision is determined by shape, not by the centre point, so you can hit "by the corner" as well and "harder" in one direction than in the other. But these are all simple calculations.