I didn't run it but it looks like it would overrun slightly on the last rotation because it doesn't check if you have gone past toRotate until after it has done it.
if you could explain this one too that'd be great…
i noticed when i walk forward until i reach the wall, then u-turn and repeat this again and again,
Z axis get offset and little by little i get out of the center.
sorry about my english, i'm trying to figure how i could say this best …
when i walk straight forward, only X axis should change… (+ or -)… but here i see Z getting strange offset too…
here's the code i use:
toMove = 2.45f
playerSpeed = 4f
if (dir == Direction.FORWARD) {
float value = 0;
value = playerSpeed*tpf*-1;
toMove += value;
playerNode.getLocalTranslation().x += cam.getDirection().x * toMove*-value;
playerNode.getLocalTranslation().z += cam.getDirection().z * toMove*-value;
if (toMove <= 0) {
isMoving = false;
}
any idea what could cause this?
is this the same kind of problem ? "value" would get > than "toMove" ?
i tryed similar workaround as for rotations but that didn't seem to help?
or would that be because of initial rotation/position is wrong, and movement code while being ok just move along incorrect initial position thus resulting in getting the player out of the center ?
.. i can see the Z axis getting offset (when it shouldn't), but i don't have any clues as to why :/
So it starts off about right, but gets less straight the more you turn?
If so that sounds like your camera is never coming exactly back to true at the end of a turn.
Going off this code I'd guess that's a build up of tiny rounding errors. This is kinda inevitable when you are not referencing any fixed value.
In a turn, each frame you apply a small rotation to an unknown quaternion (we're never sure exactly what localRotation is after you first turn). Since the operations are not perfectly accurate, you will end up with vectors like (0.99999, 0, 0.00002) where you should have (1, 0 0). It gets worse over time as each rotation adds another tiny error.
There are a few options to combat this, but I'd say if you are only ever rotating about a single axis, then by far the easiest way is just to store the current angle. That way you know that at the end of each turn you have rotated by the exact angle you wanted to. So just add a new variable currentAngle, and do your rotations something like this:
tmpAngles[0] = 0; // X
tmpAngles[1] = currentAngle; // Y
tmpAngles[2] = 0; // Z
tmpRotationQuat.fromAngles(tmpAngles);
playerNode.setLocalRotation(tmpRotationQuat);
Also unless you're sure you want to move in the direction of the camera, I would change your move code to use the playerNode's direction. Even though they are attached it just seems clearer and less error prone (and it means you can do things with the camera if you want to):