Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGDX: Rotating a model to a certain angle  (Read 301 times)
0 Members and 2 Guests are viewing this topic.
Offline SteveSmith
« Posted 2020-02-10 19:41:44 »

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?

Java Multiplayer FPS Engine - https://github.com/SteveSmith16384/SteveTech1
Offline SugarBlood
« Reply #1 - Posted 2020-02-11 16:46:23 »

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:

1  
2  
3  
4  
5  
public Matrix4 calculateTransform() {
       
        return new Matrix4(position, rotation, scale);
       
    }//end method calculateTransform


and it stored in the ModelInstance object:

1  
2  
3  
4  
5  
public void act(float dt) {
       
        modelData.transform.set(calculateTransform());
       
    }//end method act


and this method is for changing components of rotation object:

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


and this is done in update() method:

1  
2  
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 Smiley

Offline SteveSmith
« Reply #2 - Posted 2020-02-12 10:50:25 »

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

1  
2  
3  
4  
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.

Java Multiplayer FPS Engine - https://github.com/SteveSmith16384/SteveTech1
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SugarBlood
« Reply #3 - Posted 2020-02-13 10:15:32 »

I never observed Matrix4 closely Clueless But I think, I got, it says "overwriting it first by an identity matrix", this is why order matters Smiley
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (438 views)
2019-09-04 15:33:17

hadezbladez (5292 views)
2018-11-16 13:46:03

hadezbladez (2212 views)
2018-11-16 13:41:33

hadezbladez (5561 views)
2018-11-16 13:35:35

hadezbladez (1156 views)
2018-11-16 13:32:03

EgonOlsen (4592 views)
2018-06-10 19:43:48

EgonOlsen (5467 views)
2018-06-10 19:43:44

EgonOlsen (3127 views)
2018-06-10 19:43:20

DesertCoockie (4023 views)
2018-05-13 18:23:11

nelsongames (4739 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!