CollSim

=CollSim The Collision Simulator=

(And 70% Imitation Physics)
By Tom Nally

This article first appeared in the Liberty BASIC Newsletter Issue #108, May, 2003. It is reprinted here with the author's permission. In this article, Just Basic has been added to the discussion, along with the original Liberty Basic. Just Basic modifications of the original code include * Removal of tooltips (required API calls) * Replacement of the API calls to Enable / Disable the Replay button with native Just Basic commands * Use of a Notice directing to this page (http://justbasic.wikispaces.com/CollSim) rather than running the help file that would require the use of a custom dll.

What is CollSim?
CollSim, the Collision Simulator, is a Liberty BASIC program which simulates the collision of six balls inside of a square enclosure. This square enclosure is called the "arena" in CollSim.

All balls begin the simulation at rest, except for the ball known as "the Instigator". The Instigator is the white ball with the Block "I" in the middle. It differs from the other balls in that it is given the assignment of starting the sequence of collisions, hence its name. In order to instigate the collisions, the Instigator always begins the simulation with an initial velocity and an initial angle of motion. In that way, it acts like a cue ball in billiards.

The user can set up a series of collisions in countless numbers of ways. He can set the initial locations of all six balls, as well as the initial velocity and angle of the Instigator. Additionally, the user can control the extent to which the balls will retain their energy after a collision. This factor is known as the "elasticity". Finally, the user can also set the overall simulation speed. This is helpful to users who may have computer systems with slower processors. (I've successfully used CollSim on my two computers, one of which is 266mHz, while the other is 1.8gHz.)



Simulation Process: Two Phases
The simulation process actually has two phases. In the first phase, the "calculation phase", CollSim will calculate the thousands of positions of all six balls during the collision sequence. No animation takes place during the calculation phase. The process of calculating the ball positions may take anywhere from 10 to 50 seconds, depending on the speed of your computer processor, as well as the "Simulation Speed" value that you select.

The second phase is the "animation phase". This phase takes place once the calculations are complete. In the animation phase, the balls move around the arena, bouncing off of the walls and bouncing off of each other.

The Collision Simulator Screen
The Collision Simulator features a single screen, shown on the right. Most of the screen real estate is taken up by the arena, the big GraphicBox which takes up about two-thirds of the total window space. Inside the arena, you will see six balls and a CrossHair. The balls are the objects which CollSim subjects to collisions once the simulation begins. The Crosshair can be considered a targeting sprite for specifying the initial direction of the Instigator.

The CollSim controls are arranged vertically on the left-hand side of the screen. The top two controls, a CheckBox and a ListBox, are used to position the balls around the arena. Below the ListBox, a second CheckBox is used to position the Crosshairs in the arena.

Below that are two ComboBoxes. The first one is used to set the initial velocity of the Instigator. The second one is used to set the elasticity of the collisions.

Below the ComboBoxes are three buttons. The "Go!" button begins the simulation process. The "Replay" button will allow the user to replay the most recent simulation. Since the ball positions have already been calculated, the Replay button starts the animation phase instantly. Replay is only effective if no other controls have changed state since the last simulation.

The "Stop Simulation" button will stop the simulation, whether the simulation is in the calculation phase or the animation phase. But stopping the simulation will also produce disordered records in the arrays which track ball positions, and will likely force the user to restart the program. See the Known Problems section of this help document.

The TextBox at the bottom of the screen serves as a status bar. The status bar provides the user insight into what activities are taking place, and what values have been selected. Originally, the status bar was merely a means to provide me, as the developer, feedback on the activities of the program. Ultimately, I decided that the status bar might be interesting and useful to the user, too. So, it remained in the program.

To the right of the status bar is a "Quit" button. Pressing Quit exits the program.



Positioning the Collision Simulator Balls
The user has near complete control over where the balls are placed prior to the commencement of the simulation. The only limitation on placement is that the program will not begin the simulation if two balls overlap. If two balls overlap at the time "GO!" is pressed, the program will provide a Notice box asking the user to move one of the balls to a clear position.

To position a ball in the arena, first set the CheckBox labeled "Enable Ball Positioning". Then in the ListBox, click the name of the ball that you wish to move. Then, move your mouse pointer to the arena. The ball selected will move with your mouse pointer around the arena. Left click with your mouse to set the ball position.

Now, take the mouse pointer back to the ListBox to select a new ball to move, and repeat the process.

The first ball, the Instigator, can be moved just like all the other balls.



Setting the Initial Angle of the Instigator
The initial angle of motion of the Instigator is set by moving the crosshair around the arena. To move the crosshair, set the CheckBox labeled "Enable Xhair Positioning". Then, move your mouse pointer to the arena. Note how the crosshair sprite will follow your mouse pointer around the arena. Note also that a rubber band line will always be stretched between the Instigator ball and the crosshair sprite. This line represents the initial direction of motion of the Instigator once the animation commences. To set the location of the crosshair sprite, left-click the mouse.



Two Kinds of "Speed"
In the Collision Simulator, there are two ways to affect how fast the balls appear to move around the arena. One way is by adjusting the initial speed of the Instigator. The second way is to adjust the overall Simulation Speed. The controls for both of these effects are discussed in more detail below.

But what is the difference between these two different kinds of speeds? Indeed, it might be difficult for the user to tell, but let me attempt to explain it by use of the basic distance equation: code format="vb" d = r * t  ...or... distance = rate * time code You probably learned this equation in algebra. If you know that rate of speed (velocity) at which an object is traveling, and the time traveled at this speed, then you can find the distance traveled.

In the Collision Simulator, the initial velocity control affects the "r" variable of the equation. The faster the initial velocity, the greater distance the Instigator will travel for a given time period.

The Simulation Speed setting affects the "t" variable of the equation. When the simulation speed is set to "Slowest", the time increment, "t", is set to a very small value. The program is forced to calculate the positions of the balls for much smaller time slices than when the Simulation Speed is set to "Fastest". Likewise, during the animation phase of the simulation, the computer must execute a "drawsprite" command for many more positions of the balls. Because it is forced to update the postions of the balls at much narrower time intervals, the simulation will run slower.

The best thing that the user can do is keep adjusting both the initial velocity of the Instigator and the Simulation Speed until he finds a condition that he finds fun and interesting.

Setting the Initial Velocity of the Instigator
The initial velocity of the Instigator is set by selecting a speed from the ComboBox just below the label "Set Instigator Velocity". The initial velocities range from 100 per second to 300 per second. The Collision Simulator doesn't really use any distance units (other than the pixel), so I've left the distance unit out of the velocity ComboBox.

Understand also that the Instigator speed on your screen is relative to the capabilities of your own system. An Instigator speed of 200 per second on your system may seem faster or slower than a speed of 200 per second on someone else's system.



Setting the Simulation Speed
As discussed above, for any given velocity of the Instigator the user can make the simulation go faster or slower by setting the Simulation Speed. The Simulation Speed is adjusted by selecting "SimulationSpeed" from the menu at the top of the application window.

In the slowest simulation speed, CollSim will calculate a total of 6000 different ball positions for each ball, and will calculate them at the rate of 100 per time unit. In the fastest simulation speed, CollSim will calculate only 2000 ball positions, and will calculate them at the rate of only 20 per time unit. In the latter case, because the program updates the ball positions less frequently, it can render a faster animation.



Setting the Collision Elasticity
To set the Collision Elasticity, click on the ComboBox below the label that says "Set Collision Elasticity". In CollSim, the elasticity can be as high as 99% and as low as 90%.

But what is the Collision Elasticity? As I understand it, "elasticity" technically refers to the amount of energy preserved after a collision occurs. In a collision that is "perfectly elastic", the collective energy of all of the participating objects in the collision is preserved. This simulation, however, commits "imitation physics", and only works with the velocity of the balls, and not their kinetic energy.

When the Collision Elasticity is set to 95% for example, this means that after a collision, the balls will assume 95% of the velocity that they would normally take on if the collision were perfectly elastic.

Note that this does NOT mean that the balls will have 95% of their pre-collision velocity. The reason for this is that the balls will almost always change velocity at collision time even when the collision is perfectly elastic. The reason for this is that momentum--a quantity different from both velocity and energy--must be conserved after a collision. In order for momentum to be conserved, the two balls involved in a collision must exchange certain vector components of their velocities.



So, Buddy, What Makes a Good Ball Collision Simulation?
I wrote the Collision Simulator to see if I could make realistic-looking ball collisions using everybody's favorite programming language, Liberty BASIC. The simulation also works quite well with Just BASIC. On my system (1.8gHz, 512MB RAM), I like to set the Simulation Speed to slow or medium, set the Instigator initial velocity to 260 per second or higher, and set the Collision Elasticity to 96% or lower. These settings give me pretty good results. Because computer systems vary so widely, experiment with the settings to get the results that you like. (On my 266mHz computer, I set the Simulation Speed to Fastest.) When you see a simulation that you like, press the "Replay" button to see it again before you go on to the next simulation.

For programmers of Liberty BASIC or Just BASIC, there is an additional tweak that you can make to the source code of the program if you so desire. If you look at the first 30 lines of the source code, you will see a variable named "VDF", which stands for "Velocity Decay Factor". In the program, VDF is set to 0.9999. The Velocity Decay Factor is used to reduce the speed of the balls during each an every cycle of position calculation. This means that even if a ball does not collide with a wall or another ball, its velocity will be reduced to 0.9999 of it's velocity during the previous calculation cycle. Doing this causes the simulation to look more realistic by inducing a very slow decay of the velocities of the balls even when they are not colliding.

Tweak VDF if you wish, but start by tweaking it very small amounts. For instance, even setting VDF to 0.99 will cause a very rapid decay of velocity, and will produce simulations which are boring, in my opinion.



CollSim Known Problems
The Collision Simulator does have a few odd behaviors:

Prior to Phase I of the simulation (the calculation phase), the program will provide the user with a Notice if the user has positioned two balls such that they are overlapping, after which it will suspend the simulation. This same Notice may be issued if the balls are one pixel distant from each other.

There are at least two ways to stop a simulation in progress: by pressing the "Stop" button, or by moving the mouse pointer to the arena. If the simulation is halted during either Phase I or Phase II, confusion will reign in the tracking of ball positions. The user will probably need to re-start the program in order to re-set the tracking system.

There are conditions which will cause two balls to momentarily overlap during the simulation, which looks wacky. This anomaly can occur if you set the Simulation Speed to Fastest, and also set the ball speed to a very fast speed, say 300 per second. When Sim Speed is set to Fastest, the positions of the balls are only checked every 1/20th of a second. In that time period, a ball traveling at 300 pixels per second can travel a distance of 15 pixels. Say that during the prior calculation cycle, a ball was only 2 pixels away from another ball. Under that condition, a collision will not be detected. During the next cycle, however, the ball will move an additional 15 pixels, putting it 13 pixels inside the ball with which it is having a collision. CollSim attempts to correct this condition during the following cycle by placing the balls in non-overlapping positions.

--- * - Download the demonstration zip file

--- Tom Nally Steelweaver52@aol.com

---

This document is the same document contained within the help file.

--- Back to Nally's Code