我确信其他人也有同样的问题,但经过大量的谷歌搜索后,我找不到任何完全相关的内容。
我正在使用Java2D开发2D自上而下的游戏。我有单独的渲染和物理线程。 移动是基于鼠标的,计算采用以下形式:
isMoving = false;
// Sets initial distance moved to 0
playersMoveX = 0;
playersMoveY = 0;
// If player clicks, determines the distance to click
if (MainGame.movement)
{
distanceX = MouseClass.changeInX;
distanceY = MouseClass.changeInY;
moveMarker();
}
// Prevents divide by zero error, and prevents variables from becoming negative
if (distanceX < 0)
distanceX = 0;
if (distanceY < 0)
distanceY = 0;
if (distanceX == 0 && distanceY == 0)
return;
// Sets the speed that the player moves (player moves a distance of moveFactor each cycle)
if (Player.sprint)
moveFactor = 2;
else
moveFactor = 1;
double sumDistance = Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
double deltaX = (moveFactor * distanceX / sumDistance) * dT;
double deltaY = (moveFactor * distanceY / sumDistance) * dT;
// Determines distance moved in x and y directions by using the ration of distanceX to distanceY
if (MouseClass.mousePositionX > GUI.fullScreenWidth / 2 - 1 && preventCollision(0, npcs, buildings, objects))
{
playersMoveX += deltaX;
}
else if (preventCollision(1, npcs, buildings, objects))
{
playersMoveX -= deltaX;
}
if (MouseClass.mousePositionY > GUI.fullScreenHeight / 2 + 5 && preventCollision(2, npcs, buildings, objects))
{
playersMoveY += deltaY;
}
else if (preventCollision(3, npcs, buildings, objects))
{
playersMoveY -= deltaY;
}
// Accounts for movement in the distance to the click
distanceX -= deltaX;
distanceY -= deltaY;
if (deltaX != 0 || deltaY != 0)
isMoving = true;
然后,我从每个对象的x中减去playerMoveX,从每个对象的y中减去playersMoveY。 我尝试调整物理计算的速率,当它只渲染到60时,每秒超过80'帧,没有变化。
问题
当玩家移动时,物体会反弹。播放器在屏幕中居中,因此其他所有内容都与其相关。由于我使用双打,我处理舍入的方式总是用Math.floor()
进行舍入。答案 0 :(得分:0)
我从每个对象的x中减去playersMoveX,从每个对象的y减去playersMoveY。
这似乎不是好的设计,很可能是你问题的根源。查看Graphics
类中的翻译方法(我假设这是你用来绘制的),因为你真正想要的是翻译视角,而不是每一个可能的东西:
g.translate(playersMoveX, playerMoveY); // or some variation to center the player