Question

I have been trying to make my projectile travel in a wave but I can't figure out, how to do that. Right now my bullet is going towards the right side of the screen. Here is my code:

public class Projectile {

    protected Vector2 position;
    protected Vector2 velocity;

    public Projectile(float x, float y){

        position = new Vector2(x, y);
        velocity = new Vector2(50, 0);
    }

    public void update(float delta){

        position.add(velocity.cpy().scl(delta));
    }
}

I saw on the web about the function Math.sin() and Math.toRadians() that I can use but I have no idea how to use them or implement them.

I tried something along the line of velocity.y += Math.sin(Math.toRadians(50)) before the position.add and my projectile is going downwards in a nice curve but I have no idea what am I doing at all! I want to be able to change the amplitude and frequency of the wave. Any help?

Was it helpful?

Solution

Okay solution should be simple:

What you are doing atm is: You have a 2D position. now you add a 2D vector to it, which represents your speed (50, 0) * delta where delta is the elapsed time. now think about how you could represent your x- and y-direction changes with a sine-function. this is not hard at all, it is just math.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top