Smooth rotation fails in one out of two cases

Posted By: Xarthor

Smooth rotation fails in one out of two cases - 06/23/08 20:06

Hi,
I use the following snippet to smoothly turn entities:
Code:
var ship_rotateSpeed = 6;

function game_SmoothRotateToPos(&_pos)
{
	var target_vec[3];
	var angle[3];
	
	vec_set(target_vec,_pos);
	
	vec_diff(target_vec, target_vec, my.x);
	vec_to_angle(angle, target_vec);
	
	while(abs(my.pan-angle.pan) > ship_rotateSpeed)
	{
		if(ang(angle.pan - my.pan) < 0)
		{
   		my.pan -= clamp(abs(ang(angle.pan - my.pan)),0,ship_rotateSpeed) * time_step;
		} 
		else 
		{
	   	my.pan += clamp(abs(ang(angle.pan - my.pan)),0,ship_rotateSpeed) * time_step;
		}
		
		wait(1);
	}
	my.pan = angle.pan;
}


Now this works fine if the start pan of the entity is equal to zero.
However if the start pan is 180, it rotates but does not finish the rotation.
Instead it stops shortly before finishing it and never exists that while loop.

Anyone know why this happens?

Thanks in advance for any hints.
Posted By: TigerTao

Re: Smooth rotation fails in one out of two cases - 06/23/08 23:02

Its possible that the ship has hit the 180/-180 threshold and is stuck moving between going +pan/-pan. I've had trouble with using "ang" before.

However there is a very good solution to shortest angle rotation here:

http://www.coniserver.net/ubb7/ubbthreads.php?ubb=showflat&Number=201834

Yuo can also use testDummy's (thanks to him by the way) even shorter version of that:

var rotSp[3] = 20, 0, 0; // rotate speed
var vec_to_target[3];
var ang_to_target[3];

vec_diff(vec_to_target, player.x, my.x); // direction vector from my to you
vec_to_angle(ang_to_target, vec_to_target); // get angles of direction vector
temp[0] = ang(ang_to_target.pan - my.pan);
my.pan += min(rotSp.pan * time_step, abs(temp[0])) * sign(temp[0]);

Hope this helps.
Posted By: Xarthor

Re: Smooth rotation fails in one out of two cases - 06/24/08 08:03

Thanks TigerTao!
I figured it out and now got it working all nicely smile

Thanks again!
© 2024 lite-C Forums