Algorithm to find the rotational degrees of a point according to some function

70 Views Asked by At

Hi I'm developing a game. In this game you can fire a missile, the path of the missile is given accord some function. For example if the function is f(x)=x^2 the missile should look like this.

enter image description here enter image description here enter image description here

Is there some way to get the rotation degrees given a (x,y) point and the distance from the x axis? I thought that this was a very common algorithm, but I didnt find any info about it. Any tip will be appreciated

1

There are 1 best solutions below

1
On BEST ANSWER

In short, let $\varepsilon$ be some very, very small positive number (the smaller the better, but you need to be careful to avoid numerical errors).

In case your $f$ depends only on $x$ (e.g. $f(x) = x^2$ would be your parabola), then points $(x,f(x))$ and $(x+\varepsilon, f(x+\varepsilon))$ represent a segment which direction approximately agrees with what you want to find.

The smaller the $\varepsilon$, the better the approximation. In fact with the limit $\varepsilon \to 0$ you get the actual value, and this similar to how the derivative of $f$ is defined for functions $\mathbb{R} \to \mathbb{R}$:

$$f'(x) = \lim_{\varepsilon \to 0} \frac{f(x)-f(x+\varepsilon)}{\varepsilon}.$$

Assuming that your $f$ depends on time (e.g. $f(t) = (t,t^2)$ would generate your parabola), then points $f(t)$ and $f(t+\varepsilon)$ are the endings of aforementioned segment. Observe that here $f$ is $\mathbb{R} \to \mathbb{R}^2$, i.e. its values are points. If you were to perform an operation analogous to the above one on each coordinate, you would get something called a Jacobian of a function.

To calculate the angle use the inverse function to tangent, namely arctangent, or even better its sugared version atan2.

I hope this helps $\ddot\smile$