Original Post
Hi all, I'm somewhat new to the whole game programming thing, so some of what I'm trying to do may be a bit weird. Please bear with me if I don't immediately grasp what you're trying to tell me. I am designing a game with a 3rd-person behind-the-back camera (i.e., the camera rotates around the character as the mouse is moved. No problems there, luckily!) Since I'm trying to do something more adventure-style (as opposed to a 3rd-person shooter like Max Payne), I want to rotate the character to face in the direction that the movement button is pressed. This I have working, /but/ the character would instantly pop to that facing if I didn't do something - I want to rotate it over the course of a second or so to face that position, rather than just jumping to it. Herein lies my problem: I figured out how to get the character to do that... but only in one direction. All my attempts to make it rotate in the opposite direction when the new facing is, say, less than 180 degrees have resulted in strange errors (usually involving either not working at all, or popping back and forth at random). The relevant section of my code is below. Any help is appreciated. Thanks in advance!
if(charx!=oldx || charz!=oldz){
float newfacing;
vectorv charpos;
vectorv oldcharpos;
charpos.x=charx;
charpos.y=0.0f;
charpos.z=charz;
oldcharpos.x=oldx;
oldcharpos.y=0.0f;
oldcharpos.z=oldz;
charmovedist=getDist(&charpos,&oldcharpos);
float tempx=(charx-oldx)/charmovedist, tempz=(charz-oldz)/charmovedist;
if(tempx>1)
tempx=1;
if(tempx<-1)
tempx=-1;
if(tempx>=0.0f && tempz>=0.0f)
newfacing=acos(-tempx)*57.2957-90;
if(tempx<0.0f && tempz>=0.0f)
newfacing=acos(-tempx)*57.2957-90;
if(tempx<0.0f && tempz<0.0f)
newfacing=-acos(-tempx)*57.2957-90;
if(tempx>=0.0f && tempz<0.0f)
newfacing=-acos(-tempx)*57.2957-90;
while(newfacing<0)
newfacing+=360;
while(newfacing>360)
newfacing-=360;
if(facing!=newfacing){
facing+=(newfacing-facing)*ticks*7.5;
}
while(facing>360)
facing-=360;
while(facing<0)
facing+=360;
}