Single angle interpolation

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
xDan
Competition winner
Posts: 673
Joined: Thu Mar 30, 2006 1:23 pm
Location: UK
Contact:

Single angle interpolation

Post by xDan »

well, I was getting some kind of problems when using quaternions to smooth my camera movement. But since I only have yaw and pitch (both tied to mouse input) it seems reasonable to interpolate them separately.

So here we can interpolate a single angle. Also might be useful in 2D games.

e.g.

spaceShipCurrentAngle = interpolate_angle(spaceShipCurrentAngle, spaceShipDesiredAngle, speed*timeStep);

Code: Select all

// interpolate a single angle in degrees
// handles the wrap around.
f32 interpolate_angle(f32 a, f32 b, f32 alpha)
{
	// put between 0 and 360
	a = fmod(a, 360.0);
	b = fmod(b, 360.0);
	
	// The smallest arc doesn't cross the 0/360 point?
	if (fabs(b-a) <= 180.0)
		return a + (b-a)*alpha;
	
	f32 delta;
	
	if (a > b)
	{
		delta = 360.0 - a;
		a = 0.0; // equivalent to a += delta
		b += delta;
	}
	else // b > a
	{
		delta = 360.0 - b;
		b = 0.0; // equivalent to b += delta
		a += delta;
	}
	
	return fmod( ( a + (b-a)*alpha ) - delta, 360.0 );
}
Post Reply