Game design equation (Two variables(multiple two variables) and one Result (for every case)) how to make the Equation?

51 Views Asked by At

I tried to solve this using multiple methods but i couldn't figure it out. i don't know what to use in order to solve this basically i have an input x and x2

Heading

when x = 1 and x2 = 1 Y = 45

when x = 1 and x2 = -1 Y = 315

when x = -1 and x2 = 1 Y = 135

when x = -1 and x2 = -1 Y = 225

when x = 0 and x2 = 1 Y = 90

when x = 0 and x2 = -1 Y = 270

when x = 1 and x2 = 0 Y = 360

when x = -1 and x2 = 0 Y = 180

To explain it more i want an equation that when ever i input x ,x2 values in it i get the same y value. Ii don't know if its feasible or if math can deal with that type of situation but i believe that somehow there should be a way to make that equation I tried to use Excel to solve this as a regression but it didn't work

2

There are 2 best solutions below

1
On BEST ANSWER

First of all, if you are asking how to find an angle given 2 coordinates, in vast majority of languages, there is a function atan2(y,x) (or with similar name) that allows you to pass coordinates and get the value of the angle: $$ Y=\frac{180}{\pi}\mathop{\mathrm{atan2}}(x_2,x_1). $$ If we insist we want integer number from $1$ to $360$, we can add modulus shift: $$ Y = \mathrm{rem}\left(\left[\frac{180}{\pi}\mathop{\mathrm{atan2}}(x_2,x_1)-1\right], 360\right)+1. $$ In C it would be translated as ((int)round(180/PI*atan2(x2,x1)-1) % 360)+1

If we do not want trigonometric function for suchsimple problem and also do not want to use precalculated array, we can go like this. You can represent your data in a table $A_{ij}$, where $i,j=-1,0,1$: $$ A=45 \left( \begin{array}{ccc} 5 & 4 & 3 \\ 6 & \cdot & 2 \\ 7 & 8 & 1 \\ \end{array} \right) $$

All numbers except 8 can be represented as a bilinear interpolation of the corners: $$ Y'=4-2x_2-x_1x_2,\qquad A'=\left( \begin{array}{ccc} 5 & 4 & 3 \\ 6 & \cdot & 2 \\ 7 & 4 & 1 \\ \end{array} \right) $$

Now we need to add 4 to the bottom 4 to get 8. We can do it with the trick $2 (x_1+1) \left(1-x_2^2\right)$. Finally: $$ Y=45\left(4-2x_2-x_1x_2+2 (x_1+1) \left(1-x_2^2\right)\right) $$

0
On

There is no easy formula that gets all results in one equation, but the code can be made significantly easier than 8 cases as presented in the question. Pseudocode:

if (x==0) 
  {m=8, d=2}
else
  {m=6+2*x, d=x}
Y=45*((m+d*x2) % 8)

The $\%$-operator is the calculating the remainder of the term $m+d*x2$ modulo 8.

This will give $0$ instead if $360$ for $x=1, x2=0$, but I assume that this is OK as the design seems to be based on angles (I assume for movement).