I’m working on a bullet hell game and I’ve hit a bit of a snag when it comes to making my bullets move in a wavy pattern. Right now, I have this basic movement function that handles linear movement based on an angle and a speed value. It looks something like this:
“`lua
function GameObject:getVelocity()
local vX = math.cos(math.rad(self.angle)) * self.speed
local vY = math.sin(math.rad(self.angle)) * self.speed
return vX, vY
end
function GameObject:move(dt)
local vX, vY = self:getVelocity()
self.x = self.x + vX * dt
self.y = self.y + vY * dt
end
“`
This works fine for straight shooting, but I really want to incorporate some wavy movement. I’ve been reading up on sine waves and how you can use frequency and amplitude to create that swooping motion, but I’m a bit lost on how to actually do it in the context of my game.
My initial thought was to modify the `move` function, but I’m not sure how to apply the sine wave to the existing x and y coordinates. I’ve seen examples of sine waves moving side to side, but I want to control the wave motion in the direction the bullet is fired. I imagine it might involve modifying `self.y` with a sine function based on the elapsed time, and doing something similar with `self.x`, but keeping the wave in check so it doesn’t deviate too far off course.
Is there a straightforward way to integrate this into the code I already have? Should I be adjusting the velocity vector directly or setting up a separate variable to manage the sine wave? I want the bullets to feel like they’re gliding gracefully while still maintaining that directional intent. Any advice, ideas, or code snippets would be super helpful!
To achieve smooth, wavy bullet motion while maintaining directional control, you can add a sine-based oscillation perpendicular to the bullet’s trajectory. Rather than modifying your fundamental velocity directly, maintain an internal timer (e.g.,
self.timer
) that increments each frame, and use it to create sinusoidal offsets. First, compute a perpendicular (‘sideways’) vector by rotating your original trajectory by 90 degrees, then apply a controlled sine wave offset to this perpendicular direction. This ensures bullets follow your intended path while gracefully swaying side to side.Here’s how you might integrate this into your existing structure in Lua:
By adjusting
waveAmplitude
andwaveFrequency
, you can fine-tune the bullet’s smooth, dynamic movement to suit your game’s feel.You’re on the right track thinking about using sine waves for the bullet movement! To create that nice wavy motion while still keeping your bullets moving in the intended direction, you can indeed modify the `move` function to incorporate a sine wave based on the time elapsed.
The idea is to adjust the y-coordinate to include a sine function that oscillates based on how far the bullet has traveled. Here’s a simple way to achieve that:
What this does is use the current x-position (scaled by the frequency) to determine the offset for the y-position using the sine function. The `amplitude` controls how high the wave goes, and the `frequency` controls how fast the wave oscillates. Play around with these values to get the feel you want!
By updating `self.y` like this, you’ll get that nice wavy effect while still keeping the bullets moving in the initial direction they were fired. Just keep an eye on the amplitude so it doesn’t veer off course too much!