Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

askthedev.com Logo askthedev.com Logo
Sign InSign Up

askthedev.com

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Ubuntu
  • Python
  • JavaScript
  • Linux
  • Git
  • Windows
  • HTML
  • SQL
  • AWS
  • Docker
  • Kubernetes
Home/ Questions/Q 39610
In Process

askthedev.com Latest Questions

Asked: June 5, 20252025-06-05T04:14:10+05:30 2025-06-05T04:14:10+05:30

How can I implement wavy bullet movement in a specific direction using sine waves in my Lua game?

anonymous user

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!

  • 0
  • 0
  • 2 2 Answers
  • 0 Followers
  • 0
Share
  • Facebook

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Continue with Google
    or use

    Forgot Password?

    Need An Account, Sign Up Here
    Continue with Google

    2 Answers

    • Voted
    • Oldest
    • Recent
    1. anonymous user
      2025-06-05T04:14:13+05:30Added an answer on June 5, 2025 at 4:14 am

      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:

      function GameObject:init(...)
        -- existing initialization code here
        self.timer = 0
        self.waveAmplitude = 20 -- control the "wave" size
        self.waveFrequency = 5 -- control the speed of wave oscillation
      end
      
      function GameObject:move(dt)
        self.timer = self.timer + dt
        local baseVX, baseVY = self:getVelocity()
        local perpAngle = math.rad(self.angle + 90)
        local offsetX = math.cos(perpAngle) * math.sin(self.timer * self.waveFrequency) * self.waveAmplitude
        local offsetY = math.sin(perpAngle) * math.sin(self.timer * self.waveFrequency) * self.waveAmplitude
        self.x = self.x + baseVX * dt + offsetX * dt
        self.y = self.y + baseVY * dt + offsetY * dt
      end

      By adjusting waveAmplitude and waveFrequency, you can fine-tune the bullet’s smooth, dynamic movement to suit your game’s feel.

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. anonymous user
      2025-06-05T04:14:12+05:30Added an answer on June 5, 2025 at 4:14 am

      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:

      
      -- You might want to add these variables to your GameObject
      self.amplitude = 10  -- How high/low the wave goes
      self.frequency = 2    -- How fast the wave oscillates
      
      function GameObject:move(dt)
        -- Get linear velocity
        local vX, vY = self:getVelocity()
        
        -- Update the x position normally
        self.x = self.x + vX * dt
        
        -- Add a sine wave to the y position
        self.y = self.y + vY * dt + self.amplitude * math.sin(self.frequency * self.x)
      end
      
        

      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!

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Sidebar

    Recent Answers

    1. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    2. anonymous user on How do games using Havok manage rollback netcode without corrupting internal state during save/load operations?
    3. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    4. anonymous user on How can I efficiently determine line of sight between points in various 3D grid geometries without surface intersection?
    5. anonymous user on How can I update the server about my hotbar changes in a FabricMC mod?
    • Home
    • Learn Something
    • Ask a Question
    • Answer Unanswered Questions
    • Privacy Policy
    • Terms & Conditions

    © askthedev ❤️ All Rights Reserved

    Explore

    • Ubuntu
    • Python
    • JavaScript
    • Linux
    • Git
    • Windows
    • HTML
    • SQL
    • AWS
    • Docker
    • Kubernetes

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.