Search code examples
javalibgdx

LibGDX translate objects without losing rotation


I am working on a game and use libGDX. I have a ball standing on the floor and can draw an arrow rotating around it if player press the keys. Everything is ok until here. But if my ball is moving (rolling on the floor) I can't update the arrow's position with its rotation. Please take a look at: my super drawing skills

Let's say the ball is in 1. state and rolling and the player pressing the right key in the meantime, after some seconds the ball reaches the 2. state and as you can see in the image, the arrow has rotated some and translated next to the ball. I can provide this if the ball is not moving but if the ball is moving i couldn't make it somehow.

Here are my codes in render method:

// ARROW MOVEMENTS
if(arrLeft)
    Graphics.rotateAround(arrowInstance, MyGdxGame.tmpVec.set(0.8f,0,0), Vector3.Y, 1);
if(arrRight)
    Graphics.rotateAround(arrowInstance, MyGdxGame.tmpVec.set(0.8f,0,0), Vector3.Y, -1);
if(arrUp)
    Graphics.rotateAround(arrowInstance, MyGdxGame.tmpVec.set(0.8f,0,0), Vector3.Z, 1);
if(arrDown)
    Graphics.rotateAround(arrowInstance, MyGdxGame.tmpVec.set(0.8f,0,0), Vector3.Z, -1);

my rotateAround method:

public static void rotateAround(ModelInstance instance, Vector3 radius, Vector3 axis, float angle){
    instance.transform.translate(-radius.x, -radius.y, -radius.z).rotate(axis, angle).translate(radius);
}

By the way here is the one of my dozens of trying codes and i was hopeful this will work but it has just make the arrow turning around own axis:

arrowInstance.transform.getRotation(quat, true);
arrowInstance.transform.set(MyGdxGame.tmpVec.set(getBallPos()).add(0.8f,0,0), quat);

Solution

  • I thought this again tried being smarter and found the solution. I'd should position the arrow at the ball's position first, then translate it from there in amount of radius I already used for my rotateAround method. The rotations automatically stored in transform matrix and no need to mess with it.

    arrowInstance.transform.setTranslation(getBallPos());
    arrowInstance.transform.translate(0.8f,0,0);
    

    ("0.8,0,0" was my radius that i use for my rotateAround method)