Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

TIPS AND TRICKS > Collisions between same-mass objects

Collisions between same-mass objects

Remember good old Chapter 11 and conservation of momentum? That was some pretty serious code. It happens you can make it a little bit simpler when two objects of the same mass collide. Basically, along the line of collision, such objects simply swap their velocities. While you still have coordinate rotation to determine that line of collision, as well as the object's velocities on it, this wipes out the complex conservation of momentum stuff. To see how it works, let's go back to file MultiBilliard2.as, which we'll use as the base for the next example, SameMass.as. I'm not going to list all of the code from the original here, as it's quite a big file. But let's take a look at the for loop that created all the balls:

for(var i:uint = 0; i < numBalls; i++)
{
    var radius:Number = Math.random() * 50 + 20;
    var ball:Ball = new Ball(radius);
    ball.mass = radius;
    ball.x = Math.random() * stage.stageWidth;
    ball.y = Math.random() * stage.stageHeight;
    ball.vx = Math.random() * 10 − 5;
    ball.vy = Math.random() * 10 − 5;
    addChild(ball);
    balls.push(ball);
}


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint