KeyboardCamera

moderngl_window.scene.KeyboardCamera[source]

Camera controlled by mouse and keyboard. The class interacts with the key constants in the built in window types.

Creating a keyboard camera:

camera = KeyboardCamera(
    self.wnd.keys,
    fov=75.0,
    aspect_ratio=self.wnd.aspect_ratio,
    near=0.1,
    far=1000.0,
)

We can also interact with the belonging Projection3D instance.

# Update aspect ratio
camera.projection.update(aspect_ratio=1.0)

# Get projection matrix in bytes (f4)
camera.projection.tobytes()

Methods

KeyboardCamera.__init__(keys: ~moderngl_window.context.base.keys.BaseKeys, keymap: ~typing.Callable[[~moderngl_window.context.base.keys.BaseKeys], ~moderngl_window.utils.keymaps.KeyMap] = <function <lambda>>, fov=60.0, aspect_ratio=1.0, near=1.0, far=100.0)[source]

Initialize the camera

Parameters:

keys (BaseKeys) – The key constants for the current window type

Keyword Arguments:
  • keymap (KeyMapFactory) – The keymap to use. By default QWERTY.

  • fov (float) – Field of view

  • aspect_ratio (float) – Aspect ratio

  • near (float) – near plane

  • far (float) – far plane

KeyboardCamera.key_input(key, action, modifiers) None[source]

Process key inputs and move camera

Parameters:
  • key – The key

  • action – key action release/press

  • modifiers – key modifier states such as ctrl or shit

KeyboardCamera.set_position(x, y, z) None

Set the 3D position of the camera.

Parameters:
  • x (float) – x position

  • y (float) – y position

  • z (float) – z position

KeyboardCamera.set_rotation(yaw, pitch) None

Set the rotation of the camera.

Parameters:
  • yaw (float) – yaw rotation

  • pitch (float) – pitch rotation

KeyboardCamera.look_at(vec=None, pos=None) ndarray

Look at a specific point

Either vec or pos needs to be supplied.

Keyword Arguments:
  • vec (pyrr.Vector3) – position

  • pos (tuple/list) – list of tuple [x, y, x] / (x, y, x)

Returns:

Camera matrix

Return type:

numpy.ndarray

KeyboardCamera.move_left(activate) None[source]

The camera should be continiously moving to the left.

Parameters:

activate (bool) – Activate or deactivate this state

KeyboardCamera.move_right(activate) None[source]

The camera should be continiously moving to the right.

Parameters:

activate (bool) – Activate or deactivate this state

KeyboardCamera.move_forward(activate) None[source]

The camera should be continiously moving forward.

Parameters:

activate (bool) – Activate or deactivate this state

KeyboardCamera.move_backward(activate) None[source]

The camera should be continiously moving backwards.

Parameters:

activate (bool) – Activate or deactivate this state

KeyboardCamera.move_up(activate) None[source]

The camera should be continiously moving up.

Parameters:

activate (bool) – Activate or deactivate this state

KeyboardCamera.move_down(activate)[source]

The camera should be continiously moving down.

Parameters:

activate (bool) – Activate or deactivate this state

KeyboardCamera.move_state(direction, activate) None[source]

Set the camera position move state.

Parameters:
  • direction – What direction to update

  • activate – Start or stop moving in the direction

KeyboardCamera.rot_state(dx: int, dy: int) None[source]

Update the rotation of the camera.

This is done by passing in the relative mouse movement change on x and y (delta x, delta y).

In the past this method took the viewport position of the mouse. This does not work well when mouse exclusivity mode is enabled.

Parameters:
  • dx – Relative mouse position change on x

  • dy – Relative mouse position change on y

Attributes

KeyboardCamera.pitch

The current pitch angle.

Type:

float

KeyboardCamera.yaw

The current yaw angle.

Type:

float

KeyboardCamera.matrix

The current view matrix for the camera

Type:

numpy.ndarray

KeyboardCamera.mouse_sensitivity

Mouse sensitivity (rotation speed).

This property can also be set:

camera.mouse_sensitivity = 2.5
Type:

float

KeyboardCamera.velocity

The speed this camera move based on key inputs

The property can also be modified:

camera.velocity = 5.0
Type:

float

KeyboardCamera.projection

The 3D projection

Type:

Projection3D