question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

3d world coordiantes to screen coordinates returns wrong value

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
Tom-Skicommented, Aug 9, 2019

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:


import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.DirectionalLightsAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;

public class Scene extends ApplicationAdapter {

	PerspectiveCamera camera;

	ModelBatch modelBatch;
	Environment env;

	Vector3 redCubeTranslation = new Vector3();
	Array<ModelInstance> modelInstances = new Array<>();

	FirstPersonCameraController firstPersonCameraController;

	Vector3 temp = new Vector3();

	@Override
	public void create () {
		redCubeTranslation.set(10, 0, 10);

		cameraInit();
		modelBatch = new ModelBatch();


		loadModel();

		firstPersonCameraController = new FirstPersonCameraController(camera);
		Gdx.input.setInputProcessor(firstPersonCameraController);

		env = new Environment();
		env.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
		env.add(new DirectionalLight().set(1f, 1f, 1f, 1, 1, 1));

	}


	private Model createPointModel (Color color) {
		Model pointModel;
		ModelBuilder modelBuilder = new ModelBuilder();
		int s = 1;
		Material material = new Material(ColorAttribute.createDiffuse(color));
		long attributes = VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal;

		pointModel = modelBuilder.createBox(1f * s, 1f * s, 1f * s, material, attributes);
		return pointModel;
	}


	private void loadModel () {
		ModelInstance m1 = new ModelInstance(createPointModel(Color.RED));
		m1.transform.setToTranslation(redCubeTranslation);
		modelInstances.add(m1);
	}

	@Override
	public void render () {

		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1f);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
		Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

		temp.set(redCubeTranslation);
		camera.project(temp);
		System.out.println("Red cube screenspace position: "+ temp.x + "  " + temp.y + "  " + temp.z);


		firstPersonCameraController.update(Gdx.graphics.getDeltaTime());
		camera.update();

		modelBatch.begin(camera);
		modelBatch.render(modelInstances, env);
		modelBatch.end();
	}


	private void cameraInit () {

		camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera.near = 1f;
		camera.far = 500f;
		camera.update();

		camera.position.set(0, 0, 0);
		camera.lookAt(redCubeTranslation);
	}

	public static void main (String[] args) {
		Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
		config.setWindowedMode(1280, 720);
		new Lwjgl3Application(new Scene(), config);
	}

}

1reaction
NasserTahanicommented, Jul 28, 2019

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found