I have some coding in Java to calculate the movement in a virtual camera since the last time a variable was checked. More specifically, this code below:
float movementX, movementY, movementZ;
movementX = (int) (camX - sectorSize[1]);
movementY = (int) (camY - sectorSize[2]);
movementZ = (int) (camZ - sectorSize[3]);
/*
* If the variable is below 0
* then get the absolute value
* of the movement since the
* last camera position.
*/
if (movementX < 0) movementX *= -1;
if (movementY < 0) movementY *= -1;
if (movementZ < 0) movementZ *= -1;
if (movementX > 60 || movementY > 60 || movementZ > 60)
{
//Reset the sector size to allow for new points,
//don't store to save memory (may be changed later).
sectorSize[0] = 0;
}
If you need more of the code, let me know. The sectorSize variable stores 0-500 in its [0] value, the former camX in it’s [1] value, the former camY in it’s [2] value and lastly former camZ in its [3] value. The camX, camY, and camZ are handled by other code (not displayed). removed everything but the code-in-question to keep it tidy.
This code works as-is, but typing “if (a_int_value > an_other_value || etc)” each time is a bit tedious.
I would create a Movement class and encode the parameters for a “legal” movement within it.
Then you can test: movementX.isValid(). You can also keep a List or create another class to wrap the three axes and have a single isValid() method to test with.
You should consider storing sector sizes in something more descriptive than an array.
Finally, why are you casting your floats to int? What good can come from that? If you mean to strip off the decimal, there are much better ways to truncate, or floor/ceiling the value.