我正在尝试在this tutorial之后在我的平台程序中实现绳索摆动。玩家似乎没有在绳索上摇摆,而是看起来像在滑下斜坡:他非常缓慢地向底部移动。
这是现在的样子:
相反,我希望玩家能够更加自然地运动,就像他确实在绳索上摇摆一样。
这是我的播放器类中的更新方法:
@Override
public final void update() {
setPosition(getNextPosition());
if (direction == Direction.LEFT && moving) {
getVelocity().x = -WALK_SPEED;
} else if (getVelocity().x < 0) {
getVelocity().x *= COEF_FRIC;
}
if (direction == Direction.RIGHT && moving) {
getVelocity().x = WALK_SPEED;
} else if (getVelocity().x > 0) {
getVelocity().x *= COEF_FRIC;
}
checkAsleep();
animations.update();
if (ropePoint != null) {
//getCenter() returns the center position of the player
if (getCenter().toPoint().distanceSq(ropePoint) > ROPE_LENGTH * ROPE_LENGTH) {
final Vec2D oldPosition = getCenter();
final Vec2D oldVelocity = getVelocity();
final Vec2D ropePosition = new Vec2D(ropePoint);
setCenter((oldPosition.subtract(ropePosition).unit().multiply(ROPE_LENGTH).add(ropePosition)));
setVelocity(oldPosition.subtract(getCenter()).unit().multiply(oldVelocity));
}
}
}
如果需要,这是我对
getNextPosition()
的实现。public final Vec2D getNextPosition() {
final int currCol = (int) (getX() / Tile.SIZE);
final int currRow = (int) (getY() / Tile.SIZE);
final double destX = getX() + moveData.velocity.x;
final double destY = getY() + moveData.velocity.y;
double tempX = getX();
double tempY = getY();
Corners solidCorners = getCornersAreSolid(getX(), destY);
boolean topLeft = solidCorners.topLeft;
boolean topRight = solidCorners.topRight;
boolean bottomLeft = solidCorners.bottomLeft;
boolean bottomRight = solidCorners.bottomRight;
framesSinceLastTopCollision += 1;
framesSinceLastBottomCollision += 1;
framesSinceLastLeftCollision += 1;
framesSinceLastRightCollision += 1;
if (moveData.velocity.y < 0) {
if (topLeft || topRight) {
moveData.velocity.y = 0;
tempY = currRow * Tile.SIZE;
framesSinceLastTopCollision = 0;
} else {
tempY += moveData.velocity.y;
}
} else if (moveData.velocity.y > 0) {
if (bottomLeft || bottomRight) {
moveData.velocity.y = 0;
tempY = (currRow + 1) * Tile.SIZE - moveData.collisionBox.getHeight() % Tile.SIZE - 1;
framesSinceLastBottomCollision = 0;
} else {
tempY += moveData.velocity.y;
}
}
solidCorners = getCornersAreSolid(destX, getY());
topLeft = solidCorners.topLeft;
topRight = solidCorners.topRight;
bottomLeft = solidCorners.bottomLeft;
bottomRight = solidCorners.bottomRight;
if (moveData.velocity.x < 0) {
if (topLeft || bottomLeft) {
moveData.velocity.x = 0;
tempX = currCol * Tile.SIZE;
framesSinceLastLeftCollision = 0;
} else {
tempX += moveData.velocity.x;
}
}
if (moveData.velocity.x > 0) {
if (topRight || bottomRight) {
moveData.velocity.x = 0;
tempX = (currCol + 1) * Tile.SIZE - moveData.collisionBox.getWidth() % Tile.SIZE - 1;
framesSinceLastRightCollision = 0;
} else {
tempX += moveData.velocity.x;
}
}
return new Vec2D(tempX, tempY);
}
我应该在此代码中进行哪些更改才能自然运动?
最佳答案
我的第一个猜测是问题出在第一个if语句中:if (direction == Direction.LEFT && moving) { getVelocity().x = -WALK_SPEED; } else if (getVelocity().x < 0) { getVelocity().x *= COEF_FRIC; }
如果第一件事是对的,那么您将不断将速度设置为步行速度,而当您的人用绳子摆动时,这没有任何意义。当他下降时,他应该加快速度,在上升过程中,它应该减速。
如果第一件事是错误的,那么既然他要走了,那么您肯定会进入else if语句,并且他会因摩擦而放慢速度。我看不到您在哪里设定,但似乎仍然是他在地面上遇到的摩擦,这似乎可以解释为什么他都很结实,看上去更像是在滑而不是跌倒。
您可能想要添加不同的状态,而不仅仅是“移动”(也许是跳跃,摇摆,行走,奔跑,停止),并改变他在做这些事情时的行为方式。
关于java - 游戏绳摇摆物理代理奇怪,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25456447/