The above screenshots are of the code used and the variables used; camX, camY, targetSxeneX, targetSxeneY, distanceBetweenX and distanceBetweenY.
The code is getting the current camera and target x and y positions. Then working out the distance between the camera and the target. It then checks if it needs to check if it needs to takeaway or add on to the camera destination to close the gap.
I only figured this out when talking to someone about the problem and realise the solution was an equation I was using, but not using in the correct way.
Before I used:
if (camX > targetSceneX) {
camX -=1
}
else if (camX < targetSceneX) {
camX +=1
}
This worked great for whole numbers, but when it came to decimal points, it kept flipping back and forth. I created an infinite loop. Example: +1 would make a 1 pixel difference spot on, but a 0.1 pixel difference a 0.9 difference. A change of 0.01 per tick would make it always correct, but that would’ve been so slow.
On the other hand the new equation:
if !(1 >= distanceBetweenX) {
camX += 1;
}
else if !(-1 <= distanceBetweenX) {
camX -= 1;
}
Checks the distance between point A and B, if the distance is not lesser or equal to one, then add one. Then the complete opposite for taking away.
This will check if the distance is within 1 to 0.01 or -1 to -0.01, otherwise it will close the gap.
Here is an example of it in play:
The only problems with it so far is, that I can’t make huge speed jumps like 32 pixels per tick, because the camera will have a max offset of 32 pixels and I need to make rooms a bit bigger, so I can hide the black background.