core.physics package

Submodules

core.physics.vector module

Two-dimensional vectors in Forge.

class core.physics.vector.Vector2D(x: float, y: float)

Bases: object

Forge’s representation of a two-dimensional vector.

as_tuple() → tuple[float, float]

Return the x and y components of the vector in a tuple. Beneficial for internal interoperability with Pygame.

Returns

Tuple of the vector’s x and y component respectively.

Return type

tuple[float, float]

is_normalized(precision: int = 4) → bool

Check whether the vector is normalized, i.e., its length is equal to one.

Parameters

precision (int) – Precision to which the length of the vector is to be checked to keep a delta for floating-point inaccuracies; defaults to 4.

Returns

True if the length pr magnitude of the vector is equal to one; else False.

Return type

bool

length() → float

Compute the length of the vector.

Returns

Length or magnitude of the vector.

Return type

float

length_squared() → float

Compute the square of the length of the vector. Faster due to lack of a square root operation.

Returns

Square of the length or magnitude of the vector.

Return type

float

normalize() → None

Normalize the vector, i.e., set its length to one while maintaining the same direction.

Raises

ZeroDivisionError – A vector of zero length cannot be normalized.

scale(other: Vector2D) → None

Scale the vector by another vector’s corresponding components.

Parameters

other (Vector2D) – Scaling factor vector.

x: float
y: float
core.physics.vector.angle(from_vector: Vector2D, to_vector: Vector2D) → float

Calculate the unsigned angle between one vector and another. The unsigned angle means that the direction of rotation becomes irrelevant.

Parameters
  • from_vector (Vector2D) – Initial vector from which the angle is measured.

  • to_vector (Vector2D) – Final vector from which the angle is stopped being measured.

Returns

Unsigned angle between the two vectors in radians.

Return type

float

core.physics.vector.clamp(vector: Vector2D, min_: Vector2D, max_: Vector2D) → Vector2D

Clamp a vector between a specified minimum and maximum bound.

Parameters
  • vector (Vector2D) – Vector to be clamped.

  • min (Vector2D) – Minimum bound of the clamp.

  • max (Vector2D) – Maximum bound of the clamp.

Returns

Vector clamped to the minimum and maximum bound.

Return type

Vector2D

Raises

ValueError – The maximum bound cannot be greater than the minimum bound on either component axis.

core.physics.vector.cross(vector1: Vector2D, vector2: Vector2D) → float

Compute the cross product of two vectors.

Parameters
Returns

Magnitude of the cross or vector product of the two vectors.

Return type

float

core.physics.vector.distance_between(vector1: Vector2D, vector2: Vector2D) → float

Compute the distance between two vectors.

Parameters
Returns

Distance between the two vectors.

Return type

float

core.physics.vector.distance_squared_between(vector1: Vector2D, vector2: Vector2D) → float

Compute the square of the distance between two vectors. Faster due to lack of a square root operation.

Parameters
Returns

Square of the distance between the two vectors.

Return type

float

core.physics.vector.dot(vector1: Vector2D, vector2: Vector2D) → float

Compute the dot product of two vectors.

Parameters
Returns

Dot or scalar product of the two vectors.

Return type

float

core.physics.vector.down() → Vector2D

Create a new vector pointing in the down direction, i.e., x: 0, y: 1.

Returns

New down vector.

Return type

Vector2D

core.physics.vector.from_tuple(position: tuple[float, float]) → Vector2D

Create a new vector using an existing tuple of components. Beneficial for internal interoperability with Pygame.

Parameters

position (tuple[float, float]) – Tuple of the vector’s x and y component respectively.

Returns

Vector created from the tuple.

Return type

Vector2D

core.physics.vector.left() → Vector2D

Create a new vector pointing in the left direction, i.e., x: -1, y: 0.

Returns

New left vector.

Return type

Vector2D

core.physics.vector.lerp(from_vector: Vector2D, to_vector: Vector2D, t: float) → Vector2D

Compute a smooth linear interpolation of a vector along both axes with respect to a constraint.

Parameters
  • from_vector (Vector2D) – Initial vector for the interpolation.

  • to_vector (Vector2D) – Final vector for the interpolation.

  • t (float) – Interpolation parameter to smoothly go from the initial to final vector.

Returns

Linearly interpolated vector between the initial and final vectors.

Return type

Vector2D

core.physics.vector.normalized(vector: Vector2D) → Vector2D

Normalize a vector, i.e., set its length to one while maintaining the same direction and return it.

Parameters

vector (Vector2D) – Vector to be normalized.

Returns

Normalized vector of length or magnitude of one.

Return type

Vector2D

Raises

ZeroDivisionError – A vector of zero length cannot be normalized.

core.physics.vector.one() → Vector2D

Create a new one vector: i.e., x: 1, y: 1.

Returns

New one vector.

Return type

Vector2D

core.physics.vector.random() → Vector2D

Create a new vector pointing in a random direction, i.e. x: -1 | 0 | 1, y: -1 | 0 | 1.

Returns

New random vector.

Return type

Vector2D

core.physics.vector.reflect(direction: Vector2D, normal: Vector2D) → Vector2D

Reflect a vector along a given normal to the direction of the vector.

Parameters
  • direction (Vector2D) – Vector to be reflected.

  • normal (Vector2D) – Normal to the reflection.

Returns

Reflected vector with respect to the normal.

Return type

Vector2D

Raises

ValueError – The normal vector must be normalized, or have a length or magnitude of one.

core.physics.vector.right() → Vector2D

Create a new vector pointing in the right direction, i.e., x: 1, y: 0.

Returns

New right vector.

Return type

Vector2D

core.physics.vector.up() → Vector2D

Create a new vector pointing in the up direction, i.e., x: 0, y: -1.

Returns

New up vector.

Return type

Vector2D

core.physics.vector.zero() → Vector2D

Create a new zero vector, i.e., x: 0, y: 0.

Returns

New zero vector.

Return type

Vector2D

Module contents