Generate a list of points to create an arc path

So I do a lot of animations in JS, as far as moving things around, rotating, resizing, all sorts of stuff.
When I move a sprite, I generate a set of points from it's starting location to it's target location, and without going into unnecessary detail, let's say I have smooth 'regular' movement down. That is, moving in straight or diagonal lines.

What I can't figure out is how to mathematically generate a set of x, y coords that will allow my shapes to move in arcs, or curved patterns.

I would like to be able to create two separate arrays (since my animation methods already use arrays) that contain the x and y points for an image to move, with the ability to specify the beginning/ending points of the movement.
Each frame, I pop the next x and y coords from these arrays and set them as the target sprite's x and y properties, creating smooth movement.
But on the way there, I'd like the shape to move in a sort of curve or arc, rather than just a straight or diagonal line. Does this make sense?
I'd also like the 'length of time', or really, the length of the list to be variable.

I've researched it a lot, but there are so many different contexts under which one would try to mathematically define a curve, and I haven't found any that allow me to create a set of x, y points that could be used as a path for a sprite to move along.
This is about function approximation~
  1. Choose a function.
    E.g. y = x * x​

  2. Choose an interval for each independent variable.
    E.g. -2 <= x <= 2​

  3. Choose a sample count (or polling interval) for each independent variable. The more points, the more closely the result will resemble a perfect curve.
    E.g. 5 x-values in the given interval​
Putting it together to generate your x-y coordinates:
  1. Generate list of (uniformly-distributed) input values based on interval and sample count - just a "for" loop.
    E.g. -2, -1, 0, 1, 2​

  2. Then evaluate your function at each of these input values - another loop.
    E.g. 4, 1, 0, 1, 4​
😤

Multivariate functions, e.g. z = f(x, y), simply require you to have nested loops, e.g. evaluate at each combination of x-y values to yield a "grid" of z-values (if that makes sense). 😮‍💨
Personally, when I handle movement for my sprites I use math functions in the format f(t) = math function to determine the x and y. For this method to work, you need to define a start point, an end point, and timeframe, and the type of function.

Example: I want a sprite currently located at (100, 50) to move to (500, 150) in 10 frames. So...
Code:
x = ((500 - 100) / 10) * t + 100
x = 40 * t + 100

y = ((150 - 50) / 10) * t + 50
y = 10 * t + 50
Afterwards, I store that math function in the sprite and the sprite's position is updated every frame by calling that function and pasting the t (the current relative frame, 0 at the beginning of the movement and +1 per frame) into the function.

To get movement pattern, you just need to modify the function. In the previous example, I'm using a linear function, so it's going to move in a straight line at an even speed. Here are a couple functions:
Parabolic function: t = m * (x ^ 2) + b
Square function: t = m * Math.sqrt(x) + b
Cos/Sin function: t = m * Math.sin(x) + b
Just play around with math functions until you get something you like.

Here's an example of what it looks like inside a sprite:
Code:
Sprite_Test.prototype.setMovement = function(end_x, end_y, time, type, param1, param2) {
    // Param1 and param2 are used to modify function parameters in math functions
    this._anime.start_x = this.x;
    this._anime.start_y = this.y;
    this._anime.parm_x  = (end_x - start_x) / time;
    this._anime.parm_y  = (end_y - start_y) / time;
    this._anime.maxTime = time;
    this._anime.currentTime    = 0;
    this._anime.functionType   = type;
    this._anime.functionParam1 = param1;
    this._anime.functionParam2 = param2;
    this._anime.active = true;
};

Sprite_Test.prototype.update = function() {
    Sprite_Base.prototype.update.call(this);
    if (this._anime.active) {
        if (this._anime.currentTime < this._anime.maxTime) {
            this._anime.currentTime += 1;
        } else {
            this._anime.active = false;
        }
        if (this._anime.functionType == "linear") {
            this.updateAnimationLinear();
        }
    }
}

Sprite_Test.prototype.updateAnimationLinear = function() {
    var dt = this._anime;
    this.x = Math.round(dt.start_x + dt.parm_x * dt.currentTime);
    this.y = Math.round(dt.start_y + dt.parm_y * dt.currentTime);
}
sin() and cos() ?

I mean, they've been out there since..... forever.


you could also use the formula for curved shot, making gravity a variable instead of a constant.
sin() and cos() ?

I mean, they've been out there since..... forever.
Rude, and not helpful. I don't have a very complex understanding of math, I have what I was able to graduate high school with, and that was over a decade ago. If I haven't had a chance to use it, it's gone, so just give your usual attitude a rest, maybe for just one day?

@caethyril @MushroomCake28 I'm not gonna lie, I have no idea what I'm looking at. I had to work through highschool so I only took the minimum maths I needed to graduate. However, I appreciate your answers very much, and will mess around with this code, do some research and try to understand it.

It's definitely given me a direction to look in, which is most of what I needed.
@standardplayer If you're looking for the math function to get perfect circle arc, then it's the sin and cos function (to be used with radiant, not degree, but it's easy to convert between both).

You need to know that:
180 degree = PI radiants
So to convert degrees to radiants in javascript, here's the formula:
var radiant = degree / 180 * Math.PI

Okay, next thing you need to know: cos gets the x, and sin gets the y. So to get the x/y position, here's the formula:
Code:
var x = Math.cos(angle); // angle is always in radiant, hence why we want to convert degrees to radiant
var y = Math.sin(angle);

Next thing you need to know: A circle is 360 degrees, or 2 PI radiant. So if you want an arc that is half a circle, you'd need 180 degrees, or PI radiant.

Okay, now you have everything you need to know to code your arc movement (using the code I gave earlier). The idea is to set the x and y with the formulas above (x = Math.cos(angle)) on every frame, and you increase the angle by +x every frame, which will result in a movement that follows the course of a circle. The complete x (same for y, but with sin instead of cos) position would be:
var x = radius * Math.(angle) + circleCenter_x

So if you want a movement from the left side of a circle (position PI radiant) to the right side (position 0 radiant), and you want that movement to take 60 frames, you need to call that function every frame and add PI / 60 to the angle every frame.

I know it's some more complicated than usual math, but circle formulas and functions are more complicated than a simple linear function unfortunately. There is no way around it.

EDIT: The radius would define the length of your arc (remember, radius is the distance between the center of the circle and any point of the circle).
@MushroomCake28 oh God.... yeah, I don't understand anything you wrote lol, I feel so old when it comes to Math, I really have had no reason to use almost any of what I learned since I learned it.

I appreciate you trying to help, I thought maybe on the previous responses it was something I could figure out, but after reading your response just now, I think it's something where I'm just missing education. That's the hardest one to fix, because it's like googling 'Fix the holes in my high school education' lol.

I appreciate you taking the time to try explaining it, though
@standardplayer Do you have VXA and/or able to read ruby? Because if it just the logic, I think I can try to explain. But to try to understand it, you have to actually practice with the actual code, and unfortunately I can only try to explain it in RGSS3 code.

And my version does not use Sin/Cos, Degree/Radiant, or Phi. If that is too complicated for you. I will give you a barebone code that works, but you need to have VXA.
I do have VXA but I've never written any Ruby. I do want to learn it, I know might sound all doom&gloom, but I do plan on butting my head against this until I understand it.


That said, I'd gladly see what you're willing to teach, might finally be a reason for me to learn Ruby!
Alright so, try to add this script anywhere in VXA as long as it is above main
Code:
# Ignore this, it's the sprite creation code
$sprite = Sprite.new
$sprite.bitmap = Bitmap.new(32,32)
$sprite.bitmap.fill_rect($sprite.bitmap.rect, Color.new(255,255,255))

$y_base = 300 # Base Y coordinate

$sprite.x = 100
$sprite.y = $y_base

$jump_height = 0    # Final height value
$jump_force = 0     # Scalar value = velocity (addition factor to height value)
$jump_interval = 0  # Gravity (substraction factor)
$duration = 0

# Update loop goes here
loop do
  Graphics.update
  Input.update
  # This is where the jumping formula goes
  # Y base + jump height = final Y position
  $sprite.y = $y_base - $jump_height

  # This is where you trigger the jumping sequence
  if Input.trigger?(:C)
    $jump_height = 0
    $duration = 60
    $jump_force = 10
    $jump_interval = $jump_force/($duration.to_f/2)
  end

  # This is where the move update goes
  if $duration > 0
    $duration -= 1
    $jump_height += $jump_force
    $jump_force -= $jump_interval
  end
end
This is stupid, but works, and I haven't yet find a single issue with this formula (and yes, this formula is used in my battle system). Everyone uses a calculation that uses time factor and sin/cos for more natural-looking, while me, I'm just stupidly update it per frame based on velocity.

And what you will see is something like this when you press Z.
jump.gif

You can try to change the value of jump force and duration and see how it goes. Ask me anything if you don't understand.

You can also easily merged it with linear movement as long as you modify the y_base variable, not messing up with the jump value. For example.
Code:
# Ignore this, it's the sprite creation code
$sprite = Sprite.new
$sprite.bitmap = Bitmap.new(32,32)
$sprite.bitmap.fill_rect($sprite.bitmap.rect, Color.new(255,255,255))

$y_base = 300 # Base Y coordinate
$x_base = 100

$sprite.x = $x_base
$sprite.y = $y_base

$jump_height = 0    # Final height value
$jump_force = 0     # Scalar value = velocity (addition factor to height value)
$jump_interval = 0  # Gravity (substraction factor)
$duration = 0

$speed = 0 # Velocity

# Update loop goes here
loop do
  Graphics.update
  Input.update
  # This is where the jumping formula goes
  # Y base + jump height = final Y position
  $sprite.y = $y_base - $jump_height
  $sprite.x = $x_base

  # This is where you trigger the jumping sequence
  if Input.trigger?(:C)
    $speed = 2
    $jump_height = 0
    $duration = 60
    $jump_force = 10
    $jump_interval = $jump_force/($duration.to_f/2)
  end

  # This is where the move update goes
  if $duration > 0
    $duration -= 1
    $jump_height += $jump_force
    $jump_force -= $jump_interval
    $y_base -= $speed
    $x_base += $speed
  end
end
It will be
jump2.gif
Oh I can't wait to get home and try messing with this! Thank you very much @TheoAllen !
@TheoAllen
I'm home now, and I've set this into ACE, I get most of how it works.
I can read the basics of what's happening in the Ruby code, it's all familiar OOP, but there is a method I really don't recognize

($duration.to_f/2)

What is .to_f doing?
Without that bit, I'm actually not really understanding what's happening here. Again, I can follow along and read the code, but the actual dynamic change I need to know about seems to be linked to
$jump_interval

and I can't figure out what change is occuring when that method runs.

If it's what I think it is, then I've actually achieved something like this before, by way of making a platformer. But there, the natural looking arc paths were a combo of two separate functions, the player pressing jump and the player moving left or right.
But I see you've got it down in code, and that's what I'm looking for.

Of course, I could do something like decrement a modifier as I'm plotting the points, but I know it can't be what programmers are doing do generate arcs.
What is .to_f doing?
the method to_f is to convert an integer to float, you know if you make a calculation of 1/2, instead of being 0.5, it's 0, so either has to be converted to float. Which mean if 1.0/2 it will result in 0.5.

To understand what is happening, you need a console window, similar to debug/inspect element in MV

Screenshot_187.jpg

Then you can try to print each variable by putting, for example
Code:
p $jump_height
in the update loop, or anything you want to see the value

Without that bit, I'm actually not really understanding what's happening here. Again, I can follow along and read the code, but the actual dynamic change I need to know about seems to be linked to
$jump_interval
Yes, it's all linked to this variable in particular.
For example, I set a hardcoded value that the jump duration will be 60, and the force is 10 (you can change these variable to any value you want, but let's use this as an example)
The $jump_interval value will be calculated in this formula
Code:
= Force / (Duration / 2)
Known:
Force = 10
Duration = 60
> 10 / (60/2)
> 10 / 30
> 0.33
The value 0.33 will be used to subtract $jump_force in each frame. i.e, it's gravity.
If in frame 1, we have a value 10 of the force, the next frame we will have 9.77, this subtraction will continue in next 60 frames.

In the update loop, we have
Code:
$jump_height += $jump_force    
$jump_force -= $jump_interval
So what is happening here?
Let's try to print on the console. Note that I used the method round to limit the floating number for display purpose because if not we gonna have 9.666666666 and that is hard to read
Code:
$jump_height += $jump_force    
$jump_force -= $jump_interval
p "Jump height: #{$jump_height.round(2)} -- Jump Force#{$jump_force.round(2)}"
The output becomes like this
Screenshot_189.jpg

Hope this clear things up.
One common way to do what you're describing is something called Spline Interpolation.

Basically, the concept is you generate an initial set of points (i.e. in your case, you can pick out a few specific [x,y] points you want your animation to go through at specific times - the start, the end and a few specific points in between), then through some process figure out a mathematical function that describes a curve which includes all of those points. Once you have a function, you can then use it to find out the [x,y] for the animation at any time.

For example:you could say "I want my animation to have this [x,y] at the start (or time = 0), this second [x,y] after 1 second, then this third [x,y] after 2 seconds, and then the animation ends. But I don't know what all the other [x,y]s are in between". If you can figure out a function that describes the movement between the points, you can then use it to give you back the [x,y] at all of the other possible times, in a way that forms a curve between all the [x,y] points you initially gave it. Then all you have to do is set your sprite's [x,y] every frame to whatever coordinates the function says it should have at that time, until the animation is done.

The process of figuring out what that function is, is what's called Spline Fitting or Spline Interpolation. Here's a video that shows a more visual example of what I'm talking about:



So there's good news and bad news about that. The good news is that this will do exactly what you want and can create very smooth arcs, and it's very commonly used in game development. The bad news is that to fully understand it requires some advanced math, and you'll have to study a bit to be able to write the code. You need to at least learn a bit about Calculus, to understand Derivatives, and Linear Algebra, to understand Matrices.

You can read more about the mathematical definition of it here:

And here in more detail:

And example implementations:

If you look at the code from the first link in Github, the final code itself that comes from all this isn't really all that complicated. But understanding why any of it actually works and what exactly it's doing mathematically will take some extra studying.

Original thread: https://forums.rpgmakerweb.com/threads/generate-a-list-of-points-to-create-an-arc-path.118893/