This time the math was not as complicated, but still not the sort of thing the average person has to use on a daily basis.
For Infinite Zip, my current game project, I needed to figure out if the player is on the zip line or not. Currently, the game randomly generates a bunch of points and ties them together with lines.
I am working on putting “holes” in the line to give the player something they have to jump over but for now we are only concerned with keeping the player on the basic line.
What We Know
So we have a few bits of information that we know from the start.
We know where the player is.
From that we can figure out which two points on the randomly generated line that the player is between. This gives us a start and end points.
We also know the players velocity.
Using these few pieces of information, we can calculate if they are or are not currently above the zip line.
How to Calculate What We Need
What we are dealing with here is two right triangles with the same ratios but different lengths. This makes everything pretty easy and turns it into a simple algebra problem.
What we need to know is what point of the line we are comparing to the player’s height. To calculate this we simply multiply the distance from the player to one of the end points by the difference in height of the end points, then divide by the width between the two end points.
It looks something like:
Y1 / X1 = Y2 / X2
Where we know Y1, X1, and X2. So we find Y2 by changing the equation to be:
(X2 * Y1) / X1 = Y2
and that gives us the height of our line at our player’s current X position. So if the player’s Y is greater than the calculated Y, they are below the zip line and should probably be falling.
Wrap Up
Games, especially any type of games with action or real time, often involve physics and that means math.
Sometimes the math is pretty straight forward.
Until next week, keep getting better.