# attack of the atari remakes

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

## Recommended Posts

say i have two circles that move in 2d with realistic physics and i have public X as double public Y as double public Xv as double public Yv as double Im using that pythagoras to detect collions between two spheres but when they collide i want them to bounce realisticly , as opose to just reversing direction even on a grazing blow.

##### Share on other sites
You need to reflect their velocity through the collision plane, which will be perpendicular to the vector connecting the centers of the balls.

##### Share on other sites
...at the moment of impact.

float tcoll; // time of collision that you've found

// move at moment of impact
A.Pos += A.Vel * tcoll;
B.Pos += B.Vel * tcoll;

// normal of collision plane = vector separating ball centres
Vector N = B.Pos - A.Pos;

// distance between ball centres
float n = N.Length();

// normalise collision plane normal
N /= n;

// relative velocity
Vector V = B.Vel - A.Vel;

// impact velocity on the collision plane
float vn = V.Dot(N);

// balls are moving away already
if (vn > 0.0f) return;

// the collision impulse.
float Cor = 0.4f // coefficient of restitutio, or amount of 'bounce'.
V = N * (-(1 + CoR) * vn);

// apply response to balls
float ima = 1.0f / A.mass; // inverse mass of ball A
float imb = 1.0f / B.mass; // inverse mass of ball B

A.Vel -= V * (ima / (ima + imb));
B.Vel += V * (imb / (ima + imb));

##### Share on other sites
Quote:
 Original post by oliii..colliiision code..

And some nice pictures to go with that.

##### Share on other sites
OP - could you edit the title of this post? It's title really doesn't give away that you are actually talking about collision detection! Glad I stumbled upon this though, going to track and see what people post [smile]

##### Share on other sites
kaze: as olii has done, you will probably find things are easier if you look at the vast number of things you have that have an X and a Y, and create a struct/class/whatever that contains an X and a Y, and use those instead.

class Vec2f{public:    float operator[](int i) { return x; }private:    float x[2];};class Thingy{...    Vec2f mPosition;    Vec2f mVelocity;...};

When you get deeper into game physics, this will make things much easier, and save you quite a bit of typing. That's all a vector is, for now. Adding operators to it is nice aswell, then oliis code stops being pseudo and starts working just as it is.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 14
• 30
• 9
• 16
• 12