A simulation is available at: https://dl.dropboxusercontent.com/u/19633784/score_test.html (check the bottom of the article for more info)

In my last posts the main topic at hand has been how to get balls to bounce around in a labyrinth of right-angled walls. However, there hasn’t been much playability as of yet, only an animation of the balls bouncing. So I’ve introduced a player entity now, which has a position and a radius – a player is also sphere as of yet. Now, in the game the point is to *avoid the balls*. You get points for doing dangerous manoeuvres, i.e. being close to the balls. We’re going to make some assumptions:

- Being close to the ball is harder than being further away from the ball.
- It’s harder when the ball is heading towards the player, rather than heading away from it.
- It’s harder to avoid the ball when it has a huge velocity than when it’s going slow.
- Let be the score, and the difficulty. Then .

We’re going to break this down into three parts. In particular, we have a scoring function . In practice however, we’ll look at the *derivative *of this function, because we want to know how much to increase the score with each game tick. This can also be stylized as , where is the distance function, is the direction function, and is the velocity function. We’re going to look at the distance function first.

The distance function has a maximum value when the player is as close to the ball as possible without dying. If we let be the width of the ball, and be the width of the player, then obviously the closest possible distance is . We call the shortest possible distance for , and therefore . We want the distance function to decay exponentially, so going further away by constant distance decays the score by a constant factor. In other words, we can express the distance function as , where is some constant.

Next up is the direction function . The range of the function is , where corresponds to the situation where the ball is going exactly towards the player, and is the situation where the ball is going in the exact opposite direction from the player. How do we calculate ?

Well, notice that we have two angles as the arguments to the function. This is because we only need two pieces of information for this – two vectors and . The vector is the vector between the ball and the player. The horizontal constituent is the difference between the x-coordinates of the ball and the player, and likewise for the vertical constituent. And the vector is the movement vector for the ball.

Okay, so now we need to calculate the dot product of and , because it just so happens that if the two vectors are orthogonal, it also so happens that when the vectors are pointing to each other, and if the vectors are pointing away from each other. Notice that the range of the dot product is , and we need to map this unto , so we define the -function as . This function will give if the ball is going towards the player and if its going away from the player.

And finally, we’ll go through the velocity function . We’re going to assume it’s twice as hard to avoid a ball that’s travelling twice as fast as another ball. In other words, there is a *linear *correlation between the velocity and the score you should receive. In other words , where is any constant.

We can now assemble the scoring function . We see that .

In practive, we choose constants , , and . So, here is the final scoring function:

If you want to, you can test it out at the following link: https://dl.dropboxusercontent.com/u/19633784/score_test.html

You are the red ball, and the game freezes when you get hit by a ball. Refresh the window to try again (sorry about this solution).