0
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ @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! \$\endgroup\$ Commented Jul 23, 2024 at 23:17

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ but if I do that then the x and y component will stay at zero always! \$\endgroup\$ Commented Jul 23, 2024 at 23:08
  • 1
    \$\begingroup\$ 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) \$\endgroup\$ Commented Jul 23, 2024 at 23:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.