SUB_NormalizeAngles bug squashed

SUB_NormalizeAngles is a handy bit of code for rotating entities. It maps a vector of angles so that each has its unique value between 0 and 360 (the range that we usually deal with angles in). A subtle bug has been causing me problems, so I will now expose it.

Here is the original code

 vector ( vector ang ) SUB_NormalizeAngles =
{
	while( ang_x > 360 )
		ang_x = ang_x - 360;
	while( ang_x < 0 )
		ang_x = ang_x + 360;

	while( ang_y > 360 )
		ang_y = ang_y - 360;
	while( ang_y < 0 )
		ang_y = ang_y + 360;

	while( ang_z > 360 )
		ang_z = ang_z - 360;
	while( ang_z < 0 )
		ang_z = ang_z + 360;

	return ang;
};

Here is the fixed version

 vector ( vector ang ) SUB_NormalizeAngles =
{
	while( ang_x >= 360 )
		ang_x = ang_x - 360;
	while( ang_x < 0 )
		ang_x = ang_x + 360;

	while( ang_y >= 360 )
		ang_y = ang_y - 360;
	while( ang_y < 0 )
		ang_y = ang_y + 360;

	while( ang_z >= 360 )
		ang_z = ang_z - 360;
	while( ang_z < 0 )
		ang_z = ang_z + 360;

	return ang;
};

Did you catch it?

The edge case is a well known class of bug. What makes this one interesting is that even a lot of the time when you are dealing with the edge case it doesn’t matter that this function is bugged. There will still be problems around the cutoff between 0 and 360 even after the fix. The value of changing the function like this is that now if two angles are in truth the same facing, the function will return the same value. Previously ‘0 0 0’ and ‘0 360 0’ would both be returned unchanged, even though those two vectors are equal as angles.

Advertisements

3 thoughts on “SUB_NormalizeAngles bug squashed

  1. Does this mean the new Quoth will have a func_rotate_train that can do a loop without taking the long way back to 0 0 0?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s