[LibGDX] Getting smooth camera movement

I am in the process of creating a class that can handle camera positioning and effects behind the scenes and I am having an issue, I have this method here which you can use to have the camera follow the vector position of an object:

public void follow(float delta, Vector2 position, float offsetX,
			float offsetY) {
		if (isFixed)
			throw new CameraSettingsException(
					"You can not follow a fixed camera, use the correct constructor for a camera that can follow a vector");

		System.out.println(delta / 4);

		if (shakeEnabled) {
			if (shaking) {
				processShake(delta);
			}
			if (super.position.x >= (position.x)) {
				super.position.x -= position.x * delta;
			}
			if (super.position.x <= (position.x)) {
				super.position.x += position.x * delta;
			}
			if (super.position.y > (position.y)) {
				super.position.x -= position.y * delta;
			}
			if (super.position.y < position.y) {
				super.position.x += position.y * delta;
			}

		} else {
			super.position.set(position.x, position.y, z1);
		}
	}

It is fairly simple and it works, to a degree.

It follows the player just fine, from left to right. As you can see I have a shake boolean in there, this simply causes the camera to spasm around a little then reset on the object smoothly, as if an explosion occurred nearby.

The problem being is, jerky as hell, it is fine at the very left hand side but is super shaky at the right hand side. If I divide the delta time by 4 it works flawlessly but then the camera would not be able to keep up with whatever it is following if it moved quicker than the delta time. This seems due to the cameras x position increases as you move from left to right and is getting multiplied by the delta time, so it is obviously getting fired past the desired position and the next if statement in line is kicking in to knock it back, creating a back and forth between the 2 statements. If that makes sense.

I can easily use something like:

if (super.position.x >= (position.x + 0.01f)) {
			super.position.x = position.x + offsetX;
		}
		if (super.position.x <= (position.x - 0.01f)) {
			super.position.x = position.x - offsetX;
		}
		if (super.position.y > (position.y + 0.01f)) {
			super.position.y = position.y + offsetY;
		}
		if (super.position.y < position.y - 0.01f) {
			super.position.y = position.y - offsetY;
		}

But if the camera was to shake and then recentre on the vector, it would snap into position, unlikes this other method which smooths it back into place :

if (shakeEnabled) {
			if (shaking) {
				processShake(delta);
			} else {
				if (position.x > viewportWidth / 2) {
					position.set(position.x - (viewportWidth / 2) * delta
							/ 1.5f, position.y, 0);
				}
				if (position.x < viewportWidth - viewportWidth / 2) {
					position.set(position.x + (viewportWidth / 2) * delta
							/ 1.5f, position.y, 0);
				}
				if (position.y > viewportHeight / 2) {
					position.set(position.x, position.y - (viewportHeight / 2)
							* delta / 1.5f, 0);
				}
				if (position.y < viewportHeight - viewportHeight / 2) {
					position.set(position.x, position.y + (viewportHeight / 2)
							* delta / 1.5f, 0);
				}

			}

		} else {
			position.set(viewportWidth / 2, viewportHeight / 2, z1);
			System.out.println("Here");
		}

However the above code is just a basic camera position, never moves and in the middle of screen, so it’s a much easier to do.

Any idea how I can get around this? I know for a fact it is going to be simple.

I am not sure if I understand your question correctly…but why don’t you just lerp to the target position?

float smoothing = 0.5f; // lower the smoother 
super.position.lerp(position, smoothing);

I had no idea that function existed…will try that

I shall try to clarify my question a little better.

I basically have my own camera class that has some built in functions, follow, centre, shake etc.
The follow method does not follow the given vector smoothly, it is all jittery. lol, as simple as I can put it.

Heh, fixed.

Sorry if you still don’t understand my question but anyway, you saved me a whole bunch of code lol.

Glad to have at least taught you about the lerp function; it’s very useful!

Yep! Now that I have found it, I can do some cool camera effects :smiley: Well kinda lol.