Pygame - first-person shooter "looks" with the mouse

I am not writing a game, but a scientific renderer using Pygame. I would like the controls to work the same way as in a first-person shooter so that the user can navigate using a familiar set of controls.

I tried to write code to have the same properties as the watch function, for example. Skyrim or Half-Life, but the mouse does not move the cursor - it allows you to look around and around, in endless circles. A click should not have an effect.

First attempt for controls:

(code inside the game loop)

delta_y, delta_x = pygame.mouse.get_rel() rotation_direction.x = float(delta_x) rotation_direction.y = float(delta_y) 

(don’t ask me why, but y and x must be changed in such a way as to get the expected viewing direction, there must be something to do with the camera conversion implementation, which is not mine .)

This, however, leads to the fact that the cursor is on top of the window, and when the cursor reaches the edge of the screen, the window stops rotating; that is, the code reports the actual position on the screen.

I tried to “reset” the mouse position in each game loop (and by the way hide the mouse):

 pygame.mouse.set_pos([150, 150]) pygame.mouse.set_visible(False) 

But this generates a symmetrical “back to the beginning” movement in the next cycle, which means that you cannot “look” anywhere.

To summarize, I want:

  • detection of actual mouse movement reported from the device
  • do not move / show any OS cursor
  • not clip on the edge of the screen

What is the best way to do this using Pygame or other Python hacks?

+6
source share
3 answers

http://www.pygame.org/docs/ref/mouse.html :

If the mouse cursor is hidden and the input is tied to the current screen , the mouse will enter virtual input mode, where relative mouse movements will never be stopped by the borders of the screen. See Functions pygame.mouse.set_visible - hide or show the mouse cursor and pygame.event.set_grab ( docs ) - control the sharing of input devices with other applications to configure this.

+5
source

Try calling pygame.mouse.get_rel() again immediately after calling set_pos to “throw set_pos ” any relative movement made by calling set_pos .

0
source

Since you are using pyOpenGL try gluLookAt() example: How to use gluLookAt correctly?

0
source

Source: https://habr.com/ru/post/926066/


All Articles