Trigonometry and Class Diagrams

We continued discussion on the research we were asked to do last week. It makes it a little easier to apply what I read to how we’ll be using it in games, instead of just seeing it written out. I do remember learning about the first part of our class: SOHCAHTOA!

It’s something that sticks with you even 15 years after your last high school maths class, which makes it easier to remember the trigonometry calculations we’ll need for our exam (and for programming in general).

• SOH   :   Sine(angle) = Opposite / Hypotenuse   :   Y angle/movement in a game
• CAH   :   Cosine(angle) = Adjacent / Hypotenuse   :   X angle/movement in a game
• TOA   :   Tangent(angle) = Opposite / Adjacent   :   Hypotenuse is how far you want to move in a game; it’s not as useful for movement, but it will calculate the distance of the movement.

In a 2D game, this translates into…

• Cosine(angle) = X / distance   :   X = Cosine(angle) * distance
• Sine(angle) = Y / distance   :   Y = Sine(angle) * distance

We were shown a modified spaceship scene, where Ant included two different types of scripts: one with the trigonometry and one using Unity’s built-in system to do the calculations for you. I thought this was interesting to look at, since it shows that you can achieve the same results using less code and just understanding how Unity does the trigonometry in the background. The example given for Trig Movement, where the code is much more involved.Ant’s annotations make it much easier to dissect the code, too. This is the code for the NotTrig script, where it’s doing the same thing as the Trig Movement, but with far less code because it uses Unity’s built-in system.

Moving on, we discussed Radians and how they’re used more commonly in code. They’re just an alternative to degrees – as can be seen in the first line of clipped code for the Trig Movement script. As far as I understand, they’re just a different way to measure angles.

As quoted by Ant from Wikipedia:

“1 radian is the angle covered by wrapping a circle’s radius around its own circumference.” The GIF that keeps on GIFing is above, where it shows how this works. If Pi radians is 180º, then 2π radians is a full rotation. With that in mind, we were shown how to interchange between radians and degrees.

• Radians = degree / 180 * π
• Degrees = Radians / π * 180

Moving on from that, we reviewed class diagrams. The best analogy Ant gave us was to think of the diagrams as a recipe – you’ve got your recipe for a cake, but you can each the recipe and expect to taste delicious cake. They’re blueprints or instructions, which have the potential for an object (the instance of it).

C# files made in Unity are class files; they’re the blueprints for the custom components added to your game objects. The above is an example of the class diagram we were shown in class. The “Transform” title is the class. The second square contains the attributes, which are the variables associated with the class, and should include the data type (Vector3 in this case) afterwards. The third square contains the operations, which are the functions, and also include the parameters and data types (which we’ve really only covered Void so far in class).

While it’s not essential, it’s good practice to write the attributes and operations out the way you would in your code; the variables start with a lower case, while the functions are camel case.

Another example we were given, which shows a class diagram of the ship in the script we were given: I think these examples really help solidify my understand of class diagrams, more than having read about them online. I look forward to learning about them more, but we’ve been given new research to look into and continue working on our solo game ideas.