# Archived:Calculating ball collision physics in Flash Lite

We do not recommend Flash Lite development on current Nokia devices, and all Flash Lite articles on this wiki have been archived. Flash Lite has been removed from all Nokia Asha and recent Series 40 devices and has limited support on Symbian. Specific information for Nokia Belle is available in Flash Lite on Nokia Browser for Symbian. Specific information for OLD Series 40 and Symbian devices is available in the Flash Lite Developers Library.

This Flash Lite article describes the physics involved in the collision reaction between two balls.

Article
Created: Felipe.Sampaio (08 Sep 2009)
Last edited: hamishwillee (14 May 2013)

First, we need insight into trigonometry to understand how to calculate the new direction and velocities of the balls of collision.

Knowing that Flash uses radians instead of degrees, so needs to do conversion, this way:

`degrees = radians * 180/Math.PIradians = degrees * Math.PI/180`

Making an analogy with the clock the 0° angle corresponds three hours, 90° angle it´s twelve hours and so on.

Remember that cosine function, defined as the ratio of the adjacent side to the hypotenuse, the sine function as the ratio of the opposite side to the hypotenuse and the tangent function as the ratio of the opposite side to the adjacent side. You can read about the basics of trigonometry here.

This is enough to understand the code below.

`/*Project: Flash Lite Effort - Embedded Systems and Pervasive Computing Lab.Author: Felipe SampaioModified: 09/08/2009*/ //Determining distance between center of balls with the Pytagorean theorem.dx = ball1._x - ball2._x;dy = ball1._y - ball2._y;distance = Math.sqrt(dx * dx + dy * dy); //If the distance is less than ball1.radius + ball2.radius, balls collided.if (distance <= (ball1.radius + ball2.radius)    collision(ball1, ball2); function collision(ball1, ball2){ dx = ball1._x - ball2._x;dy = ball1._y - ball2._y; //Calculate the angle of collision with dy/dxcolision_angle = Math.atan2(dy, dx); //Calculate the vector velocity of the both balls.speed1 = Math.sqrt(ball1.vx * ball1.vx + ball1.vy * ball1.vy);speed2 = Math.sqrt(ball2.vx * ball2.vx + ball2.vy * ball2.vy); //Calculate the angle formed by vector velocity of each ball, knowing your direction.direction1 = Math.atan2(ball1.vy, ball1.vx);direction2 = Math.atan2(ball2.vy, ball2.vx); //this step is needed because we have balls are moving on a 2D environment (picture 1), so rotating the component x of vector//velocity to overlapping the line formed by the center of balls, with this axis transformation the collision happens//in a 1D environment (picture 2): along the x axis.vx_1 = speed1 * Math.cos(direction1 - colision_angle);vy_1 = speed1 * Math.sin(direction1 - colision_angle);vx_2 = speed2 * Math.cos(direction2 - colision_angle);vy_2 = speed2 * Math.sin(direction2 - colision_angle); //The only thing we need to solve is an elastic collision along one axis is a law of//conservation of momentum. Know that in an elastic collision total kinetic energy//is the same before and after the collision and total momentum remains constant throughout the//collision, whereas we have an isolated system. //speedAfterA = (speedBeforeA * (mA - mB) + 2 * mB * speedBeforeB)/(mA + mB)//speedAfterB = (speedBeforeB * (mB - mA) + 2 * mA * speedBeforeA)/(mA + mB)//if mA = mB, then://speedAfterA = speedBeforeB//speedAfterB = speedBeforeA final_vx_1 = ((ball1.mass - ball2.mass) * vx_1 + (ball2.mass + ball2.mass) * vx_2)/(ball1.mass + ball2.mass);final_vx_2 = ((ball1.mass + ball1.mass) * vx_1 + (ball2.mass - ball1.mass) * vx_2)/(ball1.mass + ball2.mass); //Not change because this is an 1D environment collision.final_vy_1 = vy_1;final_vy_2 = vy_2 //It´s time to convert back the velocities to standard system cartesian. Math.PI/2 used because//the angle between vx and vy is always 90°.ball1.vx = Math.cos(colision_angle) * final_vx_1 + Math.cos(colision_angle + Math.PI/2) * final_vy_1;ball1.vy = Math.sin(colision_angle) * final_vx_1 + Math.sin(colision_angle + Math.PI/2) * final_vy_1;ball2.vx = Math.cos(colision_angle) * final_vx_2 + Math.cos(colision_angle + Math.PI/2) * final_vy_2;ball2.vy = Math.sin(colision_angle) * final_vx_2 + Math.sin(colision_angle + Math.PI/2) * final_vy_2; }`
Picture 1
Picture 2