Positions > Vector#
API Reference#
- class miniworlds.positions.vector.Vector(x, y)[source]#
Represents a 2D vector for use in movement, geometry, and physics.
Supports arithmetic with both other Vectors and 2D tuples.
Examples
>>> v = Vector(1, 2) >>> v2 = Vector(3, 4) >>> v + v2 Vector(4.0, 6.0) >>> v + (1, 1) Vector(2.0, 3.0) >>> (1, 1) + v Vector(2.0, 3.0)
Public Data Attributes:
Public Methods:
__init__(x, y)Creates a 2D vector.
__getitem__(index)Converts the vector to a plain
(x, y)tuple.from_position(position)Creates a Vector from a 2-tuple position.
from_positions(p1, p2)Creates a Vector pointing from p1 to p2.
from_direction(direction)Creates a unit vector from a miniworlds direction value.
from_actors(t1, t2)Creates a vector from one actor to another actor.
from_actor_and_position(t1, pos)Creates a vector from an actor center to a target position.
from_actor_direction(actor)Creates a vector from actor direction
rotate(theta)Rotates the vector in-place by theta degrees counter-clockwise.
Converts the vector to a miniworlds direction value.
Normalizes the vector to length 1 in-place.
length()Returns the Euclidean length (magnitude) of the vector.
limit(max_length)Caps the vector's length to max_length without changing its direction.
multiply(other)Multiplies the vector by a scalar or computes the dot product with another vector.
add_to_position(position)Adds the vector to a position tuple and returns the result.
Returns a vector perpendicular (normal) to this one.
dot(other)Computes the dot product of this vector and other.
distance_to(other)Calculates Euclidean distance to another vector or position.
angle_to(other)Computes the angle between this vector and another in degrees.
__add__(other)__radd__(other)__sub__(other)__rsub__(other)__mul__(other)__rmul__(other)__neg__()__eq__(other)Return self==value.
__str__()Return str(self).
__repr__()Return repr(self).
Private Methods:
_to_vector(value)
- __init__(x, y)[source]#
Creates a 2D vector.
- Parameters:
x – Horizontal component.
y – Vertical component.
- property angle: float#
Direction angle of the vector in miniworlds convention (0° = up, 90° = right).
Equivalent to calling
to_direction().
- angle_to(other)[source]#
Computes the angle between this vector and another in degrees.
- Return type:
- Parameters:
other – A Vector or tuple.
- Returns:
Angle in degrees between 0 and 180.
Examples
>>> Vector(1, 0).angle_to((0, 1)) 90.0
- distance_to(other)[source]#
Calculates Euclidean distance to another vector or position.
- Return type:
- Parameters:
other – A Vector or tuple.
- Returns:
The distance as float.
Examples
>>> Vector(0, 0).distance_to((3, 4)) 5.0
- dot(other)[source]#
Computes the dot product of this vector and other.
- Return type:
- Parameters:
other – The other Vector.
- Returns:
Dot product as a float.
- classmethod from_actor_and_position(t1, pos)[source]#
Creates 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.
- classmethod from_actor_direction(actor)[source]#
Creates a vector from actor direction
Examples: :rtype: Vector
Creates rotating rectangle
from miniworlds import * world = World() player = Rectangle((200,200),40, 40) player.speed = 1 player.direction = 80 @player.register def act(self): v1 = Vector.from_actor_direction(self) v1.rotate(-1) self.direction = v1 world.run()
- classmethod from_actors(t1, t2)[source]#
Creates 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) print(enemy_vector.length())
- classmethod from_direction(direction)[source]#
Creates a unit vector from a miniworlds direction value.
- Return type:
- Parameters:
direction – Direction in miniworlds convention (0 = up, 90 = right, -90 = left, 180 = down).
- Returns:
A new unit Vector pointing in the given direction.
- classmethod from_position(position)[source]#
Creates a Vector from a 2-tuple position.
- Return type:
- Parameters:
position – A
(x, y)tuple.- Returns:
A new Vector with the same x and y components.
- classmethod from_positions(p1, p2)[source]#
Creates a Vector pointing from p1 to p2.
- Return type:
- Parameters:
p1 – Start position as
(x, y).p2 – End position as
(x, y).
- Returns:
Vector equal to
p2 - p1.
- get_normal()[source]#
Returns a vector perpendicular (normal) to this one.
- Return type:
- Returns:
A new Vector rotated 90° counter-clockwise.
- length()[source]#
Returns the Euclidean length (magnitude) of the vector.
- Return type:
- Returns:
Length as a float.
- limit(max_length)[source]#
Caps the vector’s length to max_length without changing its direction.
- Return type:
- Parameters:
max_length – Maximum allowed length.
- Returns:
The vector itself.
- multiply(other)[source]#
Multiplies the vector by a scalar or computes the dot product with another vector.
- normalize()[source]#
Normalizes the vector to length 1 in-place.
- Return type:
- Returns:
The vector itself. A zero-length vector is returned unchanged.
- rotate(theta)[source]#
Rotates the vector in-place by theta degrees counter-clockwise.
- Return type:
- Parameters:
theta – Rotation angle in degrees.
- Returns:
The vector itself (mutated in-place).
- to_direction()[source]#
Converts the vector to a miniworlds direction value.
- Return type:
- Returns:
Direction as a float in miniworlds convention (0° = up, 90° = right, 180° = down, -90° = left). Returns 0 for a zero-length vector.