Java:在路径上移动的对象忽略某些坐标。如何修复?
我有一个从点a移动到随机点B的圆。当对象接近点B时,会选择一个新的随机目标位置。如果圆平行于X轴或Y轴移动,则对象将穿过所有像素,并留下实心轨迹。但如果圆沿对角线移动,它会跳过像素并轻微抖动,使动画不平滑,并留下带有未绘制像素的轨迹 我的算法是:Java:在路径上移动的对象忽略某些坐标。如何修复?,java,Java,我有一个从点a移动到随机点B的圆。当对象接近点B时,会选择一个新的随机目标位置。如果圆平行于X轴或Y轴移动,则对象将穿过所有像素,并留下实心轨迹。但如果圆沿对角线移动,它会跳过像素并轻微抖动,使动画不平滑,并留下带有未绘制像素的轨迹 我的算法是: 计算X和Y距离 检查圆圈是否靠近 如果是,请选择新的目的地 如果2。如果为真,则使用毕达哥拉斯定理求实际距离 如果2。如果为真,则计算X和Y速度(坐标的变化) 设置新坐标(无论2.是否为真) 代码如下: public void move ()//д
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);
}
public void move()//жжжж
{
//查找到目标的X和Y距离
int triangleX=nextLocationX-coorX;
int triangleY=nextLocationY-coorY;
//如果离目的地坐标太近,则会发生变化
if(Math.abs(triangleX)对我来说,这听起来像是一个混叠问题。如果你画一条与坐标轴不对齐的线,你也会遇到同样的问题。正如你所知,即对角线需要“半填充”像素才能看起来平滑
您的解决方案是(取决于渲染技术)使用浮点位置计算。我明白我的误解,感谢您的澄清。我想,在这种情况下,算法看起来是正确的。我想我会猜两个问题中的一个(或者,听起来有点像两者)。可能是精度问题,低分辨率可能会使运动在对角移动时明显变得不平滑,因为运动不容易量化显示。第二,可能是重绘问题,这取决于实现,可能会闪烁或留下上一个动画帧的痕迹。在这种情况下,可能会有所帮助。我使用这些痕迹用于通过更新图像进行调试。我还有一条线连接圆的坐标和当前目标的坐标-更新图像时,线会变粗。是的…问题应该是精度。