I’m attempting to implement a camera that follows a target (the player), and specifically one that is not fixed absolutely to the target, but rather that follows the target’s position smoothly over time.
To some degree I have this working. However, I keep encountering terrible jitter when the camera is near to the target, and perhaps at relatively-low frame-rates (~45fps). And thus far I have not managed to entirely get rid of it. (At least not without incurring some other issue.)
I’ve tried a variety of things–rearranging the order of operations (which did help a bit, I think), clamping the camera’s movement so that it doesn’t overshoot, re-working it to use a velocity-vector, or re-working it to draw from a set of samples over multiple rather than a single value. Thus far to little avail, I fear.
Since the actual code in question is a little complicated (involving multiple files and classes), let me describe my current approach to this in partial pseudocode:
(I’m doing so in part off the top of my head, and so may be mistaken in some of the details. However, I believe the below to be at least broadly accurate.)
updateTask = taskMgr.add(updateMethod, "update")
def updateMethod:
dt = globalClock.getDt()
updatePlayer(dt)
return task.cont
def updatePlayer(dt):
position += velocity * dt
updateCamera(dt)
def updateCamera(dt):
diff = targetPos - cameraPos
scalar = cameraSpeed * dt
if scalar > 1:
scalar = 1
cameraPos += diff * scalar
Does anyone see a problem with my approach here? Or have suggestions as to something that might fix the issue that I’m seeing?