Skip to content

Instantly share code, notes, and snippets.

@kergalym
Created March 25, 2022 18:00
Show Gist options
  • Save kergalym/b745aca5f81f628173391d3b59199aa6 to your computer and use it in GitHub Desktop.
Save kergalym/b745aca5f81f628173391d3b59199aa6 to your computer and use it in GitHub Desktop.
TPS Mouse Camera Code Fragment (Panda3D-based game)
def on_mouse_rotate_in_aiming(self, x, y):
""" Function : on_mouse_rotate_in_aiming
Description : Activates player rotation by mouse in aiming.
Input : Integer
Output : None
Return : None
"""
# world heading in aiming
player_bs = self.base.get_actor_bullet_shape_node(asset="Player",
type="Player")
if player_bs:
heading = player_bs.get_h() - (x - int(base.win.get_x_size() / 2)) * self.mouse_sens
pitch = self.floater.get_p() - (y - int(base.win.get_y_size() / 2)) * self.mouse_sens
player_bs.set_h(heading)
if self.base.game_instance['player_ref'].get_python_tag("is_on_horse"):
self.pivot.set_h(-160)
else:
self.pivot.set_h(110)
if not pitch > 10.0 and not pitch < -50.0:
self.floater.set_p(pitch)
def on_mouse_look_around_player(self, x, y):
""" Function : on_mouse_look_around_player
Description : Activates camera rotation by mouse.
Input : Integer
Output : None
Return : None
"""
if (not self.base.game_instance["kbd_np"].keymap["forward"]
or not self.base.game_instance["kbd_np"].keymap["backward"]):
# reset heading and pitch for floater
self.floater.set_h(0)
self.floater.set_p(0)
# apply heading and pitch
heading = self.pivot.get_h() - (x - int(base.win.get_x_size() / 2)) * self.mouse_sens
pitch = self.pivot.get_p() - (y - int(base.win.get_y_size() / 2)) * self.mouse_sens
self.pivot.set_h(heading)
if not pitch > 10.0 and not pitch < -50.0:
self.pivot.set_p(pitch)
def on_mouse_rotate_player(self, x, y):
""" Function : on_mouse_rotate_player
Description : Activates player rotation by mouse.
Input : Integer
Output : None
Return : None
"""
if (self.base.game_instance["kbd_np"].keymap["forward"]
or self.base.game_instance["kbd_np"].keymap["backward"]):
player_bs = self.base.get_actor_bullet_shape_node(asset="Player",
type="Player")
if player_bs:
# apply heading and pitch
heading = player_bs.get_h() - (x - int(base.win.get_x_size() / 2)) * self.mouse_sens
pitch = player_bs.get_p() - (y - int(base.win.get_y_size() / 2)) * self.mouse_sens
player_bs.set_h(heading)
# Show player only from back
self.pivot.set_h(180)
if not pitch > 10.0 and not pitch < -50.0:
player_bs.set_p(pitch)
def mouse_control(self):
""" Function : mouse_control
Description : Mouse rotation controlling logic for 3rd person view.
Input : None
Output : None
Return : None
"""
if self.base.game_instance['mouse_control_is_activated'] == 0:
self.base.game_instance['mouse_control_is_activated'] = 1
if self.game_settings['Debug']['set_editor_mode'] == 'NO':
# TPS Logic
if self.pivot:
mouse_direction = base.win.getPointer(0)
x = mouse_direction.get_x()
y = mouse_direction.get_y()
# Recentering the cursor and do mouse look
if base.win.move_pointer(0, int(base.win.get_x_size() / 2), int(base.win.get_y_size() / 2)):
if not self.base.game_instance['is_aiming']:
self.on_mouse_look_around_player(x, y)
self.on_mouse_rotate_player(x, y)
elif self.base.game_instance['is_aiming']:
self.on_mouse_rotate_in_aiming(x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment