Game Development Community

Relative Angles?

by Paul /*Wedge*/ DElia · in Technical Issues · 03/30/2004 (1:23 pm) · 0 replies

I've been working on a door script for a few days now. An object has parameters for a start and end rotation, and when you try to open it, it rotates between these angles. However I want to be able to have the door always swing away from the player. I can reverse the swing direction of the door easily, the problem is figuring out whether or not I need too. I've done some code that sort of accomplishes this, but it's not really proper. All it's doing is getting the player's facing angle between -180-180, and offsetting that by 90 if the start angle of the door is different enough. Basically I check if the facing angle is negative or positive, and if the rotation between the door angles is negative or positive, and use that to determine if I need to reverse the rotation.

I know it must be possible to take the facing vector of the player and the start rotation of the door and get a positive or negative value that is relative to the start rotation of the door. I'm really burnt out on this code as is though, and I simply cannot think of anything now. It's sort of confusing, because the angles for my door use a 0-360 degree base, that I convert into the coordinates the game uses. I could switch it back to -180-180 now. It's like that right now though, coz' the "conversion" type of thinking was how I got the door swinging part working right in the first place.

This is just the code I'm using to try to determine whether or not to flip the rotation right now. This would probably have to be totally replaced by however it is you are supposed to be doing it.

//basically, this part gets a value based on what direction the player is looking 
//and then check against the door angles to determine if door should swing the
//in the other direction... this part isn't correct right now
%lookVector = VectorNormalize(%col.getEyeVector());
%angle = mRadToDeg(mAtan(getWord(%lookVector, 0), getWord(%lookVector, 1) ) );
			if(%obj.start >= 135 && %obj.start <= 225)
			{
			%angle += 180;
			if(%angle <= 90 || %angle >= 270){
			%angle *= -1;}		
			}
			if(%obj.start <= 45 || %obj.start >= 315)
			{
				%angle -= 180;
			if(%angle >= -90 || %angle <= -270){
			%angle *= -1;}
			}
			
			echo("facing angle is " @ %angle);
			//the full rotation value for swapping directions
			%maxRot = mAbs(%obj.end - %obj.start);
			//echo(%maxRot);			
			
			if(%obj.start >= 180 && %angle < 0 && %obj.end > %obj.start)
				%obj.end -= %maxRot * 2;
			if(%obj.start >= 180 && %angle > 0 && %obj.end < %obj.start)
				%obj.end += %maxRot * 2;
			if(%obj.start < 180 && %angle > 0 && %obj.end > %obj.start)
				%obj.end -= %maxRot * 2;
			if(%obj.start < 180 && %angle < 0 && %obj.end < %obj.start)
				%obj.end += %maxRot * 2;