A popular introductory app is BallBounce, provided by Hour of Code, which demonstrates a single ball bouncing around the screen. As a development of this app, two new apps have been added to the UniTY App Library which demonstrate how to manage the interactions between two balls, and between three or more balls.
When two balls collide, each generates its own collision event: it is only necessary to handle one such event in order to calculate the new heading for each ball. With two balls on screen, this can be achieved by writing an event handler for one ball and ignoring the other. Refer to X10_TwoBalls in the UniTY folder.
However, with three or more balls, a more flexible de-duplication strategy is required, as any two of the active balls could collide. In this example, we use the position of the colliding balls in a list to determine which collision event to handle and which to ignore. Refer to X11_ThreeBalls in the UniTY folder.

No comments:
Post a Comment