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