Monday 9 January 2012

Variable speed rotation with complex numbers

I wrote only last week that complex numbers are best for mostly fixed or uniform speed rotations. This is true in general, but there are ways to use complex numbers when the speed varies, as long as it varies in a straightforward way. In particular if the speed increases linearly, so with uniform angular acceleration, it can be modelled with complex numbers.


One example of this is easing into and out of a motion. This means the motion does not instantly start but takes time to come up to full speed, to simulate the uniform angular acceleration that's normal with a constant torque. This is more realistic, though it should be balanced against responsiveness: it is maybe better applied to an enemy's weapon than the user's in a game.


The code eases in over one second, so takes a second or thirty frames to get up to full speed. This is done by starting the rotation speed at 1/30 its full speed (in the function AimGun), then adding this same amount each frame until the full speed is reached. Being complex numbers angular speeds are added like angles, by multiplying the complex numbers. So this happens each frame until the maximum speed is reached (inside the function UpdateAim).


After thirty frames it continues at this speed (30° or π/6 radians a second) until it is pointing in the right direction. It doesn't ease in if the angle is under 15°, as it would not have time to finish the ease in.


Easing out can be done in a variety of ways. For this motion stopping 15° from the end and reversing the ease in should bring it to a halt. Or speed can increase to the half way point then decrease thereafter. Or it can initiate easing out on user command, which means the user has to anticipate how far in advance to issue the command.


The code is more complex than it should be as it still does all three sorts of motion, so has to keep track of the speed mode and the stage of the motion. I should have forked the code and removed the other variations but by the time I realised this it was too late. It does now indicate the mode/variation more clearly, and I made the gun larger and reversed it so it's clearer and so it doesn't overlap the path of the missile.


No comments:

Post a Comment