I have been having this issue for a second day in a row. I’m using JBox2d for my game physics. In the game the camera is supposed to be on player. The problem is that box2d needs constant deltatime to update properly, and I’m not expecting very constant frame rate since the game is supposed to be played on mobile devices. To combat the issue of objects flickering while moving and jumping at non constant frame rate I implemented this: http://www.unagames.com/blog/daniele/2010/06/fixed-time-step-implementation-box2d
I made a small test game and the results seemed to improve when running at different frame rates while targeting 60 ups. The method in the link above seems to work nicely on standalone objects, but when the camera is following the object, you start to notice the stutters on the object which are not visible when only object is moving.
Any ideas how to make smooth camera motion to follow the player?
I know there are other ways of doing this, but you have a camera and an object you want to follow we’ll just say it’s the player.
They both have move speeds and positions. So what you can do is instead of setting the camera’s position to the player’s position (with an offset), you can set the movement goal of the camera to the player’s position and then have the camera move to the location of the player by using it’s move speed and a time delta. Just update the camera before the player and this should produce a natural lag.
I implemented something similar in a game I’m working. The axes are separated for an effect I’m doing in the game, but you could do them both at once. You also could directly change the
camera.position
, but I separated that as well so I could control it with other things.
// CameraPosition and targetPosition are both Vector2.
// Set the cameraPosition to the current camera position
cameraPosition.set(camera.position.x, camera.position.y);
// Set the target position to the middle of the player
targetPosition.set(player.getX() + (player.getWidth() / 2), player.getY() + (player.getHeight() / 2));
// Check for horizontal camera movement, if necessary
if (Math.abs(targetPosition.x - cameraPosition.x) >= 0 && !cameraMovementX) {
// If there is, begin a camera movement
cameraMovementX = true;
}
// If the camera gets really close, it will snap to the player. This is
// optional. I have this because really close float values were causing
// Graphical issues on my TileMap. You can remove it if it doesn't
// apply to you.
if (cameraMovementX && Math.abs(targetPosition.x - cameraPosition.x) > 4.05f) {
// If there is a camera movement, we will move closer to the player by small
// amounts. cameraInterpSpeed is a float with the value of 1.8f. Mess around
// and see what works for you.
cameraPosition.x += (targetPosition.x - cameraPosition.x) * cameraInterpSpeed * delta;
} else {
// Stop the camera movement if we are already close enough.
cameraMovementX = false;
}
// Check for vertical camera movement, if necessary
// Same things happening here, just with the Y value.
if (Math.abs(targetPosition.y - cameraPosition.y) >= 0 && !cameraMovementY) {
cameraMovementY = true;
}
if (cameraMovementY && Math.abs(targetPosition.y - cameraPosition.y) > 4.05f) {
cameraPosition.y += (targetPosition.y - cameraPosition.y) * cameraInterpSpeed * delta;
} else {
cameraMovementY = false;
}
// Update camera.
if (cameraMovementX || cameraMovementY) camera.position.set(cameraPosition.x, cameraPosition.y, 0);
camera.update();
renderer.setView(camera);
Hopefully this helps. Some parts of it are specific to how I’m handling the game, but the general camera movement is a “smooth follow.”
Specifically, this portion if you don’t want to use anything else: