# Modifying the transform of a modelinstance in libgdx

Using libgdx I need to move back and forth by a very small amount a soldier head:

I do the following: But the soldier head is flying up into the y axis out of the screen:

***Note:*** rockingfactor is either -0.005f or 0.005 in every call

Vector3 vectory = new Vector3();
hapsonSoldierReference.transform.getTranslation(vectory);
vectory.z+=deltaTime*rockinFactor;

/* Matrix4 transform = new Matrix4();
transform.set(hapsonSoldierReference.transform);
transform.translate(vectory);*/

// hapsonSoldierReference.transform.set(transform);
hapsonSoldierReference.transform.translate(vectory);


How can I fix this flying off the screen(y direction) problem!

• @DMGregory ok you were correct my friend! its weird cause I thought I needed the actual vector position of the model in order for it to work. I just needed to modify the Z component. So I needed the vector (0 , Z , 0) . I really dont understand why the other components of the vector were modified by doing what I was doing? weird!
– i_o
Commented Jul 23 at 23:17

It looks to me like you want this:

// Make a new vector that's (0, 0, 0)
Vector3 vectory = new Vector3();

// Make its z have a very small value in the + or - direction
vectory.z = deltaTime * rockinFactor;

// ADD vectory to the object's current translation
hapsonSoldierReference.transform.translate(vectory);


translate already means "add this offset on top of the current translation", so when you used these lines:

hapsonSoldierReference.transform.getTranslation(vectory);
vectory.z+=deltaTime*rockinFactor;


...you were adding the current translation twice, effectively doubling your offset from the origin with every call.

• but if I do that then the x and y component will stay at zero always!
– i_o
Commented Jul 23 at 23:08
• No, they will retain whatever values they had previously. Remember that translate is an addition: (x, y, z) + (0, 0, epsilon) = (x, y, z + epsilon) Commented Jul 23 at 23:16