LibGDX: Rotating a model to a certain angle

Hi, I’ve been having trouble with the otherwise excellent LibGDX, trying to rotate a model to a certain angle. (BTW, I’m talking about rotating on the Y-axis). If I do model.setToRotation(), it overwrites the position (and scale probably).

How do other people do it? Do you store position, scale and rotation in separate vars, and then set the position, scale and rotation at the same time?

Hi, I had very limited expirience with 3D, and I don’t have clear understeanding of how calculations have done for that kinda matter, but I’ll try to share what I can.

To store rotation I created an object of Quaternion class, it stores 4 components of orientation in 3d space. Rotation object with position and scale passed to Matrix4 object like this:

public Matrix4 calculateTransform() {
        
        return new Matrix4(position, rotation, scale);
        
    }//end method calculateTransform

and it stored in the ModelInstance object:

public void act(float dt) {
        
        modelData.transform.set(calculateTransform());
        
    }//end method act

and this method is for changing components of rotation object:

public void turn(float degrees) {
        
        rotation.mul(new Quaternion(Vector3.Y, -degrees));
        
    }//end method turn

and this is done in update() method:


if(Gdx.input.isKeyPressed(Keys.Q))
            weaponObject.turn(-rotateSpeed * dt);

This is what I’ve done in my Alien Vehicle, actually it is from a book “Lee Stemkoski - Beginning Java Game Development with LibGDX (The Expert’s Voice in Java) - 2015”. I noticed its very popular amongst LibGDX newbies.

I hope it will be helpful, if you need my code I’ll share it somehow :slight_smile:

Thanks for the input. I’ve solved my problem by setting the various aspects of the model in the correct order, i.e.


model.transform.setToTranslation();
model.transform.scl();
model.transform.rotate();
modelBatch.render(model.model, environment);

The main cause of my problem is the various methods in the LibGDX Matrix4 class all seem to work slightly differently. For example, setTranslation() just sets the translation, whereas setToTranslation() resets the whole matrix first. I can’t see any rhyme or reason for determining which methods reset the matrix and which don’t.

I never observed Matrix4 closely :clue: But I think, I got, it says “overwriting it first by an identity matrix”, this is why order matters :slight_smile: