Matrix Equations – 15/01/21

For todays Maths and Programming lesson, we took a look at Matrix Equations. Addition, Subtraction and Transpose in particular. Addition and Subtraction did pop up on the mini mock and was one of the questions I got correct without even knowing what a Matrix is or how the equations work, as it turns out, these are simple equations as opposed to last weeks task.

For Addition of Matrices we simply take each number in the position its in from both brackets and add A to B to get a result of C.

For Subtraction of Matrices we simply take each number in the position its in from both brackets and subtract A from B to get a result of C.

For Transpose of Matrices we make the rows into columns, so row 1 becomes column 1, row 2 becomes column 2 and so forth.

https://www.mathsisfun.com/algebra/matrix-introduction.html

It is worth noting that for these Matrix Equations, A and B must be the same size, you can’t add a 3×3 matrix to a 4×4 Matrix because Matrix B has more values than Matrix A.

We then started on Matrix Multiplication again for further practice. This time, multiplying a 3×3 into a 2×2. I found this one easier although I don’t exactly know why.

Equation 2 featured a 3×3 into a 3×3.

Finally Equation 3 and 4 were Scalar Matrix equations, these are easy as you multiply the number in the matrix by the scalar on the outside.

Where are Matrices used in Video Games?

Matrices are used in video games for alot of movement and animation, if we’re in a game engine and we are modifying a transform of an object, the engine uses a matrix to calculate the transform. The same can be said from a player point of view, as they move the character around in the game, the game is calculating the matrix in the background in order to move that player. Another example would be for Field of View. Just adjusting a slider in a game isn’t doing anything without the proper mathematics behind it, when you adjust that slider, the computer is computing matrices programmed into the game in order to make sure the camera only changes FOV and nothing else.

Matrices – 08/01/21

Matrices are complicated pieces of maths that contain sets of numbers. Typically they can be added together, subtracted, multiplied by a Scalar value or multiplied into another matrix. Below are some examples of Matrices and their solutions.

This was fairly simple to do, for the 1.5x Matrix I had multiple ways to work this out. I could times 1.5 by the unit and then multiply by 10, I could just do the straight multiplication or once I had worked out the first three Matrices, I noticed a pattern of increments in 15’s.

For the 3x Matrix, I used my knowledge of the 3x Table to complete, again, this was fairly simple. 3 x 7 is 21, 3 x 14 is double the answer of 3 x 7 so 42 and so forth.

This one is a bit more complicated than Scalar Matrices. For these ones you get Row A values and multiply them by Column A values and add them all together, Row B multiplied by Column B and added together and Row C multiplied by Column C and added all together. This is stupidly complicated.

Another way I could work this out what by splitting Bracket B. I could write out Bracket A 3 times, then put the values of Bracket B into the corresponding places.


11 x 9 + 08 x 4 + 02 x 2 = p1
54 x 9 + 12 x 4 + 16 x 2 = p2
08 x 9 + 07 x 4 + 05 x 2 = p3

11 x 14 + 08 x 7 + 02 x 4 = p4
54 x 14 + 12 x 7 + 16 x 4 = p5
08 x 14 + 07 x 7 + 05 x 4 = p6

11 x 2 + 08 x 22 + 02 x 15 = p7
54 x 2 + 12 x 22 + 16 x 15 = p8
08 x 2 + 07 x 22 + 05 x 15 = p9

p1 = 135 | p4 = 218 | p7 = 228
p2 = 566 | p5 = 904 | p8 = 612
p3 = 110 | p6 = 181 | p9 = 245

Where have Matrices been used in Video Games?

Matrices are used in video games pretty much any time any kind of movement is involved. Although most the time when developing games, we don’t need to do the maths ourselves, the game engine will compute it in the background. When it comes to animation or moving objects, Matrices will be used here to create realistic results. Although, when I think of realistic video games, in terms of graphics and animations I think of games like Uncharted and the Last of Us which used MoCap suits, the computer once again will calculate the matrices for these particular games.

Mode, Mean, Median and Range – 11/12/20

In todays session, we covered averages and the principles that are Mode, Mean, Median and Range. It has been a while since I’ve done these so it was nice to have a refresher on this.

Firstly we looked at probability and possible outcomes, in an example given, it involved 3 coins and the chances of getting heads only once if all 3 coins were flipped at the same time., To get this we get the number of desired outcomes and divide it by the number of possible outcomes.

Mode = The most common number to appear in a set of numbers, there can be more that one.

Median = Put the numbers in order and then find the middle value.

Mean = Add up all the numbers in the set and then divide the result by the amount of numbers in the set.

Range = Subtract the lowest value from the highest value.


We then wrote some code that would find the average number from a list of randomly generated numbers.

Firstly, we declared a static float array named set that had a value of a new float[93]. What this means is that it would generate a list consisting of 93 places.

Then inside the main function, we declared another float named average with a value of 0.0f and then using C# generated a random number by accessing the system library and using a function called Random. We named this randNumGen.

This is the tricky part, we made what is called a for loop. A for loop is a conditional piece of code, so will only run until those parameters are met. So in this for loop, it declares a int variable named i. i is then assigned a value of 0. We then say if i is less then set.Length, add another value of 1 to i. set.Length is how big our float array is.

inside this for loop, we then say set[i] is equal to a random number generated between 0 and 100.

On the next line we make average equal to set[i] and then to finish the for loop, we use the WriteLine function to tell us what numbers were generated. We use set[i].ToString() to convert our int variable to a string variable.

Finally to get the mean, we get the value of average and divide it by set.Length.

Cross Products – 27/11/20

Whatever these are, whoever invented these, I will find you and I will kill you… is what Liam Neeson said when he thought ‘I want to be a programmer’.

Cross Products are nasty mathematical equations that performs a series of multiplications between two vectors to give a new perpendicular (at 90 degrees) to the original values.

The result of a cross product is a new vector. There are so many big words in this post already I don’t really want to continue.. but this is stupidly simple. The key is a table and part of the title. As an example, we had to find the cross product between (14, 11, 09) and our date of birth.

So essentially write out both vectors twice with the first one on top and second one across the bottom. We then plant X, Y and Z directly in the middle of the table, multiplying the adjacent numbers. Well, when I put it like that, it seems stupidly simple, I could have invented it myself! Just to verify my working out, I used a cross product calculator.

So now, we can look at finding the magnitude of this vector. Well, we can use the sum of Magnitude² = X² + Y² + Z², so in our case

951² + -1239² + 35² = 904401 + -1535121 + 1225

904401 + -1535121 + 1225 = -629495

SqRt of -629495 = 793.407209

Magnitude = 793.4 to nearest decimal place.


Finally, the dot product between the cross product and original value.
Dot Product = (X x X) + (Y x Y) + (Z x Z).

(14, 11, 09) · (951, -1239, 35)

X = 14 x 951 = 13,314
Y = 11 x -1239 = -13,629
Z = 09 x 35 = 315

13,314 + -13,629 + 315 = 0

Dot Products and Vectors – 20/11/20

In todays session, we learned about dot products and vectors. A dot product is essentially the value of two corresponding coordinated points multiplied and then added together. This would help us find the angle between two vector points. For 3D, we would simply add the Z co-ordinate to the mix.

As an example lets take (1, 2) • (3, 4) this would equate to:

1 x 3 = 3
2 x 4 = 8

3 + 8 = 11

If we wanted 3D, we could use (1, 2, 5) • (3, 4, 6)

1 x 3 = 3
2 x 4 = 8
5 x 6 = 30

3 + 8 + 30 = 41

We were then given the task of finding the Dot Product of three different math problems.

-3 x 2 = -6
 6 x 3 = 18
------------------
-6 + 18 = 12

-1.5 x -4 = 6
 4 x -0.8 = -3.2
------------------
 6 + -3.2 = 2.8

 2 x -0.5 = -1
 4 x 0.5 = 2
 6 x -0.5 = -3
------------------
 -1 + 2 + -3 = -2

A dot product may be used in video games for programmers to use in vectors. A vector is a force made up of magnitude (length) and a direction. If we take Super Smash Bros. as an example, they will need to program a way for the character to be flung into a certain space.

So if the opponent is on the right hand side of stage and Mario smashes the opponent in the face, the character, dependent on damage taken, would then blast off into the direction. This would be calculated using a Dot Product.

Number Systems – 13/11/20

A number system in logical terms is a way of representing numbers in an alternative format, for example Binary would be made up of 1’s and 0’s and Hexadecimal would be made up of a combination of letters and numbers from 0-9 and A-F.

There are numerous types of number systems but the most common ones are Decimal, Binary and Hexadecimal. They all run off what is called a Base, which essentially is a range of numbers and characters.

Decimal is the number system that is used pretty much in our everyday lives and although it may seem complicated when laid out like this, it essentially is just simple maths.

Decimal has a Base 10 which means its values range from 0 to 9. It is represented in the traditional number format of units, tens, hundreds, thousands and so forth. My name in Decimal would look a little something like this.

Unmistakable Letter Of The Alphabet Number Chart Code In Numbers Of The  Letters Of The Alphabet With Numbers 1… in 2020 | Letters to numbers,  Alphabet code, Coding for kids

J = 10 | O = 15 | R = 18 | D = 4 | A = 1 | N = 14

10 + 15 + 18 + 4 + 1 + 14 = 62.

Binary is a number system with a Base 2, this means it only consists of 2 numbers, 0 and 1. Essentially to write a number as Binary, you would keep dividing that number by 2 and carrying over remainders. As an example, lets take the number 27, which is my age. In Binary terms it would look something like this..

27 / 2 = 13.5 || 1
13 / 2 = 6.5 || 1
6 / 2 = 3 || 0
3 / 2 = 1.5 || 1
1 / 2 = 0.5 || 1

11011.

Where there is a decimal point in the number, that would mean something is left over so we make that a 1 and then divide the number before the decimal point to get the new Binary value.

Finally there is Hexadecimal, Hexadecimal is a Base 16. I’ve commonly used Hexadecimal when using a color picker in photoshop. The values in Hexadecimal Base 16 range from 0-9 and A-F.

Within the games industry, Binary could be used to assign values in code. Hexadecimal could be used as mentioned above, to pick a colour for a material or in a more practical sense, they may use hexadecimal to calculate how much storage or RAM the game may need to run correctly, truth of the matter is, most of this stuff is automated now, so hand drawn calculations may not be as common but still used in day to day life.

It is also possible that developers may used Binary, Hexadecimal or even number systems with a larger base for encryption purposes or serial keys that may be used to activate licenses.

Integrated Development Environments – 10/11/20

An Integrated Development Environment (IDE) is essentially where all the coding development parts of our games are normally completed. They exist to correctly format pieces of code and maybe to even help us correct the code should there be an error, it can even act as a basic text editor.

It would be no use if Unity threw up an error about some code that we may have made, for us to then not be able to find out what the error was or to help us correct it, especially in programming, a lot of mistakes can be made and an IDE can help us avoid or fix them.

IDE Examples

Visual Studio // Android Studio // Brackets.IO

An example of an IDE would be Microsoft Visual Studio. Visual Studio is normally the go to IDE for Unity developers because it is feature rich and integrates nicely with Unity.

When a new script is made in Unity, it will add the correct API’s for us to code correctly within Unity. This is noted at the top of our script when making a new script and it looks like this..

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;

Without those parts, Visual Studio wouldn’t recognise most of our code as it uses multiple components from Unity API. As mentioned above, it helps us correctly format our code so when making a new function or an if statement, it will indent the code and automatically place curly brackets, along with this it also utilizes an autocomplete function which can also be quite annoying.

Another cool feature of Visual Studio is its ability to colour certain parts of code so its easy to understand what code is referring to what, this is known as Syntax Highlighting.

Other examples of IDE’s would be Android Studio. Android Studio is needed to build android apps and dispatch on to Android Devices in a legitimate way. What I mean by this is you don’t just build the APK and download it to your device, the Android Studio IDE will sign, license and can even publish your apps to the Google Play Store.

It also has many of the same features as Visual Studio in terms of the formatting and syntax highlighting, However one unique feature is that it has an Android Emulator so you can test your apps without having to deploy the app to a device.

Finally there is also Brackets.io. Brackets.io is an online IDE that includes many of the features Visual Studio has, however it doesn’t use up as much space as Visual Studio as it doesn’t need to be downloaded and all the code is compiled in browser. It also has plug in support that can help make coding that tiny bit simpler. The one issue with this particular IDE however is when you are combining it with a Game Engine like Unity, it doesn’t recognise Unity’s API’s so you could be stabbing in the dark.

Difference Between IDE and Game Engine

The difference between an IDE and a Game Engine is astronomical. Two completely seperate entities. The IDE is there for the programming components of the Game Engine.

The Game Engine is essentially the glue that pieces everything together. If you need to construct a code for a specific Game Object in engine, you would then turn to your IDE of choice and code that class for that object. You would then attach that script to that game object within the game engine.

Game Engine = Glue that pieces everything together.

IDE == Coding environment to build our object orientated code, for use within a game engine.

Activity Diagrams – 06/11/20

In todays programming session, we looked at application diagrams and pseudo code. To start, we made a simple diagram explaining how a certain event in a game might take place. Mine was fairly simple as I chose Pokemon and evolution.

The next diagram we had to make was a one that included decisions, I thought of a common decision making problem and have managed to come up with a way to solve the problem using an app that we could build. The problem, as always, is what Takeaway do you order when you have mates around to watch the football? Well, fear not, the solution is here… providing everyone in the party likes the mentioned takeaways..

We then had to take our decision making diagram and write actual code for the decisions to be made. Ladies and Gentleman, introducing, the TakeAway decider!

Coding Recap – 16/10/20

What is a Variable and how have I used one?

A variable is part of a code that stores information, this could be a players name, a number, a bool (true or false). As in the screenshot above, I have a few string variables. Other variables would be an integer which is a whole number, a float which is a number to a decimal point, a string of text.

What is a Class and how have I used one?

A Class in code-terms is something that hold information that can be reused easily. Essentially encapsulating data and behaviour. This is also useful when it comes to organising code so all of your code isn’t just in one giant script. In this example, I created a separate class for navigation. This can then be called in another script by using “Navigation.NorthFromStart()” as an example… although that would result in death for this game.

What is a Function and how have I used one?

A function is something within a class that holds a block of code and the function can be called at various points. In this example I have a Class called GameOverExit which is called in the main program numerous times, when someone fails to choose the right option, the main program will call this Function.

This will turn the console colour red, print some text and then wait for a button press to close the console down.

What is an If Statement and how have I used one?

An If Statement is something in code that can output different options dependent on certain conditions and//or requirements. In my example, if the requirement of player name being God is met, they will be granted a cheat code you would only know if you typed in God for your name. Otherwise, it wouldn’t tell you the cheat code.

The hardest part of making my game was starting it off, I couldn’t quite get my head around how to take the user’s input and then make the computer read it and store it, but looking at it now, its deceptively simple. The part I am most proud of is all the different functions I have used and how they have been able to affect the outcome of certain scenarios.

I would love to learn how to implement a bool system and make the game check to see if that bool is set to true, just to enable or reinforce certain aspects. Take a weapon for example, if the bool is set to true for that weapon, then the user can use it, if not, it calls the function again for a different option.

Just for referencing purpose, my game can be downloaded at this blog post. LINK.

Vectors in Video Games – 16/10/20

Firstly, what is a vector? A vector is something that has magnitude and a direction. Push, Pull, Acceleration and Gravity can all be used as vectors.

So with that in mind, Vectors are used everywhere in video games, below are 5 examples from games that I like playing where vectors may have been used. I will also use Pseudo-Code to explain how this may happen.

Example 1: Mario Kart 8 Deluxe

In Mario Kart 8, each driver // kart uses a Vector, in this case it would be a Vector3. The Vector3 would need to be able to accelerate and have a speed increase.

Example 2: Crash Bandicoot 2

In Crash Bandicoot 2, when we’re jumping on polar’s head for our 20 lives and not because we disrespect animals, vectors are used here. The vector I want to point out is Crash’s jump in particular. Crash would need to have a Vector3 telling him where to stop jumping and and another vector3 pulling him backdown, although if a gravity code is used, we could also use that to keep him grounded.

Example 3: FIFA21

I don’t necessarily like playing FIFA, but its way more enjoyable with a few mates in the pro clubs game mode. Looking at this screenshot, the ball would be a Vector3. Because the ball can move in many different directions, and with spin, it would need to be able to rotate and have a force pushing it in multiple directions, dependent on where the impact has came from.

Example 4: Sonic the Hedgehog 3

The blue boy is making another appearance on my blog, this Vector2 would need pure acceleration that increases dependent on distance travelled. He would also need a point to define gravity and also to define a jump height.

Example 5: Rocket League

In Rocket League you can drive on the roof, and this would be done with the same method as used in the Super Mario Example. A certain point of gravity would be used, there would need to be some acceleration for the car to move as well.

Obviously, within these 5 games, Vector’s are used more than we may know, in the 3D models, the assets of the levels and even for camera movement. It’s not hard to understand why using and understanding how these are used are fundamental to video game programming.