1
\$\begingroup\$

I have an formula that generates a number from 20 to 180 (degree).

var a = 20 * Math.pow(str/dist, 1.5));
    a = Math.max(180, w);

Using this formulae, i can generates some arcs and draw them just fine and depending on dist (which is cursor distance from target), the arc is either very wide, or narrow (pow basicly makes it so the arc expands non-linear).

This approach works in one direction: Having a location, and a distant point, to create arc. I need a counter-equation that gives me the required DIST for a given angle.

i.e. assuming str is 800, what is the value of dist so a would be 114 (or 68 or anything).

114 = Math.pow(900/dist, 1.5)) // ????

How can i solve this equation for dist ?

thanks,

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Generally, the easiest and fastest way to find answers to similar questions is to feed it to wolframalpha.com e.g. a = (x/y)^1.5 solve for y gives you y = x/a^(2/3). Using similar software is also less prone to errors than doing it manually on paper. \$\endgroup\$
    – wondra
    Apr 30, 2017 at 14:03

1 Answer 1

2
\$\begingroup\$

I'm going to disregard your max line for a moment (did you mean min? Written with a max, any values in the 20-180 range you described will come out as 180, since max returns the greatest argument)

We can solve this step by step by reversing each operation. Starting with the multiplication by 20:

$$ \begin{align} a &= 20 \times{(\frac{str}{dist})}^{1.5} \\ \frac a {20} &= {(\frac{str}{dist})}^{1.5} \end{align} $$

The opposite operation of a power is a corresponding root. So to undo a square \$ x^2 \$ we'd take a square root \$ \sqrt {x^2} = x \$, often available as its own function, sqrt(). For less common roots, we can use pow as its own inverse, using the fact that the nth root of x, \$ \sqrt[n]{x} \$, is just x to the power of 1/n:

$$ \begin{align} \frac a {20} &= {(\frac{str}{dist})}^{1.5} \\ {(\frac a {20})}^{\frac 1 {1.5}} &= \frac {str} {dist} \end{align} $$

And taking the reciprocal to get \$dist\$ on top (which is the same as negating the power), then multiplying both sides by \$str\$...

$$ \begin{align} {(\frac a {20})}^{-\frac 1 {1.5}} &= \frac {dist} {str} \\ str \times {(\frac a {20})}^{-\frac 1 {1.5} } &= dist \end{align} $$

Or in code:

dist = str * pow(a / 20.0, -1/1.5)
\$\endgroup\$
3
  • \$\begingroup\$ Thanks. The max was a typo. I didnt know how to reverse the pow, that was basicly what i was asking. Much appreciated. \$\endgroup\$ Apr 30, 2017 at 6:48
  • \$\begingroup\$ Also just for the record, in your personal opinion, would this question be better served to the Math SO, even though it includes code in this specific q ? \$\endgroup\$ Apr 30, 2017 at 6:53
  • \$\begingroup\$ I think it's fine to ask about math we need for games here. We'll tend to approach things a little differently in game math than in pure math (eg. using Math/Mathf libraries instead of Matlab or R code, avoiding transcendental functions whenever we can). I might recommend the math or computer science exchanges if someone had an obscure algorithm question, outside the daily experience of most gamedevs, or if they wanted proofs & explanations of why a particular formula works. It's just about which set of experts can answer the question. If we can, then it's less hassle than migrating! \$\endgroup\$
    – DMGregory
    Apr 30, 2017 at 7:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .