MATLAB – Air Hockey Code Explanation – Part 1

My air hockey code has been something I’ve wanted to explain a bit more — and so in an effort to get somewhere with it, I’ll be splitting it up into parts; blogging as I have free time. In the first part of my explanation, I’ll be focusing on some overarching object oriented ideas as well as constructors.

CLICK HERE! to view the code in a new browser window/tab and we can walk through it together.

First thing you’ll notice is that there are four .m files needed to run the code: main.m, puck.m, cursor.m, and goal.m. main.m is the script that drives everything. The other three files are classdef’s, that is, class definitions. I like to think of classdef files as blueprints, describing how an object lives and interacts with its world.

For now, let’s skip main.m and dive into the first class: puck.m. After line 1, which declares, ‘this file describes objects in the puck class!’ we see two headings: properties and methods. Properties are just that: the properties that describe a puck. Here, we see five properties: x_pos and y_pos (a puck’s x and y position), and x_dot and y_dot (‘x dot’ and ‘y dot’ refer to the difference in positions in one time step). These four properties control where the puck is in the field and how fast it is moving. For now, we’ll ignore the on property since I don’t use it in the code.

Let’s move on to the methods. While properties describe a puck, methods control how these properties can change. The first method, puck, is a constructor. That is, it is used to ‘construct’ a puck object. On line 11, b is the variable that holds the puck object as we create it. Once we are finished modifying b, the constructor hands the initialized object back to whoever asked for a puck.

Next, we initialize the four different parameters of a puck. As you can see, all pucks start at (250, 250), but their velocities are somewhat different. The MATLAB function rand() gives a random value between 0 and 1. So let’s roll an imaginary die that only goes between 0 and 1. If we end up with a value less than 0.5, then we move to line 17; if we happen to roll greater than 0.5, then we move to line 19. Lines 17 and 19 are mutually exclusive, that is, only one line can be reached, never both. Comparing the two lines, you can see that they’re pretty much the same except for the -1 in front of line 19. In the end, the constructor sets the x velocity to a number between 5 and 10 if you roll less than 0.5. If you roll 0.5 or above, then the x velocity is set to a number between -5 and -10. A similar calculation is done for the y velocity.

Next time, I’ll be delving into the update function within the puck class.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s