Positions > Vector#

API Reference#

class miniworlds.positions.vector.Vector(x, y)[source]#

2D vector for movement, geometry, and physics.

Supports arithmetic with both other Vectors and 2D tuples.

Examples

direction = Vector.from_actors(enemy, player)
enemy.move_vector(direction.normalize() * 2)
__init__(x, y)[source]#

Create a 2D vector.

Parameters:
  • x – Horizontal component.

  • y – Vertical component.

add_to_position(position)[source]#

Add the vector to a position.

Return type:

Tuple[float, float]

Parameters:

position – Position as (x, y).

Returns:

New position as (x + self.x, y + self.y).

property angle: float#

Direction angle in Miniworlds convention.

0 points up and 90 points right. Equivalent to to_direction().

Type:

float

angle_to(other)[source]#

Compute the angle to another vector or position.

Return type:

float

Parameters:

other – A Vector or tuple.

Returns:

Angle in degrees between 0 and 180.

Examples

angle = Vector(1, 0).angle_to((0, 1))
distance_to(other)[source]#

Calculate the Euclidean distance to another vector or position.

Return type:

float

Parameters:

other – A Vector or tuple.

Returns:

The distance as float.

Examples

distance = Vector(0, 0).distance_to((3, 4))
dot(other)[source]#

Compute the dot product.

Return type:

float

Parameters:

other – The other Vector.

Returns:

Dot product as a float.

classmethod from_actor_and_position(t1, pos)[source]#

Create a vector from an actor center to a target position.

Return type:

Vector

Parameters:
  • t1 – The start actor.

  • pos – Target position as (x, y).

Returns:

A vector equal to pos - t1.center.

Examples

vector = Vector.from_actor_and_position(player, (100, 80))
classmethod from_actor_direction(actor)[source]#

Create a unit vector from an actor direction.

Return type:

Vector

Parameters:

actor – Actor whose direction is used.

Returns:

A unit vector pointing in the actor direction.

Examples

step = Vector.from_actor_direction(player) * 5
player.move_vector(step)
classmethod from_actors(t1, t2)[source]#

Create a vector from one actor to another actor.

Return type:

Vector

Parameters:
  • t1 – Start actor.

  • t2 – Target actor.

Returns:

A vector equal to t2.center - t1.center.

Examples

enemy_vector = Vector.from_actors(player, enemy)
if enemy_vector.length() < 50:
    player.move_away(enemy, 3)
classmethod from_direction(direction)[source]#

Create a unit vector from a Miniworlds direction.

Return type:

Vector

Parameters:

direction – Direction in Miniworlds convention. Common values are 0 or “up”, 90 or “right”, -90 or “left”, and 180 or “down”.

Returns:

A new unit Vector pointing in the given direction.

Examples

@player.register
def on_key_pressed_right(self):
    step = Vector.from_direction("right") * 5
    self.move_vector(step)
classmethod from_position(position)[source]#

Create a vector from a position.

Return type:

Vector

Parameters:

position – Position as (x, y).

Returns:

A new Vector with the same x and y components.

Examples

vector = Vector.from_position(actor.position)
classmethod from_positions(p1, p2)[source]#

Create a vector pointing from p1 to p2.

Return type:

Vector

Parameters:
  • p1 – Start position as (x, y).

  • p2 – End position as (x, y).

Returns:

Vector equal to p2 - p1.

get_normal()[source]#

Return a vector perpendicular to this one.

Return type:

Vector

Returns:

A new Vector rotated 90° counter-clockwise.

length()[source]#

Return the Euclidean length of the vector.

Return type:

float

Returns:

Length as a float.

limit(max_length)[source]#

Cap the vector length without changing its direction.

Return type:

Vector

Parameters:

max_length – Maximum allowed length.

Returns:

The vector itself.

Examples

velocity.limit(10)
multiply(other)[source]#

Multiply by a scalar or compute a dot product.

Return type:

Union[Vector, float]

Parameters:

other – Scalar value or another Vector.

Returns:

A scaled Vector for scalar input, or a dot product for vectors.

normalize()[source]#

Normalize the vector to length 1 in-place.

Return type:

Vector

Returns:

The vector itself. A zero-length vector is returned unchanged.

rotate(theta)[source]#

Rotate the vector in-place.

Return type:

Vector

Parameters:

theta – Rotation angle in degrees.

Returns:

The vector itself.

Examples

vector.rotate(90)
to_direction()[source]#

Convert the vector to a Miniworlds direction.

Return type:

float

Returns:

Direction in degrees. Returns 0 for a zero-length vector.

to_position()[source]#

Return the vector as an (x, y) tuple.

Return type:

Tuple[float, float]

property x: float#

The x-component of the vector.

property y: float#

The y-component of the vector.