gpt4 book ai didi

java - 如何以波浪模式移动对象?

转载 作者:行者123 更新时间:2023-12-01 14:08:49 25 4
gpt4 key购买 nike

我知道以下代码将沿直线移动对象。我怎样才能让物体以波浪线行进?我知道 x 变量需要额外的东西。

public void draw(Graphics2D g) 
{
g.setColor(Color.WHITE);
g.fillOval ((int) (x - r), (int) (y - r), (int)
(2 * r),
(int) (2 * r));

y++;


if (y - r > height)
y = -r;
}

最佳答案

使用正弦或余弦函数计算 y 作为 x 的函数。

乘以正弦或余弦函数以增加幅度(幅度有多高)

y = 100 * sin(x) // will make it have peaks of -100 and 100

除以 x 以增加周期。 (峰间距离)

y = sin(x/2) // will make it take twice the x distance between peaks.

类似这样的:

public void draw(Graphics2D g) 
{
g.setColor(Color.WHITE);
g.fillOval ((int) (x - r), (int) (y - r), (int)
(2 * r),
(int) (2 * r));

x++; // Left to right movement
// Example, modify the multipliers as necessary
y = 100 * Math.sin(Math.toDegrees(x/4))
}

关于java - 如何以波浪模式移动对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8342887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com