I want to get the 3d coordinates (as a Vector3) from my model Instances. with the following code I can only get the original coordinates, but in the render method, my model is rotating around the Y-axis (and moving at the same time) and I want to get its coordinates each frame. I'm using a little class created by @Xoppa :

public static class GameObject extends ModelInstance {
    public Vector3 center = new Vector3();
    public Vector3 dimensions = new Vector3();
    public float radius;

    public BoundingBox bounds = new BoundingBox();

    public GameObject (Model model, String rootNode, boolean mergeTransform) {
        super(model, rootNode, mergeTransform);
        radius = dimensions.len() / 2f;

and here's my code:

public void render(float delta) {
    if (loading && manager.update()) {

    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());


    for(GameObject instance : instances){
        instance.transform.rotate(new Vector3(0, 1, 0), 0.5f);
        float x = instance.bounds.getCenterX();
        float y = instance.bounds.getCenterY();
        float z = instance.bounds.getCenterZ();
        System.out.println(x+" "+y+" "+z);
        if(isVisible(cam, instance)){
            modelBatch.render(instance, environment);

Please help, there's no function "getPosition" or sth like that, it's all about Matrix4 and I have never had a math course about it. I'm stuck.


Try something like:

Vector3 position;
position = modelInstance.transform.getTranslation(new Vector3());

This gets the position and stores it in the position vector.

Sometimes you might have to transform the transform with the view and projection matrix to get the correct result.

/* Make sure that you use a new Matrix4, otherwise you will 
   change the models transform, which we don't want to do. */
Matrix4 modelTransform = new Matrix4();
/* Multiply the transform with the combined matrix of the camera. */
/* Extract the position as usual. */
Vector3 position;
position = modelTransform.getTranslation(new Vector3());

This gets the position and stores it in the position vector and the transform and stores it in the modelTransform matrix. You should probably re-use this vector and matrix every time you get the position instead of creating new ones.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.