Slightly Buggy Camera Script

EDIT: SOLVED! (see https://blenderartists.org/forum/showthread.php?446116-Possibly-Buggy-Function)

I am using a modified version of this third-person template by laser blaster in my game:

I have found that the camera script (“Camera.py”) has a bug that, when the camera is manually controlled, causes crazy jittering while certain conditions are met. Below are some steps to recreate the bug:

  1. Open the .blend file in the template
  2. Duplicate one of the ground platforms and make it very large (or just make a new one; this step is only for making the glitch easier to see)
  3. Start the game, and press the middle mouse button to activate manual camera control
  4. Go to your large platform and have the character run directly towards the camera (if using wasd-mouse, just hold s key)
  5. jerk the mouse/camera quickly left or right, after a few moments you’ll notice the camera jitter like crazy

Another glitch (or perhaps a symptom of the same glitch) is caused by setting the default camera control to manual (set “auto = False” in “Input.py”)
Then, after playing the game once and pressing esc to end the game, press p to play again WITHOUT MOVING THE MOUSE BEFOREHAND (mouse pointer starts at exactly screen center) to cause the camera to rapidly spin around the player until the mouse is moved.

I have spent several days trying to fix this bug myself; I am good enough at python to be able to edit most of the other scripts without issue but I have not been able to figure out the camera script. If someone could please take a look at the script and its associated functions and help figure out this bug, I would greatly appreciate it!

Just one quick clarification: in Step 5, keep running towards the camera during and after jerking the mouse to trigger the glitch

So, the crazy spinning when restarting the game is a bug from the BGE itself.
My understanding is that while the mouse is not moving, the mouse position is not updated until an event appears…

Using the exact same files, the bug is not happening on UPBGE (up-to-date BGE).

Still using UPBGE, could not reproduce the camera jitter (I could do it on standard-BGE).
But the physics seem broken on UPBGE, so its hard to test.

I don’t really understand why the camera jitters.
There’s some kind of camera orientation offset stack going on, maybe the problem comes somewhere around there, I just lack the motivation to debug this lol.

I think the scripts could take a good refactoring if you want to use it, but its up to you.

Thanks for the quick reply. I took a look at the offset stack, but it turns out it’s only used for camera position, not orientation (though the jittering is always delayed by several frames, so this could still have something to do with the problem). I’m still looking into the portion of code that sets orientation, as well as the mouse input code.

As far as UPBGE, the python definitely breaks completely on it. I’m already too invested into my modified version of the template to switch now (I already have attacking, dodging, taking/dealing damage, working enemies, camera targeting, etc. implemented). I suppose this camera issue is the one remaining bug I have to purge before I can release a demo of my game :frowning:

Anyway, thanks again for your help. If you or anyone else wants to help debug, I would still greatly appreciate any helpful feedback!