Determining an equation from a graph

33 Views Asked by At

Background: I am trying to make a script for a 2d game that calculates how fast the player moves based on the the direction they are trying moving. To do this, it uses variables that represent longitudinal and lateral speeds (dubbed "lon" and "lat" respectively) that determine a multiplier to the base speed.

For simplicity, let's assume:

  • lon = 5;
  • lat = 3;
  • x = player's relative rotation (0° being directly up);
  • y = speed multiplier;
  • 0° ≤ x ≤ 180°;

So given this, f(0° or 180°) = 5 and f(90°) = 3 It gets more complicated when we consider intermediate values, which will be calculated by a simple linear function between the lon and lat, so f(45° or 135°) = 4. However, each axis also has a 30° "deadzone" where the y is equal to the lon or lat. Here is a rough depiction. For example, a rotation value that falls anywhere within the lon deadzone would return a speed multiplier of lon.

Problem: So given the above situation and the sample lon and lat values, I created a graph that represents the function I am trying to achieve here. What I'm trying to do is find the equation of this graph (if it can be done in a single equation) so I can calculate the speed multiplier from a given rotation. Keep in mind that lon and lat are variables, so the equation will have to be accurate regardless of what they are; they will always be greater than 0 if that makes things easier. I haven't been able to find any answers online so if anybody could provide insight on this that would be greatly appreciated.