# 2d elastic collision of two round objects

1. Sep 5, 2008

### realgreatbass

Hi.

I'm new to vector physics, and I'm trying to write a function to predict the rebound vectors of two circles impacting. To this end, I wrote the following function in Flex. My problem is that the y components that get calcuated ( v1_final_y and v2_final_y ) are always equal to the original y components. I'm doing something wrong, and I thought I understood what I was doing, but I must be miscalculating something. Can anyone point me in the right direction?

public static function ReboundCircleToCircle( aVector:CCommonVector,
bVector:CCommonVector ) :void
{

/* CALCULATE THE NORMAL VECTOR */
var n_x:Number = bVector.x - aVector.x;
var n_y:Number = bVector.y - aVector.y;

/* CALCULATE THE LENGTH OF THE VECTOR */
var vx_len:Number = Math.sqrt( Math.abs( ( n_x * n_x ) + ( n_y * n_y ) ) );

/* CALCULATE THE UNIT NORMAL VECTOR */
var un_x:Number = n_x / vx_len;
var un_y:Number = n_y / vx_len;

/*
* THE UNIT TANGENT VECTOR IS A PERPENDICULAR LINE TO THE
* UNIT NORMAL. SO X = -Y AND Y = X
*/
var ut_x:Number = -un_y;
var ut_y:Number = un_x;

/*
* PRE-COLLISION VELOCITY VECTORS, IF NOT
* ALREADY VECTORS, ARE JUST THE X & Y COORDS
* OF THE INITIAL VELOCITIES
*/
var v1_x:Number = aVector.x;
var v1_y:Number = aVector.y;
var v2_x:Number = bVector.x;
var v2_y:Number = bVector.y;

/*
* NOW WE FIND THE PRE-COLLISION SCALAR, OR DOT-PRODUCT
* OF EACH OBJECT ALONG THE UNIT NORMAL AND UNIT TANGENT
* VECTORS.
*/
var v1n:Number = ( un_x * v1_x ) + ( un_y * v1_y );
var v1t:Number = ( ut_x * v1_x ) + ( ut_y * v1_y );
var v2n:Number = ( un_x * v2_x ) + ( un_y * v2_y );
var v2t:Number = ( ut_x * v2_x ) + ( ut_y * v2_y );

/*
* THE VELOCITY SCALAR ON THE TANGENT VECTOR BEFORE AND AFTER
* COLLISION IS THE SAME....
* I'M REFERRING TO POST-COLLISION CALCULATIONS
* AS PRIME...
*/
var v1t_prime:Number = v1t;
var v2t_prime:Number = v2t;

/*
* FIND THE NEW "NORMAL" VELOCITIES, OR THE VELOCITIES
* ON THE NORMAL VECTOR. USE 1D COLLISION FORMULAS.
*/

var v1n_prime:Number = ( ( v1n * ( aVector.Mass - bVector.Mass ) )
+ ( ( 2 * bVector.Mass ) * v2n ) ) / ( aVector.Mass + bVector.Mass );
var v2n_prime:Number = ( ( v2n * ( bVector.Mass - aVector.Mass ) )
+ ( ( 2 * aVector.Mass ) * v1n ) ) / ( aVector.Mass + bVector.Mass );

/*
* CONVERT THE SCALAR NORMAL AND TANGENTIAL VELOCITIES INTO VECTORS
* TO DO THIS,
* WE MULTIPLY THE UNIT NORMAL VECTOR BY THE SCALAR NORMAL VELOCITY.
*
* THE PRODUCT OF A VECTOR TIMES A SCALAR IS A VECTOR
* WHOSE COMPONENTS
* ARE BOTH THE ORIGINAL VECTOR COMPONENTS MULTIPLIED BY THE SCALAR.
*/
var v1n_prime_vector_x:Number = un_x * v1n_prime;
var v1n_prime_vector_y:Number = un_y * v1n_prime;

var v2n_prime_vector_x:Number = un_x * v2n_prime;
var v2n_prime_vector_y:Number = un_y * v2n_prime;

var v1t_prime_vector_x:Number = ut_x * v1t_prime;
var v1t_prime_vector_y:Number = ut_y * v1t_prime;

var v2t_prime_vector_x:Number = ut_x * v2t_prime;
var v2t_prime_vector_y:Number = ut_y * v2t_prime;

/*
* FIND THE FINAL VELOCITY VECTORS BY ADDING THE NORMAL AND
* TANGENTIAL COMPONENTS
* FOR EACH OBJECT.
*
*/
var v1_final_x:Number = v1n_prime_vector_x + v1t_prime_vector_x;
var v1_final_y:Number = v1n_prime_vector_y + v1t_prime_vector_y;
var v2_final_x:Number = v2n_prime_vector_x + v2t_prime_vector_x;
var v2_final_y:Number = v2n_prime_vector_y + v2t_prime_vector_y;

/*
* UPDATE THE VELOCITIES OF THE TWO OBJECTS
*/
aVector.VelocityX = v1_final_x - aVector.x;
aVector.VelocityY = v1_final_y - aVector.y;
bVector.VelocityX = v2_final_x - bVector.x;
bVector.VelocityY = v2_final_y - bVector.y;

/*
* UPDATE THE X AND Y COORDINATES OF THE TWO OBJECTS
*/
aVector.x += aVector.VelocityX;
aVector.y += aVector.VelocityY;
bVector.x += bVector.VelocityX;
bVector.y += bVector.VelocityY;

}