2

I am wondering how exactly I can convert a location (X Y Z) to a point on screen (X Y).

I have a player (the player you control) who is in (X Y Z) co-ordinates and another player who is also in (X Y Z) co-ordinates.

How exactly can I convert the other player's X Y Z to X Y on screen so that I can draw a name above him/it using the X Y.

Hope that makes sense...

Edit:

Here is my gluProject code:

IntBuffer viewport = GLAllocation.createDirectIntBuffer(16);
FloatBuffer modelview = GLAllocation.createDirectFloatBuffer(16);
FloatBuffer projection = GLAllocation.createDirectFloatBuffer(16);
FloatBuffer objectCoords = GLAllocation.createDirectFloatBuffer(3);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);
GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
GLU.gluProject(x, y, z, modelview, projection, viewport, objectCoords);
eturn objectCoords;

Thanks.

Timothy Hanes
  • 297
  • 1
  • 5
  • 17

4 Answers4

1

You should be able to deal with that:

public get2DFrom3D(float x, float y, float z)
{
    /*
    FloatBuffer screen_coords = GLAllocation.createDirectFloatBuffer(4);
    IntBuffer viewport = GLAllocation.createDirectIntBuffer(16);
    FloatBuffer modelview = GLAllocation.createDirectFloatBuffer(16);
    FloatBuffer projection = GLAllocation.createDirectFloatBuffer(16);
    */

    double[] screen_coords = new double[4];
    int[] viewport = new int[4];
    double[] modelview = new double[16];
    double[] projection = new double[16];


    GL11.glGetFloat(2982 /*GL_MODELVIEW_MATRIX*/, modelview);
    GL11.glGetFloat(2983 /*GL_PROJECTION_MATRIX*/, projection);
    GL11.glGetInteger(2978 /*GL_VIEWPORT*/, viewport);

    boolean result = GLU.gluProject(x, y, z, modelview, projection, viewport, screen_coords);
    if (result)
    {
        //System.out.printf("Convert [ %6.2f %6.2f %6.2f ] -> Screen [ %4d %4d ]\n", x, y, z, (int)screen_coords[0], (int)(screen_coords[3] - screen_coords[1]));
        System.out.printf("Convert [ %6.2f %6.2f %6.2f ] -> Screen [ %4d %4d ]\n", x, y, z, (int)screen_coords.get(0), (int)(screen_coords.get(3) - screen_coords.get(1)));
        return new Vector2(screen_coords[0], screen_coords[3] - screen_coords[1]);
    }
    else
    {
        System.out.printf("Failed to convert 3D coords to 2D screen coords");
        return null;
    }
}
Manitoba
  • 8,522
  • 11
  • 60
  • 122
0

To find the screen coordinate corresponding to a point in the world, apply the camera transform to the world point. You don't say what 3D framework you are using, but almost certainly there is an API call you can make to perform this transformation. For example, in the Unity3D framework, you call Camera.WorldToScreenPoint, and in OpenGL, you call gluProject.

However, this isn't necessarily the best way to render "head up" displays like character names. Another technique is to create billboards, which are objects in the world that always face the camera. Again, your 3D framework probably has support for this: for example, in Unity3D you might call object.transform.LookAt(camera.transform).

Gareth Rees
  • 64,967
  • 9
  • 133
  • 163
  • Thanks for the gluProject idea. I am indeed using OpenGL (Java). I tried it out although it the X Y Z returned isn't quite exactly on the player. I am actually creating a mod/plugin for Minecraft, via the reason I need to get their X Y from the X Y Z. If you want, I can send you a picture of what it looks like when I draw a line to them. – Timothy Hanes Apr 06 '12 at 17:28
  • I updated the main post with my gluProject code. I also tried to inverse the y coordinate as it said it returns a negative y coordinate. That didn't work. It seems as though the X Y isn't rotating with the player's rotation as it just stays in one point even if you look to the left/right. Any ideas? – Timothy Hanes Apr 06 '12 at 17:28
0

a method to plot 3d points in 2d exists. you will have to define a focal length parameter. the following code does it correctly

int 3D_2Dx(int pt[3]){
int focallength = ;//your focal length parameter i.e. 30,45,60,....
float multiplier =  focallength/(focallength+pt[2]) ;// pt[2] is its z co-ordinate
return (pt[0]*multiplier); //got the x co-ordinate
}
int 3D_2Dy(int pt[3]){
int focallength = ;//your focal length parameter i.e. 30,45,60,....
float multiplier =  focallength/(focallength+pt[2]) ;// pt[2] is its z co-ordinate
return (pt[1]*multiplier); //got the y co-ordinate
}

this code will do it perfectly. even I had made 3d graphics in flash 6 which is based totally on 2d.

Fennekin
  • 216
  • 3
  • 12
-1

this is a maths, not programming question. the answer depends on where you're looking from and where your 'screen' is sitting in relation to your viewpoint and the player.

Essentially you need to draw a line from the viewpoint to the position in 3D, then calculate the x,y coordinates on a 2D plane (the screen) which intersects the line. - i think!

Ferguzz
  • 5,777
  • 7
  • 34
  • 41
  • That's like saying asking for "how to protect my software" isn't programming, it's obfuscation, cryptography, etc. I never said it was a programming question either, I am making a game and need to do this to draw a name above their head. Drawing a line to them would require you to know their X Y... – Timothy Hanes Apr 04 '12 at 11:27
  • 2
    this a programming forum. 'how to to protect my software' wouldn't be a particularly good question here either. 'how to protect my software using the so-and-so algorithm in C' is a programming question. you know the coordinates of both your viewpoint and player. so you can calculate the line between them. – Ferguzz Apr 04 '12 at 11:38
  • OK, let me change the title of this just for you. "How do I convert a 3D location to a 2D point in Java (OpenGL)?" that good? Anyways, thanks for that answer. Not quite what I'm looking for. – Timothy Hanes Apr 04 '12 at 17:14
  • I also don't understand why you are so hostile about this. But as I said, if you're just going to tell me I'm doing something wrong then please leave this thread. Thanks. – Timothy Hanes Apr 04 '12 at 18:06
  • 1
    i was not hostile. i'm trying to help you to ask better questions in future. perhaps read this http://stackoverflow.com/faq – Ferguzz Apr 04 '12 at 22:29