We need 3 input variables, so let's define and assign them:
b
- The base speed we want to "vectorize"x
- The displacement of our object from Link along the X-axisy
- The displacement of our object from Link along the Y-axisThe best way to think about this is multiple sequences. At least to me. Perhaps there's some obscure high-level branch of mathematics that can deal with this in more universally understandable terms, but we're going to think of it as a series of numbers. Multiple series of numbers.
j
- The larger magnitude of our two displacements; j=max(|x|,|y|)
k
- The smaller magnitude of our two displacements; k=min(|x|,|y|)
l
- All sequences we have will be of length l=b+1
, unless b=0
in which case they will be of length l=257
.a
will describe our collection plate. It has the following rules:
a_{0}=0
a_{n}=(a_{n-1}+k) mod256
when (a_{n-1}+k) mod256<j
a_{n}=(a_{n-1}+k-j) mod256
when (a_{n-1}+k) mod256≥j
s
will describe our main output. It has the following rules:
s_{0}=0
s_{n}=s_{n-1}
when (a_{n-1}+k) mod256<j
s_{n}=s_{n-1}+1
when (a_{n-1}+k) mod256≥j
Okay, so formalizing it didn't make it too much more understandable, but now we have what we need to work through an actual problem step by step.
b=5
x=20
y=-30
l=5
, as b
was nonzero; b≠0
j=30
, as y
has a larger magnitude; y>x
k=20
, as x
has a smaller magnitude; x<y
a=0, …
s=0, …
We did it! Let's keep going.
We'll do each step by finding the next element in a
. For the first term and beyond, we need to use the functions defined above. For our example, we have 0+20≡20 mod256
and 20<30
. So a_{1}=(0+20) mod256=20
. That inequality is used to contruct a rule for both sequences, so we can also find the next element in s
. s_{1}=s_{0}=0+
.
a=0, 20, …
s=0, 0, …
Let's do the next 4 steps, but do them faster:
[a_{1}+k mod256]=[20+20 mod256]=40
and 40≥30
, so we use the second function for the next term of both sequences.
a_{2}=[a_{1}+k-j mod256]=[20+20-30 mod256]=10
s_{2}=s_{1}+1=0+1=1
a=0, 20, 10, …
s=0, 0, 1, …
[a_{2}+k mod256]=[10+20 mod256]=30
and 30≥30
, so we use the second function for the next term of both sequences.
a_{3}=[a_{2}+k-j mod256]=[10+20-30 mod256]=0
s_{3}=s_{2}+1=1+1=2
a=0, 20, 10, 0, …
s=0, 0, 1, 2, …
[a_{3}+k mod256]=[0+20 mod256]=0
and 0<30
, so we use the first function for the next term of both sequences.
a_{4}=[a_{3}+k mod256]=[0+20 mod256]=20
s_{4}=s_{3}=2
a=0, 20, 10, 0, 20, …
s=0, 0, 1, 2, 2, …
[a_{4}+k mod256]=[20+20 mod256]=40
and 40≥30
, so we use the second function for the next term of both sequences.
a_{5}=[a_{4}+k-j mod256]=[20+20-30 mod256]=10
s_{5}=s_{4}+1=2+1=3
a=0, 20, 10, 0, 20, 10
s=0, 0, 1, 2, 2, 3
Phew. We're done. What we care the most about is the final term of sequence s
, which, in this case, is 3
.
v_{x}
is the velocity along the X-axisv_{y}
is the velocity along the Y-axisv_{x}
and v_{y}
are determined by the original displacements:
|x|≥|y|
:
v_{x}=b×(x/|x|)
v_{y}=a_{l}×(y/|y|)
|x|<|y|
:
v_{x}=a_{l}×(x/|x|)
v_{y}=b×(y/|y|)
v_{x}=5
v_{y}=−3
In other words, the axis that had the larger displacement (the Y-axis when both are equal) receives the base speed b
as the magnitude of its velocity, with the sign matching the displacement along the axis. The smaller displacement (the X-axis when both are equal) receives the final value calculated from the loop, again, matching the sign of the displacement.
Our example problem used a small input for b
, otherwise the algorithm would have been more difficult and tedious to follow. As such, the resulting output being small is not surprising. Perhaps this example is more enlightening as to why b=0
results in the behavior that it does. Working out of such an example is left as an exercise to the reader.