# collision response prob

This topic is 4688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi Guys, I'm working on a poolgame in C++ and Qt. It's a 2d pool game and i use a QCanvas with QCanvasitems on (just a painting of a table with sprites as objects). Every QCanvasitem does have a XVelocity() and YVelocity(), together they form a resulting vector... For now my collisions with walls work fine. Now I have some troubles with the collision between 2 balls... I've read a lot and found this : http://www.gamasutra.com/features/20020118/vandenhuevel_03.htm. Now my question : can someone explain me how I have to work with Xvelocity and Yvelocity , in the article they use things like : "Vector n = circle1.center - circle2.center;" <- do they mean the (x,y) middlepoints of the 2 balls ? And the resulting vector is both the sum of X,YVelocity's ? If somebody could help me with this , I would be very thankfull ! Greetz and thx in advance ! Unzip *don't mind my bad english*

##### Share on other sites
Sounds like you just need to brush up on vector math a little, and this will start to make more sense.

Yes, circle.center is the middle point of the ball. Vector addition and subtraction are component-wise, so this code:

Vector n = circle1.center - circle2.center;

Does this:

Vector n;
n.x = circle1.center.x - circle2.center.x;
n.y = circle1.center.y - circle2.center.y;

'Hope that helps.

##### Share on other sites
you're damn right :)

##### Share on other sites
circle1.setMovementVector(v1');
circle2.setMovementVector(v2');

How do I calculate the Xvelocity and YVelocity when I've got the circle1's middlepoint and the new vector V1 (with X en Y -points) ?

Somebody know a good site where I can learn a bit more about useful vectorcalculations ? thx

##### Share on other sites
Hi , can anyone plz help me with this code !?

I have a "this"-item vector with a xVelocity(), yVelocity() and x() & y()-coordinate.

The code is based on this : http://www.gamasutra.com/features/20020118/vandenhuevel_03.htm

I really have to know if my fault is in this code or elsewhere !
Ruben

code :

// get the second ball from a collision listCBal* bal2 = (CBal*) lijst.last();	// make a new vector n CBal* n = new CBal();// set the X and Y value of the vector n (substract point of the 2 ball-vectors) n->setX(x() - bal2->x());n->setY(y() - bal2->y());// normalize the n-vector double t = sqrt(n->x()*n->x() + n->y()*n->y());n->setX(n->x()/t);n->setY(n->y()/t);// do the dot-product between the movement-vectors and the n-vectorfloat a1 = (x() * n->x() + y() * n->y());float a2 = (bal2->x() * n->x() + bal2->y() * n->y());// calculate the optimized pfloat optimizedP = (2.0 * (a1 - a2)) / (giveMass() + bal2->giveMass());// calculate the new movement vectors// from ball 1setXVelocity(xVelocity() - optimizedP * bal2->giveMass() * n->x());setYVelocity(yVelocity() - optimizedP * bal2->giveMass() * n->y());// from ball 2bal2->setXVelocity(bal2->xVelocity() - optimizedP * giveMass() * n->x());bal2->setYVelocity(bal2->yVelocity() - optimizedP * giveMass() * n->y());// move ball 1 with a xVelocity and yVelocity  moveBy(xVelocity(),yVelocity());// move ball 2 with a xVeloctity and yVelocitybal2->moveBy(bal2->xVelocity(),bal2->yVelocity());

##### Share on other sites

This topic is 4688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628688
• Total Posts
2984243

• 16
• 13
• 13
• 10
• 10