Tweening a rotation, rotating the wrong way sometimes

Hey guys!

I am storing a rotation in degrees, and I am tweening this to animate rotation.
I have a problem with tweening from 0 -> 270 degrees for instance, in which case the object is rotating the longer way around.
It should just count downward through 0 to 360, because that is the shortest path. How can I approach this problem?

I am using the Universal Tween Engine by the way, in case there is a built-in solution.

Thanks in advance.

Not sure about a built-in solution, but you can easily determine which way to rotate by finding which way is longer and choosing the other, you’ll need a method for setting the rotation from 0 -> 360 or 360 -> 0(have you ever made a circular array?)

OR

you could find the angle between a “zero vector”, (0, -1) or (0, 0, -1) are what I prefer, and the direction you want to go, then just do a couple checks to make sure you don’t over rotate (> 360 or < 0) and apply the rotation

something like the following (warning math):



public void getAngle()
{
	Vector3 zero_vector = new Vector3(0.0f, 0.0f, -1.0f);
	Vector3 objective_dir = new Vector3(0.0f, 0.0f, 0.0f);

	objective_dir = m_goal.subtract(m_curr_position);
	objective_dir.normalize();
		
	m_angle = zero_vector.getAngleBetween(objective_dir);
}

public void lockDirection()
{
        if(m_angle - m_rot >= 180.0f) 	m_angle -= 360.0f;
        else if(m_rot - m_angle > 180.0f) 	m_angle += 360.0f;

        m_rot = m_rot * 0.90f + m_angle * 0.10f;

        if(m_rot >= 180.0f)	 m_rot -= 360.0f;
        else if(m_rot < -180.0f)	 m_rot += 360.0f;
}


//Part of my Vector3 class
public float getAngleBetween(Vector3 vec)
{
	Vector3 a = new Vector3(this.m_x, this.m_y, this.m_z);
	Vector3 b = new Vector3(vec.m_x, vec.m_y, vec.m_z);

	float angle = b.m_x * a.m_x + b.m_z * a.m_z;
	if(angle > 1.0f) angle = 1.0f;
	else if(angle < -1.0f) angle = -1.0f;
	angle = (float) Math.acos(angle);
	float dot  = b.m_x * (a.m_z) + b.m_z * (-a.m_x);
	if(dot < 0.0f) angle *= -1.0f;
	return radiansToDegrees(angle);
}

public static float radiansToDegrees(float radians) 
{
        return radians * 57.2957795f;
}