Degrees, radians and comparing them

edited August 2012 in Help request
Yet again I stumped on a problem. I got an object which is rotated by 90 degrees. It is done either by adding or substracting value from the current rotation. Now, if I add the value everything's fine. But when I substract it, then my if clause doesn't return true.

The code for setting rotation:
if(rotation == 0){
				orxObject_SetRotation(cat, (orxMATH_KF_DEG_TO_RAD * 270));
			}else{				
				orxLOG("%f ? %f", orxMATH_KF_DEG_TO_RAD * 180, rotation - (orxMATH_KF_DEG_TO_RAD * 90));
				orxObject_SetRotation(cat, rotation - (orxMATH_KF_DEG_TO_RAD * 90));				
			}

The check code:
if(rotation == 0){
		orxLOG("0");
		orxVector_Set(&selectedCell, selectedCell.fX, selectedCell.fY - 35, selectedCell.fZ);			
	}else if(rotation == orxMATH_KF_DEG_TO_RAD * 90){
		orxLOG("90");
		orxVector_Set(&selectedCell, selectedCell.fX + 35, selectedCell.fY, selectedCell.fZ);		
	}else if(rotation == orxMATH_KF_DEG_TO_RAD * 180){
		orxLOG("180");
		orxVector_Set(&selectedCell, selectedCell.fX, selectedCell.fY + 35, selectedCell.fZ);		
	}else if(rotation == orxMATH_KF_DEG_TO_RAD * 270){
		orxLOG("270");
		orxVector_Set(&selectedCell, selectedCell.fX - 35, selectedCell.fY, selectedCell.fZ);		
	}

I guess it's connected with the precision?

Comments

  • edited August 2012
    It is indeed a floating point precision issue!

    Unfortunately you can't expect a float to be precisely one value after doing some math operations, it's simply unreliable.

    Here's an excellent series on floating points and their properties, what we can rely on and what we should avoid, by Bruce Dawson: http://randomascii.wordpress.com/category/floating-point/

    I'd recommend using an enum to track your orientation and then act upon your rotation based on that enum but not use the rotation itself to track the orientation. :)
Sign In or Register to comment.