JavaScript:
int[] x[(-4 to +4 step = 1 block resolution)] = {3 blocks, 5 blocks, ...}; // in real code: or use 0..8 with offset -4
// same for y and z
// update on block creation/deletion: x[block.x]++, -- or += number blocks
int left = 0;
int right = x.length;
int centre = right-left/2:
Initialize more realistic centre x:
int i = -4; // lower index; negative to positive index
int i2 = +4; // upper index; positive to negative index
while( i < i2){
. . if( left < right ) left+= x[i++];
. . . else right+= x[i2--];
}
centre = i // and i2 now both point as closest as possible to the centre.
/*
updates:
. to blocks left or right from the centre should update the counter-variables "left" or "right"
. compare diff(left, right)
. . to diff(( (left - x[centre-1]), (right + x[centre-1]) ))
. . and to diff(( (left + x[centre+1]), (right - x[centre+1]) ))
. take the centre with lowest difference and compare to a possible centre 1 index further in this direction.
Permanent variables:
1 integer per x, y and z position
3 integers: left, right, centre
3 integers: left, right, centre
calculations on block addition/loss (if changed in one step and with optimized calculations)
change 1 int for each different x, y, and z
currently 1 integer per block is updated anyway : block count
update integers for left or right, above or below, inFront or behindcurrently mass is updated.
each second:
check if an updated left/right or above/below, inFront/behind causes a movement of the centre of mass.
. about 4 variable increments/decrements, 6 subtractions/additions, 2 comparison.
. and 2 increments/decrements, 3 subtractions/additions 1 comparison per block which the centre of mass moves.
. about 4 variable increments/decrements, 6 subtractions/additions, 2 comparison.
. and 2 increments/decrements, 3 subtractions/additions 1 comparison per block which the centre of mass moves.
Last edited: