所以基本上我目前正在做一个小项目,玩家将能够通过移动来对抗东西,但重力给我带来了一些问题。所以基本上,我得到了这个方法, checkGravity() 它将检查 isGravityApplicable() 是否返回 true (实体的底部对撞机(一个 Line2D)是否与一个立足点发生碰撞?(另一个 line2D))
如果 isGravityApplicable() 返回 true,则应该应用重力,但我遇到的问题是 1 像素 Y 以上的任何东西都太快了,甚至,将角色移动一个像素非常快。我不确定我是否应该修复我的游戏循环或什么?
public boolean isGravityApplicable() {
for (Line2D line : frame.getMap().getFootholds()) {
/*Does the bottom collider intersect the foothold?*/
/*v THIS CHECK DOESN'T WORK CORRECTLY FOR SOME REASONS v*/
if (!GraphicHelper.getLineCameraRelative(getBottomCollider(), frame.getCam()).intersectsLine(line)) {
return true;
}
/*Above check returned false, if the velocity.y is above 1, then it might
skip the line since it would be skipping 5 pixels for example at once.
this check should resolve that.*/
if (velocity.y > 0) {
Line2D collider = getPositionToVelocityCollider();
if (!collider.intersectsLine(line)) {
position.y = (int) line.getY1(); //Y1 or Y2, same sh*t.
return true;
}
}
}
return false;
}
public void checkGravity() {
if (isGravityApplicable()) {
if (isJumping) { //Player has jumped
velocity.y += 1;
velocity.y = velocity.y > TERMINAL_ACCELERATION ? (int) TERMINAL_ACCELERATION : velocity.y;
if (velocity.y < 0) {
isFalling = true;
}
} else if (isFalling) { //Player is currently falling but not from jumping. Most likely just spawned
velocity.y = 1;
/*Anything higher than 1 is WAY too quickly...*/
//velocity.y += 1;
//velocity.y = velocity.y > TERMINAL_VELOCITY ? (int) TERMINAL_VELOCITY : velocity.y;
} else if (isJumping && isFalling) { //Player has jumped and has reached its highest point, falling back down
velocity.y = 1;
/*Anything higher than 1 is WAY too quickly...*/
//velocity.y += 1;
//velocity.y = velocity.y > TERMINAL_VELOCITY ? (int) TERMINAL_VELOCITY : velocity.y;
} else { //Player hasn't jumped and has not started falling, he most likely just spawned
isFalling = true;
velocity.y = 1;
}
} else {
isFalling = false;
isJumping = false;
}
}
提前致谢。哦,如果有什么我可以做得更好的,比如由实体周围的简单线条组成的碰撞,请告诉我。:) 谢谢。