1
votes

I am developing my first libgdx 3D game. Until now i can move arround in a maze-like (hardcoded) world, collision detection works. Also i have some enemies with working A* Pathfinding. I also loded my first (pretty ugly) Blender model, using FBX-Conv to get a .g3db file. For some reason the model lise on the floor instead of standing. Maybe i had some wrong settings when i exported it as .fbx. For that i tryed to rotate() him arround the z-Axis by 90 degrees by calling: modelInstance.transform.rotate(Vector3.Z, 90) in the show() method of my Screen, after loading the Model and instantiating my ModelInstance (at a given position). For some reason it did not rotate. Then i put the rotate method in the render(delta), thinking, that it would now rotate 90 degrees every render loop. But instead it was standing still, like it should. Okay, but now i want the modelInstance to rotate to where it actually looks, meaning it should rotate, depending on my enemies Vector3 direction. I am allready setting his position with modelInstance.transform.setTotranslation(enemie.getPosition()) which works perfect. So i thought i can also use modelInstance.transform.setToRotation(Vector3 v1, Vector3 vs), with v1 = enemie.getPosition() and v2 = enemie.getPosition().add(enemie.getDirection). Note, that the position Vector is not used directly, as it would change its values inside the add() method. Doing this, i don't see the object anymore, meaning also its position is wrong.

Why is this happening? And how can i rotate my modelInstance by using the direction vector?

Thanks a lot.

2
Matrix4#rotate does rotate (post multiplies) the matrix. So you might want to include some code to show the problem. You also probably want to rotate -90 degrees on the X axis (not Z axis), or better yet check the export to FBX settings in your modeling application, see also: github.com/libgdx/libgdx/wiki/… Use Matrix4#setToRotation(Vector3, Vector3) or rotate(Vector3, Vector3) to rotate using a direction, where one vector is the direction and the other is face side of the model (e.g. Vector3.Z).Xoppa
@Xoppa Okay thanks again i will try that with the 2 Vecotr3s. I think i missunderstood them, cause i thougt, that one Vector3 is the object position and one is the target position (pos.add(direction)). If it does not work i will post the code. ThanksRobert P
@Xoppa i think it works now. I did not see, that Matrix4.setToTranslation() does not only set the position of the ModelInstance, but does also set it to an identity matrix first. At the moment my ModelInstances only look to front, back, left, right and so i am not sure if it really works, but i think it does. I will make another test soon. ThanksRobert P

2 Answers

1
votes

I solved this with @Xoppas help. The problem was:

  1. i used setToTranslation to move my Model to a given position, but this als resets the rotation
  2. I missunderstood the setToRotation(Vector3, Vector3) method.

So the solution was to to the setToTranslation first, and then use setToRotation(Vector3 direction, Vector3 face), where direction is the direction, in which my Model is looking and face is the face, which should look in this direction, in my case the Vector3.X.

Hope it helps someone else.

-1
votes

Worse case scenario, you could modify the transformation matrix using:

ModelInstance.transform.rotate()