游戏 - 将俯仰和偏航转换为XY

时间:2012-04-09 19:09:18

标签: java

我正在尝试在屏幕上的某个点上显示图像。

我有当前点的音高和偏航。我只需要将这些值转换为2个坐标点。

这是我到目前为止所做的:

double tmp_yaw = yaw - myPlayer_yaw;
double tmp_pitch = pitch - myPlayer_pitch;

if (tmp_yaw < -180D) tmp_yaw += 360D;
if (tmp_yaw > 180D) tmp_yaw -= 360D;

// X Y screen coords
int x = (tmp_yaw / 180) * (screen_width / 2);
int y = (tmp_pitch / 90) * (screen_height / 2);

乍一看,这段代码看起来很简单,但我不知道为什么我没有显示预期的位置。

变量yawpitch在这里是3D中旋转点。

变量myPlayer_yawmyPlayer_pitch代表玩家随时可以看到的位置。

我做错了吗?

enter image description here

我想得到那样的结果:

  • 我正在寻找一名球员=&gt;返回(高度/ 2,宽度/ 2)
  • 玩家在我后面=&gt;返回(高度,宽度/ 2)
  • 玩家在我的左边=&gt;返回(高度/ 2,0)
  • 玩家在我右边=&gt;返回(高度/ 2,宽度)
  • 玩家就在我的上方=&gt;返回(0,宽度/ 2)

1 个答案:

答案 0 :(得分:0)

最后我使用vertex3D

解决了这个问题

以下是代码:

public void drawLine(Vector3 origin, Vector3 destination, int color)
{
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
    GL11.glLineWidth(3.0F);
    GL11.glDisable(GL11.GL_DEPTH_TEST);
    GL11.glDepthMask(false);
    GL11.glBegin(GL11.GL_LINES);

    float red = (float)((color & 0xFF0000) >> 16);
    float green = (float)((color & 0x00FF00) >> 8);
    float blue = (float)(color & 0x0000FF);
    GL11.glColor3f(red, green, blue);

    double diffX = destination.x() - origin.x() + 0.05D;
    double diffY = destination.y() - origin.y() - 1.35D;
    double diffZ = destination.z() - origin.z() - 0.05D;

    GL11.glVertex3d(0.0D, 0.0D, 0.0D);
    GL11.glVertex3d( -diffX, -diffY, -diffZ);

    GL11.glEnd();
    GL11.glDepthMask(true);
    GL11.glEnable(GL11.GL_DEPTH_TEST);
}

你可以这样使用它:

Vector3 vOrg = new Vector3(x, y, z);
Vector3 vDest = new Vector3(cameraX, cameraY, cameraZ);
drawLine(vOrg, vDest, 0xFF0000); // Will display a red line (RGB format)

我希望这会对某人有所帮助。

相关问题