Circle Collision Responce
Author |
Message |
matt271
![](http://compsci.ca/v3/uploads/user_avatars/1721484549ee55140af00.png)
|
Posted: Mon Apr 06, 2009 10:03 pm Post subject: Circle Collision Responce |
|
|
hi alls
i am just new to this forum, so i would like to contribute something
i have come up with something interesting as part of my final project in java, and i thought it might be something good i can contribute here
i wanted to make a way for circles to collide and bounce off each other in a way that feels and looks real
here is the logic:
imagine 2 circles, called "i" and "j"
"i" collides w/ a still "j" at some velocity v
the circle "j" will get moved at some velocity v_j in the *same direction as the displacement
therefore the triangle representing v_j is a similar triangle to the triangle representing d.
let us define the ratio between v_j and d to be k. therefore d = k_j * v_j
now let us consider the disk "i" after the collision. for the collision to be an elastic collision "i" would have to move perpendicularly to "j"
m = -1/m so the rise and run of v_i are switched. also one is negative. from my diagram you can see i picked the run to be negative because it points the op way of the x component of the displacement
now this is also a similar triangle to d like before.
u still with me? haha
ok now the math:
first lets define displacement in terms of x and y:
now the total velocity before the collision must equal the total velocity after the collision (we assume all circles have mass of 1 unit)
now let us solve for k_j:
multiply both sides of the equations such that we will get a term and its negative to eliminate k_i
add these equations together:
to get:
now we know (i hope) that
therefore:
now do the same to solve for k_i:
(there is a type. it should be over d^2. i am not making a new image to fix it)
and from the diagram we know that the components of the vectors are:
ok but this is only if one of the disks is still so its pretty useless right? wrong.
we can assume any one collision is really two collisions. a collision between "i" and a still "j", then before any velocities change, another collision between "j" and a still "i" then add the two resulting velocities together to get a final velocity for both circles after the collision
working out the collisions this way does work, because the total velocity before the collision is equal to the total velocity after the collision. but this only means momentum was conserved, we could have still got the directions wrong. but (v_i)^2 + (v_j)^2 before = (v_i)^2 + (v_j)^2 after. this means that kinetic energy was also conserved. therefore it is a static collision, aka, right.
here is what i coded in java:
Java: | /**
* calculates the velocity of 2 disks after they collide
*
* if they do not collide it will just return false
*
* works by saying the vector of the velocity of disk j due to i after the
* collision is a similar triangle to the vector of the displacement from
* disk i to j and the vector of disk i due to i is perpendicular to j
*
* the same concept is applied again to the disks but due to j. then the
* final velocities of disks i and j are these velocities added together
*
* i like math :D
*
* @param i first disk
* @param j sec disk
* @return true if they collide
*/
private boolean calcCollision(Disk i, Disk j) {
double x, y, d2;
// displacement from i to j
y = (j.y - i.y);
x = (j.x - i.x);
// distance squared
d2 = x * x + y * y;
// dividing by 0 is bad
if (d2 == 0.)
return false;
// if d^2 < (30)^2, the disks have collided
if (d2 < 900.) {
double kii, kji, kij, kjj;
kji = (x * i.dx + y * i.dy) / d2; // k of j due to i
kii = (x * i.dy - y * i.dx) / d2; // k of i due to i
kij = (x * j.dx + y * j.dy) / d2; // k of i due to j
kjj = (x * j.dy - y * j.dx) / d2; // k of j due to j
// set velocity of i
i.dy = kij * y + kii * x;
i.dx = kij * x - kii * y;
// set velocity of j
j.dy = kji * y + kjj * x;
j.dx = kji * x - kjj * y;
return true;
}
return false;
} |
i have posted the entire source code at http://compsci.ca/v3/viewtopic.php?t=20676
enjoy
i would like some comments plz ![Very Happy Very Happy](images/smiles/icon_biggrin.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
matt271
![](http://compsci.ca/v3/uploads/user_avatars/1721484549ee55140af00.png)
|
Posted: Sat Jun 20, 2009 12:17 pm Post subject: Re: Circle Collision Responce |
|
|
u all suck ![Very Happy Very Happy](http://compsci.ca/v3/images/smiles/icon_biggrin.gif) |
|
|
|
|
![](images/spacer.gif) |
Cruncher222
|
Posted: Thu Nov 05, 2009 1:24 pm Post subject: RE:Circle Collision Responce |
|
|
Dude, this looks good and all, but some of those variables you are refering to came out of nowhere. do you have msn or something, that i can talk to you about this? |
|
|
|
|
![](images/spacer.gif) |
|
|