我一直在尝试让我的射弹以波浪的形式行进,但我不知道该怎么做。现在我的子弹正朝着屏幕的右侧移动。这是我的代码:
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));
}
}
我在网上看到关于函数 Math.sin()
和 Math.toRadians()
我可以使用但我不知道如何使用它们或实现它们。
我在 position.add
之前尝试了一些类似 velocity.y += Math.sin(Math.toRadians(50))
的方法,我的抛射物正在运行以一条漂亮的曲线向下,但我根本不知道我在做什么!我希望能够改变波的幅度和频率。有帮助吗?
最佳答案
好的解决方案应该很简单:
您在 atm 上所做的是:您有一个 2D 位置。现在你向它添加一个 2D vector ,它代表你的速度 (50, 0) * delta
其中 delta 是耗时。
现在想想如何用正弦函数表示 x 和 y 方向的变化。这一点都不难,这只是数学。
关于java - 如何使弹丸在波浪中行进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23513090/