Wednesday, March 28, 2012

Collision Of Balls On Move

The moving balls, collision of balls on motion is a nice example of creating user controls with properties and events. I tried the program first in earlier 2001, but got it after a long hard work in 2002. When i used programs as moving a ball by timer and bounced by the the 4 boundaries, top, bottom, left and right easily then tried the collision of balls together as well as the walls

Moving a ball can be created by setting the x and y increment values randomly to a ball and adding the value to the left and top properties of the ball (a circle shape filled with a color), if the value is positive it will add if negative it will decrease at the same time detecting the position reaching any one of the 4 boundaries; once reached, we can bounce back the ball by changing the sign of the value.

Here I created three separate programs, one the balls user control program to create a ball with gradient filled color as created a Color property, second program is Container user control, here we can set boundaries and add balls, there is one event Accident which detect the balls coming to collide and raise the Accident event, the third program is the user program by this program we can add the user controls ball and the container, each ball is added to the container where all the balls are detected and turned the path according the speed of the ball and the direction
In the container user control, a collection object holds the ball objects and by timer it moves the balls with speed of the ball to the direction both are randomly initialized when the ball is added

The both values defines a x increment value and a y increment value with sign and the balls are moved with the values on each execution of the timer event

And each time the position are checked whether any ball is collide with the wall to turn the direction or with any other ball to raise the Accident event. The event arguments provide the details such as the balls come under collide etc..

How it detects the Accident
By the count of the balls collection, it checks each ball with others

No comments:

Post a Comment