Convert a 3D location to a two-dimensional screen point. (XYZ => XY)

I am wondering how exactly I can convert a location (XYZ) to a point on the screen (XY).

I have a player (the player you control) that is in coordinates (XYZ), and another player who is also in coordinates (XYZ).

How exactly can I convert another XYZ player to XY on the screen so that I can draw a name over it / this with X Y.

Hope this 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.

+2
math 3d location point
source share
4 answers

You have to deal with this:

 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; } } 
+1
source share

To find the screen coordinate corresponding to a point in the world, apply the camera transform to a point in the world. You are not saying which three-dimensional structure you are using, but there is almost certainly an API call you can make to perform this conversion. For example, in the Unity3D structure, you call Camera.WorldToScreenPoint , and in OpenGL you call gluProject .

However, this is not necessarily the best way to display "head up" as character names. Another way is to create billboards that are objects in the world that always look at the camera. Again, your 3D structure probably supports this: for example, in Unity3D you can call object.transform.LookAt(camera.transform) .

0
source share

There is a method for constructing three-dimensional points in 2d. You will need to determine the focal length parameter. the following code does it right

 int 3D_2Dx(int pt[3]){ int focallength = ;//your focal length parameter ie 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 ie 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 made 3D graphics in Flash 6, which is entirely based on 2d.

0
source share

this is math, not a programming issue. The answer depends on where you look and where your β€œscreen” sits in relation to your point of view and to the player.

Essentially you need to draw a line from the point of view to a position in 3D, and then calculate the x, y coordinates on the 2D plane (screen) that intersects the line. - I think!

-one
source share

All Articles