Skip to content

Instantly share code, notes, and snippets.

@wezu
Last active May 9, 2019 17:26
Show Gist options
  • Save wezu/48039b6498eb5902e57893726daa17d8 to your computer and use it in GitHub Desktop.
Save wezu/48039b6498eb5902e57893726daa17d8 to your computer and use it in GitHub Desktop.
#Panda3D input polling interface idea
# When a keyboard/controller/mouse button is pressed/released/moved
# the values in base.inputs is updated.
# The keys are {device_name}-{button_name}
# The values for buttons are 1 or 0
forward = 'keyboard-w'
if base.inputs[forward]:
self.pc.set_y(self.pc, dt*move_speed)
# if the 'button' is an analog axis, it's value in 0.0-1.0 is returned
# each axis has 3 events/buttons(?), one for positive, one for negative values
# and one for a -1.0 +1.0 range
# this way the same code will work for both analog axis and digital buttons
forward = 'gamepad-x_axis_positive'
back = 'gamepad-x_axis_negative'
if base.inputs[forward]:
move_speed *= base.inputs[forward]
elif base.inputs[back]:
move_speed *= -base.inputs[back]
else:
move_speed *= 0.0
self.pc.set_y(self.pc, dt*move_speed)
# simpler, all in one, **but analog only**:
move_axis = 'gamepad-x_axis' # -1.0, +1.0 value range
if base.inputs[move_axis]:
move_speed *= base.inputs[move_axis]
self.pc.set_y(self.pc, dt*move_speed)
# if the 'button' is the mouse
# one can use 'x_delta'/'y_delta' or 'x_delta_positive'
# to check how far the cursor moved since last frame
# again, using the same logic
forward = 'mouse-x_delta_positive'
if base.inputs[forward]:
move_speed *= base.inputs[forward]
self.pc.set_y(self.pc, dt*move_speed)
# the position of the mouse cursor should act like an axis of a gamepad/joystick
# but there also needs to be a way to detect if the mouse cursor is in the window
if base.inputs['mouse-x'] is not None:
self.picker_ray.set_from_lens(self.camNode, base.inputs['mouse-x'], base.inputs['mouse-y'])
# The values can be compound using '&' (and)
# For example: move diagonally when A and W are pressed at the same time
if base.inputs['keyboard-w&keyboard-a']:
self.pc.set_pos(self.pc, Point3(dt*move_speed, dt*move_speed, 0))
# The values can be compound using '|' (or)
# Use case - alternative binds
forward = 'keyboard-w|gamepad-x_axis_positive'
if base.inputs[forward]:
move_speed *= base.inputs[forward]
self.pc.set_y(self.pc, dt*move_speed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment