Programming Tidbit #3 – Constraining Rotation to a Max Turn Rate Per Second

This is the third post in the series “Programming Tidbits.” These articles consist of useful bits of information or code that I would like to share, but are not necesarily full length articles.
Sometimes you may wish to constrain the amount of rotation an entity can rotate per second or tick or whatever, such as the turret on a tank or the turning of the tank itself. This can be done with a bit of math. It could seem a bit complicated without using the math libraries I have already created.
Here it is using my code:
We are going to assume the maxTurnRate will be in degrees per second.
if( maxTurnRate > 0 && currentVelocity.angleTo( newVelocity ) > ( maxTurnRate * MathUtils.DEG_TO_RAD ) * stepSize ) { var mat:Matrix2D = new Matrix2D(); mat.rotate( ( ( maxTurnRate * MathUtils.DEG_TO_RAD ) * stepSize ) * heading.sign( newVelocity ) ); mat.transformVector( currentVelocity); mat.transformVector( heading ); } else { velocity.x = _newVel.x; velocity.y = _newVel.y; }
The Code Explained
So let’s take a look at what is happening there. First, we are checking to see if maxTurnRate is > 0…because we will assume if it is set as 0 then the entity should be able to turn freely. If it is greater, then we will get the angle from our current velocity and check to see if it is greater than the max turn rate multiplied by the step size. The step size is generally just: tickSize / 1000.
The math to get the angle from one vector to another is:
public function angleTo( vector1:Vector2D, vector2:Vector2D ):Number { return Math.acos( dotProduct( vector1, vector2 ) / ( vector1.length * vector2.length ) ); } public function dotProduct( vector1:Vector2D, vector2:Vector2D ):Number { return ( vector1.x * vector2.x ) + ( vector1.y * vector2.y ); }
Next, if the rotation is greater than the max amount aloud…we will use a matrix to rotate the velocity vector to it’s correct position and transform it and the heading ( the direction the entity is going ). Note: The “sign” function just returns -1 if vector is counterclockwise, else 1 if clockwise. We need this to know which direction to rotate obviously.
The Matrix Code
Here is some of the matrix code, though I definitely don’t plan on explaining matrices in this post. Which is why it’s best to just use the library if you don’t actually understand the math part itself.
/** * Use to rotate. * ( Considers that the Vector object represents a point, not an actual vector ) * @param rot * */ public function rotate(rot:Number):void { var sin:Number = Math.sin(rot); var cos:Number = Math.cos(rot); multiply( new Matrix2D( cos, sin, 0, -sin, cos, 0, 0, 0, 1 ) ); } /** * Use to multiply two Matrixes...mainly used internally. * @param matrix The Matrix to multiply by. * */ public function multiply(matrix:Matrix2D):void { var m:Matrix2D = new Matrix2D(); //first row m.a1 = (a1 * matrix.a1) + (a2 * matrix.b1) + (a3 * matrix.c1); m.a2 = (a1 * matrix.a2) + (a2 * matrix.b2) + (a3 * matrix.c2); m.a3 = (a1 * matrix.a3) + (a2 * matrix.b3) + (a3 * matrix.c3); //second row m.b1 = (b1 * matrix.a1) + (b2 * matrix.b1) + (b3 * matrix.c1); m.b2 = (b1 * matrix.a2) + (b2 * matrix.b2) + (b3 * matrix.c2); m.b3 = (b1 * matrix.a3) + (b2 * matrix.b3) + (b3 * matrix.c3); //third row m.c1 = (c1 * matrix.a1) + (c2 * matrix.b1) + (c3 * matrix.c1); m.c2 = (c1 * matrix.a2) + (c2 * matrix.b2) + (c3 * matrix.c2); m.c3 = (c1 * matrix.a3) + (c2 * matrix.b3) + (c3 * matrix.c3); Set(m.a1,m.a2,m.a3,m.b1,m.b2,m.b3,m.c1,m.c2,m.c3); } /** * This is used to do the final transformation upon the Vector * @param point The Vector to apply this Matrix transformation to. * */ public function transformVector(point:Vector2D):void { var tempX:Number = (a1*point.x) + (b1*point.y) + (c1); var tempY:Number = (a2*point.x) + (b2*point.y) + (c2); point.x = tempX; point.y = tempY; }
And that’s all!
So anyways…I’ve shown the internal workings of the math code there for you, though the first code block is all that should really matter to you in the end…since it shows how to actually use the math.
No example today, since I’m pretty sure you can imagine a turret rotating at a max speed…lol. However, it will be used in the next Game Structure post for the entities.





