[Resolved] How do you have a game object move is a sinewave pattern?

Hey folks,

As my first beginner game, I’m building a vertical shooter.

I want the enemy ships to move in a sinewave pattern;

Despite my research on trigonometry and coding implementations, am spectacularly failing at this simple task.

What’s Happening

I recorded a video showing what my movement looks like.

While I do have movement that somewhat resembles a sinewave pattern, I fail to control the speed at which it moves along the x-axis.

So when I get this super fast-moving wave.

I have a video but since I’m a new users I am restricted to one external link in this topic.

What I Want To Happen

This is a video that implements this sinewave solution (using Unity).

The difference is this is a horizontal shooter.

My code is quite similar to their implementation

Implementation Example

This is my function, which results in the movement of my enemy object, and it is called in the Update game loop.

// param value
float seconds = (float)gameTime.ElapsedGameTime.TotalSeconds;

// movement function
  public void MoveSinWave(float seconds) {
      float amplitude = 50f;
      float frequency = 50f;
      float sinX = System.MathF.Sin(position.Y * frequency) * amplitude;
      position.X = sinCenterX + sinX;
      position.Y += MathF.Round(speed * seconds);
  } 

Considerations

All examples I see use the opposite position (in my case, the Y position) to drive the radian value in the Sin math function.

I would think that I could just create a different value that controls the rate of speed, but I don’t see any other implementation requiring to do this (and I have yet to test if this might produce more desirable results)

Any suggestions about my code or just my math logic here is greatly appreciated :pray:

Here is my video demo:

Its unlisted on Youtube so there’s no video preview of the thumbnail.

Okay so this is dumb, but I think solved my issue. :sweat_smile:

I was looking at replacing the contents of my Sin function position.Y * frequency with a separate sinSpeed variable, and as I was testing with frequency with 1 it was still too fast.

So then I thought, if the speed is too fast then I should just use a fraction value for the frequency, so at 0.1 , and yes of course I get a more desirable speed.

The position.Y speed when the enemy moves top to bottom doesn’t seem fast, but then again its multiplied by the seconds input which is fractional value.

So maybe I should be doing position.Y * seconds * frequency with frequency set as 0 or 1 and maybe I’ll a reasonable speed.

Okay so I updated my function and now I get the movement I want.

// values initialized elsewhere
amplitude = 500f;
frequency = 1f;

// moving function
  public void MoveSinWave(float seconds) {
      float sinX = System.MathF.Sin(position.Y * seconds * frequency) * amplitude;
      position.X = sinCenterX + sinX;
      position.Y += MathF.Round(speed * seconds);
  } 

Here you can see the new result.

Retrospective

I think where I was confused was I was visualizing the sinwave as being taller and wider based on the frequency and amplitude value being larger.

What I needed to do what to think of the Sin input as the speed, and so smaller number, would mean slower speed.

:star: We’ll call this one resolved.