From e1e359fe51a8f31eb8275c432fa56cd4e2d34f43 Mon Sep 17 00:00:00 2001 From: Max Hanson <29157840+mmhanson@users.noreply.github.com> Date: Tue, 1 Jun 2021 15:52:32 -0600 Subject: [PATCH] Update main.py Made rotation smoother by buffering changes in rotation made by the on_mouse_motion callback, and adding them later in the update method. Updating the position and rotation at the same time removes the jittery which appears while walking and moving your sight simultaneously. --- main.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 6332d97a..b80fd839 100644 --- a/main.py +++ b/main.py @@ -462,6 +462,12 @@ def __init__(self, *args, **kwargs): # 90 (looking straight up). The horizontal rotation range is unbounded. self.rotation = (0, 0) + # Changes to player rotation is stored here by the on_mouse_motion + # callback and the player rotation is updated in the update method + # at the same time as the position. Without this, rotation is jittery + # while moving and rotating simultaneously + self.rotation_buf = (0, 0) + # Which sector the player is currently in. self.sector = None @@ -590,6 +596,11 @@ def _update(self, dt): The change in time since the last call. """ + # rotation + x, y = self.rotation + dx, dy = self.rotation_buf + self.rotation = (x+dx, y+dy) + self.rotation_buf = (0, 0) # walking speed = FLYING_SPEED if self.flying else WALKING_SPEED d = dt * speed # distance covered this tick. @@ -701,10 +712,10 @@ def on_mouse_motion(self, x, y, dx, dy): """ if self.exclusive: m = 0.15 - x, y = self.rotation - x, y = x + dx * m, y + dy * m + x, y = dx * m, dy * m y = max(-90, min(90, y)) - self.rotation = (x, y) + old_x, old_y = self.rotation_buf + self.rotation_buf = (old_x+x, old_y+y) def on_key_press(self, symbol, modifiers): """ Called when the player presses a key. See pyglet docs for key