21

I am making a game with LibGDX (Java).

I need the camera to follow a fast moving character. The easiest way to do it is to just write this:

this.getCamera().position.set(obj.x, obj.y, 0);

But, is there any algorithm to make this more smooth? Like when camera is not that strict, and is always a bit late: character goes quick right, camera follows with slight delay, or if you suddenly appeared somewhere far, camera doesn't teleport instantly but travels at a top speed to you when it comes closer it slows down a bit and finds you again.

Is there any libgdx libs that do that or anyone had this experience?

Matsemann
  • 21,083
  • 19
  • 56
  • 89
Avetis Zakharyan
  • 887
  • 2
  • 9
  • 20
  • "*But, is there any algorithm to make this more smooth?*" Have you tried any algorithms on your own, or done any Google searches for smooth camera interpolation or something? – Nicol Bolas Nov 05 '12 at 08:14
  • 1
    I tried it on my own and have several working examples, but I do not like result much, I was curiousif there can be something already figured out as it sounds like something any game dev will need, Regarding google search, I am not sure what to search, it brings tons of other stuff. – Avetis Zakharyan Nov 05 '12 at 13:48

2 Answers2

51

Try something simple like lerping a tenth of the distance. It works surprisingly well.

float lerp = 0.1f;
Vector3 position = this.getCamera().position;
position.x += (Obj.x - position.x) * lerp * deltaTime;
position.y += (Obj.y - position.y) * lerp * deltaTime;
Martin Rohwedder
  • 1,712
  • 4
  • 19
  • 34
R Hyde
  • 10,301
  • 1
  • 32
  • 28
6

Take a look at Aurelion Ribon's Java Universal Tween Engine. This performs interpolation and has several easing equations that I think would get you what you are looking for. It also has other advanced features like waypointing and chaining certain actions together for other interesting effects.

Your game logic could check to see if the character is moving quickly or has a step change in terms of position. In response to this, turn your current camera position over to the tween engine and let it take over -- smoothly zooming to the character's current position.

  • Just for the record (I realise this was a while ago) no you would not want to use the tween engine for this since the game character is always moving and prone to user controlled directional changes. The overhead of starting and killing tweens makes no sense. Simply use the lerp algorithm above in the upd(delta) call. That said the UTE uis excellent for other such transitions or even for moving the camera for things other than following the main game character. – RichieHH Jul 26 '15 at 14:38
  • I disagree. While I haven't done a timing analysis, tweens are pooled so the impact of starting and killing should be minimal. Assigning a new tween is simply a set of assignments and killing a tween is similarly just returning the object back to the pool. I've used this approach in a game to help smooth out the camera motion rather than locking directly on the character with no observable performance impact. Instead of jarring motions, the camera lazily follows the player which looks a lot better to the eye. –  Jul 29 '15 at 12:20