Elastic Collisions - Page 2 by
alex nino and kirupa | 25 April 2010
In the
previous page, you learned a bit about elastic
collisions and how they fit in with the physical laws that
we are subjected to! In this page, let's look at two quick
examples of Flash applications that implement elastic
collisions.
Revisiting the Energy Ring
If you saw the video in the previous page, you saw a
circular ring with colorful blocks colliding with other
blocks and bouncing back and forth. What is unique about the
collision is that the path all objects move in is constrained
by a circular, ring shape.
Below, you will find an example of the energy ring
created in Flash:
You can download the source code for the above example
here:
First of all, as you can tell from my version, I focused
only on the movement and the collision. Some of the nice
artistic touches like the small flying particles cannot be
found in this. I'll leave it as an exercise to you, the fair
reader, the add those nice finishing touches!
If you saw the source code for the bouncing blocks in the
previous page, you'll see a lot of similarities in this
version. What you may find useful is how to look at elastic
collision from not just the horizontal/x-axis collisions but
also taking the vertical/y-axis as well. Add in a bit of
gravity and simulating the loop using a sine curve, and
hopefully you find the source code valuable to look into.
More 2D Collisions
The final example I am going to show is a pure 2D
implementation of elastic collisions involving the typical
billiards/pool scenario:
Move your mouse around the white ball to adjust the angle
you are interested in hitting the ball with the pool stick.
Click on the pool stick and move it back to adjust the force
with which you want to hit the ball.
As you can tell by playing with this for a few minutes,
this is something that I haven't fully finished! If you
pocket the white ball, the ball simply just appears behind
the line haha. It is really more of a technical demo than a
game...for now. I will provide the full source file for this
shortly once I have finished it as well.
To make up for not providing the source file, I will
share how I am specifying the elastic collision. In this
example, I am optimizing for performance because I want the
response from a collision to be nearly instantaneous:
One way I am able to get better performance is because I
am not using the Cosine and Sine functions. The downside is
that some of the rotational tricks you can perform on a real
game of billiards or pool aren't possible, but the upside is
blink-of-an-eye collision detection and reaction!
Conclusion I hope this
article helped get you started with elastic collisions and
how to implement them in your applications.
One thing I
want to emphasize is that you are free to choose whatever
approach you want for collision detection independently
of what approach you want to take for the reaction to the
collision. Kirupa has a few
articles on Collision Detection already written, so check
them out by
clicking here. This article provides you with some
examples of how to react to the collision. A clean reaction
to the collision where momentum is preserved is...an elastic
collision!
If you have any questions or want to learn more, visit
yoambulante.com or post on the forums
here.