Libgdx 使用 Vector2s 旋转角度
Libgdx rotating to angle with Vector2s
我正在尝试让玩家旋转并面向目标位置,但遇到了一个我无法解决的问题。
船在向目标位置旋转时,当到达目标位置正下方时,疯狂地左右移动,同时向下(远离目标位置)最终离开屏幕。
我不想限制屏幕,因为如果接近屏幕限制,缓慢旋转时会看起来很奇怪。在这种情况下,如果我确实限制了屏幕,它仍然会卡在底部,因为它一直在左右转动。我在 imgur 上上传了一张图片,试图更好地解释我的问题。
黄色是船移动的方向(它正在向目标位置旋转但会越过红线),绿色是朝向目标位置的虚构矢量,红色是另一个虚构矢量,代表船何时发疯.当绿线越过红线时,船开始飞出屏幕。
public void ai() {
switch (playerArrived()) {
case 0:
break;
case 1:
pTargetPos.set(ran(0, Gdx.graphics.getWidth()), ran(0, Gdx.graphics.getHeight()));
System.out.println("Player Destination Reached: " + pPos.x + "," + pPos.y + " Moving to new point: " + pTargetPos.x + "," + pTargetPos.y);
break;
}
turn();
move();
ePlayerBody.set(pPos.x, pPos.y, pWidth);
}
public int playerArrived() {
if (pTargetPos.x < pPos.x + pWidth && pTargetPos.x > pPos.x - pWidth && pTargetPos.y < pPos.y + pHeight && pTargetPos.y > pPos.y - pHeight) {
return 1;
} else {
return 0;
}
}
public float ran(float low, float high) {
return (float) (Math.random() * (high - low + 1)) + low;
}
public void move() {
pVel.set(pTargetPos.x - pPos.x, pTargetPos.y - pPos.y);
pVel.nor();
pVel.x *= sMaxSpeed + Gdx.graphics.getDeltaTime();
pVel.y *= sMaxSpeed + Gdx.graphics.getDeltaTime();
pVel.setAngle(pNewRotation + 90);
pPos.add(pVel);
}
public void turn() {
pRotation = ((Math.atan2(pTargetPos.x - pPos.x, -(pTargetPos.y - pPos.y)) * 180.0d / Math.PI)+180.0f);
pNewRotation += (pRotation - pNewRotation) * Gdx.graphics.getDeltaTime();
System.out.println(pRotation+" "+pNewRotation);
}
我在 imgur 上上传了一张图片,试图更好地解释我的问题
当您的飞船位于目标正下方,稍稍偏左时,调用 Math.atan2(pTargetPos.x - pPos.x, -(pTargetPos.y - pPos.y))
将 return 一个非常接近 π/2 的值。当您的飞船位于目标正下方且稍偏右时,该调用将 return -π/2。当您越过红线时,您的方向将翻转 180 度(π 弧度)。这就是为什么它是 "going crazy".
你需要想出一个更好的方法来确定你应该转向哪个角度。我会就此提供建议,但我仍然不清楚您期望的行为到底是什么。
这对我有用:
public void turn() {
pRotation = ((tPos.y - pPos.y) / (tPos.x - pPos.x) * 180.0d / Math.PI);
pNewRotation += (pRotation - pNewRotation) * Gdx.graphics.getDeltaTime();
}
public void move() {
pVel.set(pDir);
pVel.nor();
pVel.x *= sMaxSpeed + Gdx.graphics.getDeltaTime();
pVel.y *= sMaxSpeed + Gdx.graphics.getDeltaTime();
pVel.setAngle(pNewRotation + 90);
pPos.add(pVel);
}
我正在尝试让玩家旋转并面向目标位置,但遇到了一个我无法解决的问题。
船在向目标位置旋转时,当到达目标位置正下方时,疯狂地左右移动,同时向下(远离目标位置)最终离开屏幕。
我不想限制屏幕,因为如果接近屏幕限制,缓慢旋转时会看起来很奇怪。在这种情况下,如果我确实限制了屏幕,它仍然会卡在底部,因为它一直在左右转动。我在 imgur 上上传了一张图片,试图更好地解释我的问题。
黄色是船移动的方向(它正在向目标位置旋转但会越过红线),绿色是朝向目标位置的虚构矢量,红色是另一个虚构矢量,代表船何时发疯.当绿线越过红线时,船开始飞出屏幕。
public void ai() {
switch (playerArrived()) {
case 0:
break;
case 1:
pTargetPos.set(ran(0, Gdx.graphics.getWidth()), ran(0, Gdx.graphics.getHeight()));
System.out.println("Player Destination Reached: " + pPos.x + "," + pPos.y + " Moving to new point: " + pTargetPos.x + "," + pTargetPos.y);
break;
}
turn();
move();
ePlayerBody.set(pPos.x, pPos.y, pWidth);
}
public int playerArrived() {
if (pTargetPos.x < pPos.x + pWidth && pTargetPos.x > pPos.x - pWidth && pTargetPos.y < pPos.y + pHeight && pTargetPos.y > pPos.y - pHeight) {
return 1;
} else {
return 0;
}
}
public float ran(float low, float high) {
return (float) (Math.random() * (high - low + 1)) + low;
}
public void move() {
pVel.set(pTargetPos.x - pPos.x, pTargetPos.y - pPos.y);
pVel.nor();
pVel.x *= sMaxSpeed + Gdx.graphics.getDeltaTime();
pVel.y *= sMaxSpeed + Gdx.graphics.getDeltaTime();
pVel.setAngle(pNewRotation + 90);
pPos.add(pVel);
}
public void turn() {
pRotation = ((Math.atan2(pTargetPos.x - pPos.x, -(pTargetPos.y - pPos.y)) * 180.0d / Math.PI)+180.0f);
pNewRotation += (pRotation - pNewRotation) * Gdx.graphics.getDeltaTime();
System.out.println(pRotation+" "+pNewRotation);
}
我在 imgur 上上传了一张图片,试图更好地解释我的问题
当您的飞船位于目标正下方,稍稍偏左时,调用 Math.atan2(pTargetPos.x - pPos.x, -(pTargetPos.y - pPos.y))
将 return 一个非常接近 π/2 的值。当您的飞船位于目标正下方且稍偏右时,该调用将 return -π/2。当您越过红线时,您的方向将翻转 180 度(π 弧度)。这就是为什么它是 "going crazy".
你需要想出一个更好的方法来确定你应该转向哪个角度。我会就此提供建议,但我仍然不清楚您期望的行为到底是什么。
这对我有用:
public void turn() {
pRotation = ((tPos.y - pPos.y) / (tPos.x - pPos.x) * 180.0d / Math.PI);
pNewRotation += (pRotation - pNewRotation) * Gdx.graphics.getDeltaTime();
}
public void move() {
pVel.set(pDir);
pVel.nor();
pVel.x *= sMaxSpeed + Gdx.graphics.getDeltaTime();
pVel.y *= sMaxSpeed + Gdx.graphics.getDeltaTime();
pVel.setAngle(pNewRotation + 90);
pPos.add(pVel);
}