Let’s get back into this code.

Again, you can CLICK HERE to open up the code.

So let’s talk about the puck class’s update function. You’ll find this method starts at line 31 of puck.m. Compared to the constructor, the update function has many more different inputs and outputs. One thing you’ll notice is that both b and count are found as an input and output. b stores our individual puck object that we are updating and count holds the score of our game.

In addition, other variables are passed to the update method as inputs. If you look ahead at the code, you’ll notice that there are a lot of different areas I commented as collisions. These inputs signify the different things that can collide with the a puck. c is a cursor, walls signifies the walls of the playing field, and br_1 and br_2 are the goals of the air hockey field (I call them bricks here because I’ve also used them to make a “brickles” game in MATLAB.)

Before we collide with anything, though, we first move the puck. Starting at line 32, we see that the first thing that the code does is add x_dot to x_pos and then y_dot to y_pos. The effect is that if the puck is going in one direction, it will keep moving in that direction, at that speed.

Next, starting at line 36, we check whether or not a puck has collided with the walls. The playing field is defined by walls which is a vector of size 4. Elements 1 and 2 of walls hold the x boundaries and elements 3 and 4 of walls hold the y boundaries. If the puck’s current position is outside of the x boundaries, then the direction of its x velocity is flipped by multiplying by -1. Thus, the next time the puck’s position is updated, it moves back towards the center of the field. A similar calculation is done with the y boundaries and y velocity.

For cursor collision, starting at line 45, we compare the position of the cursor (held in the variable c) to the position of the puck. In general, if the puck is within a certain radius of the cursor (held in the variable dist), then we change the ball’s velocity. For the finer detail, you might notice that x_delt and y_delt — literally, x and y delta — are calculated from the cursor to the ball. Because of this, by we don’t have to use a -1 to change the direction of the velocity vector. By simply scaling this delta, we can set a new velocity for the puck. The scaling is proportional to dist the amount that the puck is inside the cursor. By using dist like this we allow the player to modify the overall speed of the puck. If the player wants to increase the velocity of the puck, they can move the cursor quickly towards the puck. Doing so will cause the puck to be closer to the center of the cursor during the collision, decreasing the value of dist and increasing the puck’s velocity since the velocity is inversely proportional to dist.

The final collision check is between the puck and the bricks or goals. This one is simple. Basically, if the puck collides with the upper goal, the score is increased by one and if the puck collides with the lower goal, the score is decreased by one.

Next time I’ll discuss some finer points, like how the cursor is mapped to the playing field.

Advertisements