Cosine or Sine Movement in different angles

Guys i been working on the boss attack for my platformer game, anyone know how control the boss attack come out to be in cosine/sine graph sequence? let say boss fire 8 balls and they move following a cosine graph curve away from boss (each to different angles(0,45,90,135,180,225,270,315,360 Degrees)

It is not the easiest of the tasks, you have to do a classic sin(x), but then you have to rotate this point around the shooting origin in the desired angle.
For example, imagine you shoot at 45 degrees from x0, y0. At time 0 the distance is 0, then you start increasing the shot distance by the shot speed, for example move the bullet at 200 pixels/second:

Do + 200*TimeDelta() to the variable "x"

This will give the current “x” position, right now you’ve to imagine you are shooting horizontally (angle = 0). Now you get the y through the “sin” function:

Do = sin(Bullet.Variable(x)) to the variable "y"

So far so good, you have the position x, y position but with angle = 0. Now the funny part, the equation to rotate a point (x, y) relative to an origin around that origin (ox, oy):

x' = ox + x*cos(angle) - y*sin(angle) y' = oy + y*cos(angle) + x*sin(angle)
This returns a new point (x’, y’) in global coordinates (because if you see I’ve already added the ox, oy).
Now just set the shot position = rotated point.

Here is an example, I’ve added other options as frequency and amplitude, to make it easier to customize :slight_smile: (3.48 KB)

You can do even more things in the update function, as increasing the amplitude in function of time (and reduce the frequency) to make a sine “cone” effect.


Thank you so so much for your help Lizard-13 ! you had been a really great help to my project :slight_smile: May i ask is this the harmonic oscillation formula?

Yes, but it would be the simplest oscillator. There are other types of oscillators that add complexity to the solution, for example in physics there’s a damping/overdamping harmonic oscillator, it adds a damping term that slow down the movement tending to approach the equilibrium position (it’s the most realistic oscillator since the air, friction, electrical resistance, etc. work as dampers).

Note that the rotation part is not part of the oscillator formula, the oscillation here is just the “sin(x)” part. The rotated point formula is super general and you can use it wherever you want, for example I use it a lot to rotate some relative points in the debug drawing for the new physics examples that will come with the brand new physics behavior :smiley:

Nice Work Lizard-13 :slight_smile: