This is untested Java. It assumes a 2D world, but could be expanded into 3D without much work.
private static final int G_HALF = 1, L_HALF = 2, G_QTER = 4, L_QTER = 8;
public float[] getGravityVector( Entity ent ) {
int sector = 0; // Bitfield integer
int x = getLocalCoords( ent ).getX(),
y = getLocalCoords( ent ).getY();
if ( y > x + 2 ) sector = sector | G_HALF;
if ( y < x - 2 ) sector = sector | L_HALF;
if ( y > 2 - x ) sector = sector | G_QTER;
if ( y < -2 - x ) sector = sector | L_QTER;
switch ( sector ) {
case 0: default: return new int[]{0,0}; // At center, no gravity
case 1: return interpolate( new int[]{0, 1}, new int[]{ 1,0}, ip_factor );
case 2: return interpolate( new int[]{0,-1}, new int[]{-1,0}, ip_factor );
case 4: return interpolate( new int[]{0, 1}, new int[]{-1,0}, ip_factor );
case 5: return new int[]{0,1};
case 6: return new int[]{-1,0};
case 8: return interpolate( new int[]{0,-1}, new int[]{1,0}, ip_factor );
case 9: return new int[]{1, 0};
case 10: return new int[]{0,-1};
}
}
Something else has just come up while I was typing this, so I\'ll have to define interpolate() and ip_factor later.
(Great, tags aren\'t working either. Have a pastebin.)