Quick question: I have a problem that keeps recurring. It looks like this: I have a set of sprite frames (say, 32) for an opponent in my game. Each frame represents a possible orientation. When the enemy walks south, I display frame 16, east is 8, west is 24.
Anyway, when this enemy changes it's direction from east to north, it needs to rotate clockwise, since that is the shortest path.
Now, suppose it rotates from frame 30 to frame 2. It can easily be seen that rotating clockwise is best here, and at 32 there's a wrap-around.
However, catching this in code is tricky. I would like to have an efficient function that takes two orientations, both in the range 0..31, and calculates the shortest path between them.
The best thing I could come up with is this:
But that's two conditionals, and I need to use signed ints. I'm quite sure there must be a better way.
Anyone?
- Jacco.[/code]
Anyway, when this enemy changes it's direction from east to north, it needs to rotate clockwise, since that is the shortest path.
Now, suppose it rotates from frame 30 to frame 2. It can easily be seen that rotating clockwise is best here, and at 32 there's a wrap-around.
However, catching this in code is tricky. I would like to have an efficient function that takes two orientations, both in the range 0..31, and calculates the shortest path between them.
The best thing I could come up with is this:
- Code:
int dist32( int x, int y )
{
int d=y-x;
if(d>16) d=y-(x+32);
if (d<-16) d=(y+32)-x;
return d;
}
But that's two conditionals, and I need to use signed ints. I'm quite sure there must be a better way.
Anyone?
- Jacco.[/code]