Hello all,
i’m trying to create a methode to rotate the viewDirection of a character smoothly. So i get the current viewRotation angle and increase or decrease the current angle until the final angle is reached. But sometimes the mesh turns on itself. I noticed that it’s happened when the angle sign change.
Moreover ,occasionally, the incrementation direction is wrong. So the mesh turn in the wrong direction and take the biggest angle. I don’t know why.
[java]float angle = FastMath.atan2(this.getViewDirection().x, this.getViewDirection().z); //The current view rotation angle
Vector2f walkTemp2 = new Vector2f(direction.z, direction.x);
float angle2 = walkTemp2.getAngle(); // The final view rotation angle
text = String.valueOf(tpf);
if (angle2 - angle > 0.1) {
angle += tpf0.01;
} else if(angle2 - angle < -0.1) {
angle -= tpf0.01;
}
this.setViewDirection(new Vector3f(FastMath.sin(angle), 0, FastMath.cos(angle))); [/java]
Just use quaternions to rotate the vector:
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies
Mmmm i change my method but it’s still bugged. Nothing has changed. But the tutorial is very clear and helpful.
[java] float angle = FastMath.atan2(this.getViewDirection().x, this.getViewDirection().z);
Vector2f walkTemp2 = new Vector2f(direction.z, direction.x);
float angle2 = walkTemp2.getAngle();
text = String.valueOf(tpf);
Quaternion increment = new Quaternion();
if (angle2 - angle > 0.1) {
increment.fromAngleAxis(tpf0.01f, Vector3f.UNIT_Y);
} else if(angle2 - angle < -0.1) {
increment.fromAngleAxis(-tpf0.01f,Vector3f.UNIT_Y);
}
this.setViewDirection(increment.mult(this.getViewDirection()));[/java]
I don’t know what can i do…
Here a diagram of my problem.
When the angle2 changes from 3.14 to -3.14 the character turns on itself.
I finally resolve my problem. (after searching throughout the internet)
[java] Quaternion increment1 = new Quaternion();
increment1.fromAngles(direction.x, 0, direction.z);
Quaternion increment2 = new Quaternion();
increment2.fromAngles(this.getViewDirection().x, 0, this.getViewDirection().z);
Quaternion increment3 = increment2.slerp(increment1, increment2, 0.99f);
float[] tempAngle = {0,0,0};
increment3.toAngles(tempAngle);
Vector3f finalDirection = new Vector3f(tempAngle[0], 0 , tempAngle[2]);
this.setViewDirection(finalDirection);
this.setWalkDirection(finalDirection);[/java]