gpt4 book ai didi

Java:对象在路径上移动,忽略某些坐标。怎么修?

转载 作者:行者123 更新时间:2023-11-29 09:08:28 25 4
gpt4 key购买 nike

我有一个从点 A 移动到随机点 B 的圆。当对象接近点 B 时,会选择一个新的随机目标位置。如果圆圈平行于 X 轴或 Y 轴移动,则对象会穿过沿途的所有像素并留下实线轨迹。但是如果圆沿对角线移动,它会跳过像素并轻微抖动,使动画不流畅并留下未绘制像素的痕迹。

我的算法是:

  1. 计算X和Y距离
  2. 检查圆是否在附近
  3. 如果是,选择新的目的地
  4. 如果 2. 为真,使用毕达哥拉斯定理求出实际距离
  5. 如果2.为真,计算X和Y速度(坐标的变化)
  6. 设置新坐标(不管2.是否为真)

代码如下:

  public void move ()//движение
{
//finds the X and Y distance to the destination
int triangleX = nextLocationX - coorX;
int triangleY = nextLocationY - coorY;
//if too near the coordinates of the destination changes
if (Math.abs(triangleX) <= Math.abs(speedX) || Math.abs(triangleY) <= Math.abs(speedY))//setting the new target
{
//setting the new destinatio
int randInt;
for (;;)//I don't want the the new destination to be that same spot
{
randInt= randGen.nextInt(appletX);
if (randInt != nextLocationX)
{
nextLocationX = randInt + radius;
break;
}
}
for (;;)
{
randInt = randGen.nextInt(appletY);
if (randInt != nextLocationY)
{
nextLocationY = randInt + radius;
break;
}
}
//calculating the change of the circle's X and Y coordinates
triangleX = nextLocationX - coorX;
triangleY = nextLocationY - coorY;
speedX = ((double)(speed * triangleX) / (Math.sqrt (Math.pow(triangleX, 2) + Math.pow(triangleY, 2))));
speedY = ((double)(speed * triangleY) / (Math.sqrt (Math.pow(triangleX, 2) + Math.pow(triangleY, 2))));
}
//the realCoor variables are from type double
//they are the exact coordinates of the circle
//If I only use integers, the circle almost
//never reaches it's destination
//unless the change of the coordinates gets calculated
//after every time they change
realCoorX = realCoorX + speedX;
realCoorY = realCoorY + speedY;
coorX = (int)Math.round(realCoorX);
coorY = (int)Math.round(realCoorY);
}

我怀疑问题出在坐标变化的计算上。

最佳答案

对我来说,这听起来像是一个别名问题。如果你画(!)一条不与坐标轴对齐的线,你会遇到同样的问题。如您所知,即对角线需要“半填充”像素才能看起来平滑。

您的解决方案是(取决于渲染技术)使用浮点位置计算。

关于Java:对象在路径上移动,忽略某些坐标。怎么修?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13611430/

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