The Math of Artificial Intelligence in Simulations — Part 2

Christopher Pearl
4 min readFeb 4, 2025

--

Variables needed in this class
Autopilot Method

First things first! I added the variables I’ll be using in this script. The autopilot() method will be calling the CalculateAngle() method which I’ll describe below, followed by the calculation of the Z axis (forward tank position) multiplied by the tank speed and Time.deltaTime.

Distance

Distance can be used for calculating angles and normalizing vectors. You’ll need to calculate 2 points: a point on the x-axis and a point on the y-axis. Once you have any two points, it can be used to form a right triangle.
To calculate the distance, I used something we all thought we’d never see after middle school: The Pythagorean theorem. c=a2+b2

For this project, a2 is the difference between the two points on the X-axis and b2 is the difference between the two points on the Y-axis.
I need to get the distance of the tank and the fuel for both the X-axis (x1-x2)squared and the Y-axis (y1-y2)squared, add the result, and then grab the square root of the result.

As in the previous project with the pigs and the farmer, I calculated the vector for the tank distance to the fuel: subtract the fuel’s position minus the tanks position. This will involve only getting the X and Z positions, as this in a 2 dimensional space.

The Debug Logs are simply to see a visual of the calculations in real time as your tank is moving around in Unity. It will give you a better understanding of how the values are being calculated in Unity. I used distance in my Pythagorean Theorem and magnitude is used for the Dot Product when getting the angle calculations. It’s good to double check your mathematics (this is why I used uDistance = Unity Distance calculation and the results from my equation as well).

The Dot Product

A visual aid to see the tankForward direction and the direction from the tank to the fuel

I’m using some trigonometry here, which was a new world to dive into, but fascinating to learn! I needed to get the angle between the vectors
Sum of the multiplication of corresponding components or coordinate values in 2 vectors

  • multiply both x values together, both y values together, and then add them together for the result
  • Positive values → Angle less than 90 degrees
  • Negative values → Angle greater than 90 degrees
  • 0/flat value → Exactly 90 degrees

2 vectors were needed to do this calculation. The first is the direction the tank is facing which is stored in the up (Z axis, up vector) of the tanks position. The second vector needed for the Dot Product is the vector measured from the tank to the fuel. To grab this vector, I subtracted the fuel position from the tank position.

Calculating the Cross Product

autopilot working but the tank isn’t going to the fuel

This is where cross product comes into play! The whole point of Cross Product is to take 2 vectors and get a 3rd vector from the result. This will make the tank turn toward the the fuel as it go towards the tank, no matter what direction it is facing. Cross Product multiplies each component of the vectors with every other component. This operation is not reversable, meaning the operation performed the opposite way around will not result in the same answer.

Snap turn of the tank and automatically goes toward the fuel, after pressing the T key

I had so much fun creating this project and found the magic in automation and artificial intelligence!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Christopher Pearl
Christopher Pearl

Written by Christopher Pearl

Unity/Unreal Developer, Artificial Intelligence Specialist, AR/VR Developer

No responses yet

Write a response