Orthographic camera following a game object without delay

I’d like the camera to follow a fast moving game object with no lerp and no visible movement. Is this possible?

What I’ve tried so far:

  • Orthographic camera.follow(): The game object moves visibly and sometimes jitters.
  • Manually moving the camera to match the game object position: Similar if not the same to camera.follow().
  • Reparent the camera inside the moving game object: I’ve seen this suggested on the forum, but for me it doesn’t work.
  • Late physics update hack with physics.raycast_async(): Like manually moving the camera, the game object visibly moves.

Is there a way to instantly update the camera, to avoid the lag seen in the video?

Minimal project:
CameraFollow.zip (3.4 KB)

Usecase :star_struck::

4 Likes

Using the default render script with a camera set orthographic inside the same game object as the sprite works as you intend. You have to offset the sprite in order for it not to be in the corner.

1 Like

Instead of moving the player through the world, you could move the world around the player. The camera needn’t synchronize with the player object at all, then.

1 Like

Interesting! Will have to experiment with this. Any idea why it doesn’t work with the Orthographic camera extension?

I was considering this, but what complicated it is the physics and that the end result will be a realtime multiplayer game with many game objects whizzing about. I haven’t quite figured out how to handle all that with the player effectively stationary.

I have not looked into exactly why, but I presume that it is because the sprite is updated in the go property and orthographic being updated in script updates. The go animate property works separately from update functions so depending on the life cycle, there will be some jitter due to LUA being behind the internal go animate. Orthographic is great extension but given the new camera functions of Defold it is a bit outdated. The functions you need from orthographic could easily be implemented into the current camera component, such as shake and following.

1 Like

Brilliant. This is the way. Thanks!

1 Like

I have plans to switch to a normal camera component, but I have not had time to do so!

3 Likes

Just a followup on this: Using the new camera component and moving it inside the moving game object works perfectly. No stutter, utterly smooth, always. To me, this is major! :partying_face:

4 Likes

Attaching the camera to a game object controlled by a dynamic collision object looks like this:

CameraFollow.zip (4.4 KB)

Is there a way to get a smooth camera in this case?

You have enabled use_fixed_timestep for physics, so for all physical objects and for the camera you need to add yourself interpolation of their position and rotation in time. In short, using vmath.lerp/vmath.slerp.

My workaround, if the gameplay of the game allows, i.e. it does not require a strict constistency in the application of forces: is to disable fixed timestep for physics, but set engine.max_time_step = 0.05 (i.e. 20 fps) or less. And limit max fps to 90-120 via display.update_frequency. So that objects don’t fall through if the game slows down at the player or the window loses focus (in a web game). Suitable for 2D only, as Box2D handles floating dt perfectly.

4 Likes

This seems to work, thanks!

Update 1: Now to the tricky part! :slight_smile:

Adding the camera to a game object controlled by a dynamic collision object means the position is perfect, but so is rotation.

Is there a way to prevent the camera from inheriting the rotation? Doing so manually works, but results in jitters:

CameraFollow.zip (4.5 KB)

Update 2: Solved! Attaching a spring joint to the rotating body, and reparenting the camera to that instead, works.

CameraFollow.zip (4.5 KB)

1 Like