我有一个矩形,当我按住鼠标按钮时,我希望该矩形一次跟随一条海峡线1像素移动到该点。
到目前为止,这是我的代码(我在其中添加了注释,以便您理解)
float distanceX = finalX - x; //the number of pixels needed to get to destination on the X axis
float distanceY = finalY - y; // same as above but Y axis
float moveX = distanceX > 0 ? 1 : -1; // I only want it to move 1 pixel per render
float moveY = distanceY > 0 ? 1 : -1; // same as above
Array<Stuff> collidedX = new Array<Stuff>(); //saves collisions seperately for x and y
Array<Stuff> collidedY = new Array<Stuff>(); //because I want the square to move where the mouse is pointing even if it means only aligning one axis
for (Stuff s : collidables) {
if (overlapsT(s, x + moveX, y)) {
collidedX.add(s);
}
}
if (collidedX.size < 1) {
if (distanceX != 0)
x += moveX;
}
for (Stuff s : collidables) {
if (overlapsT(s, x, y + moveY)) {
collidedY.add(s);
}
}
if (collidedY.size < 1) {
if (distanceY != 0)
y += moveY;
}
现在的问题是它完全对角线,直到它与其中一个轴对齐,然后向左或向右向上移动到目的地。
我不想移动像素的分数。我的自定义物理引擎的工作方式是每个像素都很重要,分数像素不好,所以我试图找出如何平滑路径或者如何决定何时将1添加到x然后再添加y。