📜  pyrr (1)

📅  最后修改于: 2023-12-03 15:33:55.606000             🧑  作者: Mango

Pyrr

Pyrr is a Python library for working with 3D math, including vectors, matrices, quaternions, and transformations.

Features
  • 3D vector & matrix math
  • Quaternion math
  • Common transformations:
    • Translation
    • Scaling
    • Rotation
  • GLSL-style matrix functions
  • Axis-angle & Euler angle representations
  • Inverse & transpose matrix functions
Usage

First, install pyrr using pip:

pip install pyrr

In your Python code, start by importing the relevant classes/functions:

import numpy as np
from pyrr import Vector3, Matrix44, Quaternion
Vectors

Create a 3D vector:

v = Vector3([1.0, 2.0, 3.0])

Access individual components:

x, y, z = v

Arithmetic operations:

v += [1.0, 1.0, 1.0]
v *= 2.0

Dot product:

dot_product = np.dot(v1, v2)

Cross product:

cross_product = np.cross(v1, v2)
Matrices

Create a 4x4 matrix:

m = Matrix44.from_scale([2.0, 2.0, 2.0])

Matrix multiplication:

result = np.dot(matrix1, matrix2)

Transpose matrix:

transpose_matrix = np.transpose(m)

Inverse matrix:

inverse_matrix = np.linalg.inv(m)
Quaternions

Create a quaternion:

q = Quaternion.from_axis_rotation([1.0, 0.0, 0.0], 90.0)

Quaternion multiplication:

result = q1 * q2

Convert to matrix:

m = q.get_matrix()
Transformations

Apply translation, scaling, and rotation to a matrix:

matrix = Matrix44.from_translation([1.0, 2.0, 3.0])
matrix *= Matrix44.from_scale([2.0, 2.0, 2.0])
matrix *= Matrix44.from_quaternion(q)
GLSL-style functions
matrix = Matrix44.identity()
matrix = Matrix44.translate(matrix, [1.0, 2.0, 3.0])
matrix = Matrix44.scale(matrix, [2.0, 2.0, 2.0])
matrix = Matrix44.rotate(matrix, np.pi / 4, [0.0, 1.0, 0.0])
Conclusion

Pyrr is a powerful Python library for working with 3D math in computer graphics and game development. Its rich features and easy-to-use API make it a great tool for programmers.