Chapter 9 - Diagonal Camera Movement on a Mouse is Jittery

After completing Chapter 9’s challenge to complete a PlayerCamera, I noticed that when using the mouse to pan the camera in a diagonal direction, the camera is very jittery. It is smooth when I move the mouse along one axis, or when I use keyboard input for diagonal movement.

Is this expected? And if so, how can we make it smooth? I’m guessing most game engines must have some way to smooth out mouse input to ensure smooth camera movement in any direction, but I can’t find any resources on this particular topic.

Thanks!

Hi @dersan and welcome to the forum :slightly_smiling_face: !

Mine is a bit jittery, but I wouldn’t call it “very”.

In InputController.swift, I commented out:

center.addObserver(
  forName: .GCMouseDidConnect,
  object: nil,
  queue: nil) { notification in
    let mouse = notification.object as? GCMouse
    mouse?.mouseInput?.leftButton.pressedChangedHandler = { _, _, pressed in
      self.leftMouseDown = pressed
    }
    mouse?.mouseInput?.mouseMovedHandler = { _, deltaX, deltaY in
      self.mouseDelta = Point(x: deltaX, y: deltaY)
    }
    mouse?.mouseInput?.scroll.valueChangedHandler = { _, xValue, yValue in
      self.mouseScroll.x = xValue
      self.mouseScroll.y = yValue
    }
}

and it seemed to be less jittery.

Embarrassingly, I can’t for the moment remember why that code is there, because I wrote it several years ago. Maybe it was needed at the time. Mouse code was always rather difficult because Apple didn’t want to recognise my Logitech mouse, and I was determined that it should.

Taking that out works both on macOS and iPadOS with the mouse.

Hey Caroline, thanks for the warm welcome and for providing the only real book on game engines/rendering in Metal.

Hmm, maybe that change makes more sense in the upcoming edition, but in my edition that code is the low-level GCMouse code that detects mouse input. So, if I comment that out I don’t get any mouse input at all.

It is true that mouse input in any game isn’t perfectly smooth when panning in a direction, because you’re not going to move your arm at a perfectly constant speed. So, I played around some more and was able to improve the issue by adjusting the sensitivity values and making one other change to the GCMouse code:

            mouse?.mouseInput?.mouseMovedHandler = { _, deltaX, deltaY in
                self.mouseDelta.x += deltaX
                self.mouseDelta.y += deltaY
            }

So, instead of replacing the point each time input is detected we accumulate the deltas. I think this change makes sense because you may receive a lot more than one mouse input deltas between frames.

What do you think?

Yes, using the delta is a good way of handling it :clap: .

However, it didn’t make any difference on my system, but if it works for you, that’s great!