简体   繁体   中英

Game - Convert pitch and yaw to XY

I'm trying to display an image on a point on the screen.

I have the pitch and the yaw of the current point. All I need is to convert these values to a 2 coord points.

Here is what I've done so far:

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);

At first glance, this code looks easy but I don't know why I doesn't display the point where it is expected.

Variables yaw and pitch are here the rotation to the point in 3D.

Variables myPlayer_yaw and myPlayer_pitch stand for where the player is looking at any moment.

Did I do something wrong?

在此处输入图像描述

I want to get those kind of results:

  • I'm looking a player => Returns (height/2, width/2)
  • The player is behind me => Returns (height, width/2)
  • The player is on my left => Returns (height/2, 0)
  • The player is on my right => Returns (height/2, width)
  • The player is just above me => Returns (0, width/2)

Finally I solved it using vertex3D

Here is the code:

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);
}

You can use it like that:

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)

I hope this will help someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM