You Are Here:

Community: Wiki

This page was last modified on 10 September 2009, at 19:56.

Collision for Balls

From Forum Nokia Wiki


Reviewer Approved   

This article describes the physics involved in the collision reaction between two balls. 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.PI
radians = 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 Sampaio
Modified: 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/dx
colision_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;
 
}


Image:Collision1.JPG

Picture 1

Image:Collision2.JPG

Picture 2

Download

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fCS001215E5fE2dE5fAddingE5faE5fcontactE5fentryE5finE5fFlashE5fE4citeX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZflashQ qfnZtopicQUqfnTopicZflashE5fliteQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx3E2E0000X