3d world coordiantes to screen coordinates returns wrong value
See original GitHub issueI have a simple project to render a 3D model (simple cube) using Perspective camera in android. The model is rendered fine but I need to get its coordinates on the device screen. Using project method of the perspective camera returns the wrong x value which is out of bounds of the viewport (2048*1536), but for the y value it works fine. How can I achieve the right x,y screen coordinate from 3d model?
Vector3 red_cube;
private void loadModel() {
ModelInstance m1 = new ModelInstance(createPointModel(Color.RED));
red_cube = new Vector3(516326, 423652, 1200);
m1.transform.setToTranslation(node2);
modelInstances.add(m1);
}
Vector3 loc = new new Vector3(516340, 423635, 1200);
Vector3 dir = new float[]{220, -80, 0};
@Override
public void render() {
if (!isLoaded) {
loadModel();
isLoaded = true;
}
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Vector3 v = camera.project(red_cube);
Log.d("red_cube:", v.x + ", " + v.y);
Vector3 pointing = OrientationSensorFusion.getDirection(dir);
camera.position.set(loc);
camera.direction.set(pointing);
camera.update();
modelBatch.begin(camera);
modelBatch.render(modelInstances, environment);
modelBatch.end();
}
The returned value by the project method is -2314.235, 767.8047.
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Projecting a point from world to screen. SO solutions give bad ...
It's very confusing that it gives you back weird negative coords. Hope other people will find this answer useful.
Read more >ScreenToWorldPosition returns wrong coordinates the further i ...
I have tried providing different z value and nothing changes. I always get those small discrepancies and rotating the tank turret is inaccurate....
Read more >ofCamera::worldToScreen returning wrong coordinates?
To do this I want to get the 3D position of the point in world space, I then want to convert it to...
Read more >Computing the Pixel Coordinates of a 3D Point (Mathematics ...
To calculate the world coordinates of that vertex we need to multiply the point original coordinates by the local-to-world matrix: we call it...
Read more >Converting world space coordinate to screen space ...
The value does not appear to be in screen space coordinates and is not limited to a [-1, 1] range. What step have...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Your test is still not valid.
I changed the test to be conformant with what we require for issues, and there is no issue. https://pix.asidik.com/tomski-wGxLyR.mp4
Updated valid test:
Dear @Tom-Ski I’ve created a simplified version of my application which includes the problematic part. Regarding the project file size, I’ve included only the src folder of the Android project in the zip file. You can see the negative and out of bound values if you change the
orientationValues = new float[]{220, -80, 0};
in the Scene class. It would be gratefull if you could take a look at it. src.zip