# Object doesn’t rotate in the correct direction

Hi all,

Today i have a serious problem which I cant find any solution. I’ve a Camera wich is following an Object, the fonction works but now i would implement a fonction to adapt the rotation of the Object depending on the rotation of the Camera smoothly. So i’ve get the Angle of the Camera and the Object and make this procedure.

[java]

//Get the Angle//

f = (cam.getRotation().toAngles(null));

c = (obj.getLocalRotation().toAngles(null));

//Transform in Degree 0-360°//

if(f[1]< 0){

f[1] = f[1]+2FastMath.PI;

}

if(c[1]< 0){

c[1] = c[1]+2
FastMath.PI;

}

f[1] = (float) Math.toDegrees(f[1]);

c[1] = (float) Math.toDegrees(c[1]);

//End of the Conversion//

walkDirection.set(0, 0, 0);

camDir = cam.getDirection();

camLeft = cam.getLeft();

if(characterMove && i>100){ // i is a counter wich is incremented when the character is moving//

if(forward && !Boucle2 && !Boucle3 && !Boucle4){//Do not pay attention to variables “Boucle”//

Boucle1 = true;

//Methode qui modifie les valeurs de la methode précedente//

if(left){

Angle = 135;

camDirInc = 0.5f;

camLeftInc = 0.5f;

}

if(right){

Angle = 225;

camDirInc = 0.5f;

camLeftInc = -0.5f;

}

if(!left && !right){

camDirInc = 1;

camLeftInc = 0;

Angle = 180;

}

if(((c[1]+Angle)%360 < f[1])){

obj.rotate(0,0.005f,0);

}

if(((c[1]+Angle)%360 > f[1])){

obj.rotate(0,-0.005f,0);

}

}

/else{

avancerNotCharMove();

}
/

player.setWalkDirection(walkDirection);

}[/java]

This procedures works but when I change direction quickly. The object doing the opposite of the rotation he should do (Decrement the rotation when it should increment and vice versa.) An other problem from a certain point my character turns before getting back into the correct position.

Besides if you want more details tell me.

Ok i found the solution. After thinking for hours and tried many times. It works!

I’m still improving…

[java]

Angle = Angle%360;

walkDirection.set(0, 0, 0);

camDir = cam.getDirection();

camLeft = cam.getLeft();

directionEnabled = false;

if(characterMove && i>100){

if(forward && left){

if(Angle < 225){

Angle += 0.3f;

}

if(Angle > 225){

Angle -= 0.3f;

}

camDirInc = 0.5f;

camLeftInc = 0.5f;

}

if(forward && right){

if(Angle < 135){

Angle += 0.3f;

}

if(Angle > 135){

Angle -= 0.3f;

}

camDirInc = 0.5f;

camLeftInc = -0.5f;

}

if(back && !left && !right){

camDirInc = -1;

camLeftInc = 0;

if(Angle > 0){

if(Angle > 180){

Angle += 0.3f;

}

else{

Angle -= 0.3f;

}

}

}

if(forward && !left && !right){

camDirInc = 1;

camLeftInc = 0;

if(Angle < 180){

Angle += 0.3f;

}

if(Angle > 180){

Angle -= 0.3f;

}

}

if(left && !forward && !back){

camDirInc = 0;

camLeftInc = 1;

if(Angle < 270){

if(Angle < 90){

if(Angle < 0.5){

Angle = 359;

}

Angle -= 0.3f;

}

else{

Angle += 0.3f;

}

}

if(Angle > 270){

Angle -= 0.3f;

}

}

if(right && !forward && !back){

if(Angle < 90){

Angle += 0.3f;

}

if(Angle > 90){

if(Angle > 359){

Angle = 0;

}

if(Angle > 270){

Angle += 0.3f;

}

else{

Angle -= 0.3f;

}

}

camDirInc = 0;

camLeftInc = -1;

}

if(right && back){

if(Angle < 45){

Angle += 0.3f;

}

if(Angle > 45){

if(Angle > 359){

Angle = 0;

}

if(Angle > 225){

Angle += 0.3f;

}

else{

Angle -= 0.3f;

}

}

camDirInc = -0.5f;

camLeftInc = -0.5f;

}

if(left && back){

if(Angle < 315){

if(Angle < 0.5f){

Angle = 359;

}

if(Angle < 135){

Angle -= 0.3f;

}

else{

Angle += 0.3f;

}

}

if(Angle > 315){

Angle -= 0.3f;

}

camDirInc = -0.5f;

camLeftInc = 0.5f;

}

}

if(Angle < AnglePrec){

sens = -1;

}

if(Angle > AnglePrec){

sens = 1;

}

if(Angle == AnglePrec){

sens = 0;

}

}

//Last Method Obsolete//

/if((c[1]+Angle)%360 < f[1]){

obj.rotate(0,0.005f,0);

}

if((c[1]+Angle)%360 > f[1]){

obj.rotate(0,-0.005f,0);

}
/

/else{

avancerNotCharMove();

}
/

AnglePrec = Angle;

player.setWalkDirection(walkDirection);[/java]