Sign in to follow this  
Unzip Attack

collision response prob

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 this post


Link to post
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;

Same for addition.

'Hope that helps.

Share this post


Link to post
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 this post


Link to post
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 !
Thx in advance !
Ruben



code :


// get the second ball from a collision list
CBal* 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-vector
float a1 = (x() * n->x() + y() * n->y());
float a2 = (bal2->x() * n->x() + bal2->y() * n->y());

// calculate the optimized p
float optimizedP = (2.0 * (a1 - a2)) / (giveMass() + bal2->giveMass());

// calculate the new movement vectors
// from ball 1
setXVelocity(xVelocity() - optimizedP * bal2->giveMass() * n->x());
setYVelocity(yVelocity() - optimizedP * bal2->giveMass() * n->y());

// from ball 2
bal2->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 yVelocity
bal2->moveBy(bal2->xVelocity(),bal2->yVelocity());


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this