I have a android game where I have to from time to time move camera (visible screen part) from point A to point B.
Obviously I could do it like that:
camera.setCenter(B.getX(), B.getY();
But it does not looks good, it simply jump immediately, and what I want to achieve is smooth movement from A to B. I can access onUpdate method which is loop updating certain game objects (so I can execute certain things in certain milliseconds)
I really can not figure out how to create such algorithm, to allow smooth movement between two points (Classifying I have no clue how to calculate what values should I add to the
camera.setCenter(camera.getX() + xValue, camera.getY() + yValue)
Because those values have to be calculated depends on what is distance between those two points.
If your game updates at 60 frames per second, and you want a transition to take two seconds, then it should take 120 frames to get there.
Thus, the amount it should move in any given update is
total/120.You can abstract this into a method that queues a movement, calculates the FPS and the amount to move each update, and calls
.setCenteron its current position plus the update amount. This is typically called interpolation. To determine how far you need to move, its no more than the absolute value ofcurrent - destination(it doesn’t matter which is in front of the other, so we just take the absolute value to ignore the sign. To be technical, distance is a scalar whereas position is a vector).So, to sum up in semi-code (I don’t know the actual contracts for these functions):
Then you simply need to keep track of when to stop, and you should be good to go.
Whether or not you’re using an engine that already has a means of doing this, I don’t know. But in terms of a pure algorithm, this seems the way to go.