0
\$\begingroup\$

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);
        calculateBoundingBox(bounds);
        bounds.getCenter(center);
        bounds.getDimensions(dimensions);
        radius = dimensions.len() / 2f;
    }
}

and here's my code:

public void render(float delta) {
    super.render(delta);
    if (loading && manager.update()) {
        doneLoading();
    }

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

    batch.begin();
    backgroundSprite.draw(batch);
    batch.end();

    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.begin(cam);
            modelBatch.render(instance, environment);
            modelBatch.end();
        }
    }
}

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.

\$\endgroup\$
3
\$\begingroup\$

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();
modelTransform.set(modelInstance.transform);
/* Multiply the transform with the combined matrix of the camera. */
modelTransform.mul(camera.combined);
/* 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 | |
\$\endgroup\$

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.