Skip to content

Fixed jitter while walking and rotating at the same time. #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down