hump.vector-light

vector = require "hump.vector-light"

An table-free version of hump.vector. Instead of a vector type, hump.vector-light provides functions that operate on numbers.

Note

Using this module instead of hump.vector may result in faster code, but does so at the expense of speed of development and code readability. Unless you are absolutely sure that your code is significantly slowed down by hump.vector, I recommend using it instead.

Example:

function player:update(dt)
    local dx,dy = 0,0
    if love.keyboard.isDown('left') then
        dx = -1
    elseif love.keyboard.isDown('right') then
        dx =  1
    end
    if love.keyboard.isDown('up') then
        dy = -1
    elseif love.keyboard.isDown('down') then
        dy =  1
    end
    dx,dy = vector.normalize(dx, dy)

    player.velx, player.vely = vector.add(player.velx, player.vely,
                                    vector.mul(dy, dx, dy))

    if vector.len(player.velx, player.vely) > player.max_velocity then
        player.velx, player.vely = vector.mul(player.max_velocity,
                            vector.normalize(player.velx, player.vely)
    end

    player.x = player.x + dt * player.velx
    player.y = player.y + dt * player.vely
end

Function Reference

vector.str(x, y)
Arguments:
  • x,y (numbers) – The vector.
Returns:

The string representation.

Produce a human-readable string of the form (x,y). Useful for debugging.

Example:

print(vector.str(love.mouse.getPosition()))
vector.mul(s, x, y)
Arguments:
  • s (number) – A scalar.
  • x,y (numbers) – A vector.
Returns:

x*s, y*s.

Computes x*s,y*s. The order of arguments is chosen so that it’s possible to chain operations (see example).

Example:

velx,vely = vec.mul(dt, vec.add(velx,vely, accx,accy))
vector.div(s, x, y)
Arguments:
  • s (number) – A scalar.
  • x,y (numbers) – A vector.
Returns:

x/s, y/s.

Computes x/s,y/s. The order of arguments is chosen so that it’s possible to chain operations (see example).

Example:

x,y = vec.div(self.zoom, x-w/2, y-h/2)
vector.add(x1, y1, x2, y2)
Arguments:
  • x1,y1 (numbers) – First vector.
  • x2,y2 (numbers) – Second vector.
Returns:

x1+x2, x1+x2.

Computes the sum \((x1+x2, y1+y2)\)`` of two vectors. Meant to be used in conjunction with other functions like vector.mul().

Example:

player.x,player.y = vector.add(player.x,player.y, vector.mul(dt, dx,dy))
vector.sub(x1, y1, x2, y2)
Arguments:
  • x1,y1 (numbers) – First vector.
  • x2,y2 (numbers) – Second vector.
Returns:

x1-x2, x1-x2.

Computes the difference \((x1-x2, y1-y2)\) of two vectors. Meant to be used in conjunction with other functions like vector.mul().

Example:

dx,dy = vector.sub(400,300, love.mouse.getPosition())
vector.permul(x1, y1, x2, y2)
Arguments:
  • x1,y1 (numbers) – First vector.
  • x2,y2 (numbers) – Second vector.
Returns:

x1*x2, y1*y2.

Component-wise multiplication, i.e.: x1*x2, y1*y2.

Example:

x,y = vector.permul(x,y, 1,1.5)
vector.dot(x1, y1, x2, y2)
Arguments:
  • x1,y1 (numbers) – First vector.
  • x2,y2 (numbers) – Second vector.
Returns:

x1*x2 + y1*y2.

Computes the dot product of two vectors: x1*x2 + y1*y2.

Example:

cosphi = vector.dot(rx,ry, vx,vy)
vector.cross(x1, y1, x2, y2)
Arguments:
  • x1,y1 (numbers) – First vector.
  • x2,y2 (numbers) – Second vector.
Returns:

x1*y2 - y1*x2.

Computes the cross product of two vectors: x1*y2 - y1*x2.

Example:

parallelogram_area = vector.cross(ax,ay, bx,by)
vector.vector.det(x1, y1, x2, y2)
Arguments:
  • x1,y1 (numbers) – First vector.
  • x2,y2 (numbers) – Second vector.
Returns:

x1*y2 - y1*x2.

Alias to vector.cross().

Example:

parallelogram_area = vector.det(ax,ay, bx,by)
vector.eq(x1, y1, x2, y2)
Arguments:
  • x1,y1 (numbers) – First vector.
  • x2,y2 (numbers) – Second vector.
Returns:

x1 == x2 and y1 == y2

Test for equality.

Example:

if vector.eq(x1,y1, x2,y2) then be.happy() end
vector.le(x1, y1, x2, y2)
Arguments:
  • x1,y1 (numbers) – First vector.
  • x2,y2 (numbers) – Second vector.
Returns:

x1 <= x2 and y1 <= y2.

Test for partial lexicographical order, <=.

Example:

if vector.le(x1,y1, x2,y2) then be.happy() end
vector.lt(x1, y1, x2, y2)
Arguments:
  • x1,y1 (numbers) – First vector.
  • x2,y2 (numbers) – Second vector.
Returns:

x1 < x2 or (x1 == x2) and y1 <= y2.

Test for strict lexicographical order, <.

Example:

if vector.lt(x1,y1, x2,y2) then be.happy() end
vector.len(x, y)
Arguments:
  • x,y (numbers) – The vector.
Returns:

Length of the vector.

Get length of a vector, i.e. math.sqrt(x*x + y*y).

Example:

distance = vector.len(love.mouse.getPosition())
vector.len2(x, y)
Arguments:
  • x,y (numbers) – The vector.
Returns:

Squared length of the vector.

Get squared length of a vector, i.e. x*x + y*y.

Example:

-- get closest vertex to a given vector
closest, dsq = vertices[1], vector.len2(px-vertices[1].x, py-vertices[1].y)
for i = 2,#vertices do
    local temp = vector.len2(px-vertices[i].x, py-vertices[i].y)
    if temp < dsq then
        closest, dsq = vertices[i], temp
    end
end
vector.dist(x1, y1, x2, y2)
Arguments:
  • x1,y1 (numbers) – First vector.
  • x2,y2 (numbers) – Second vector.
Returns:

The distance of the points.

Get distance of two points. The same as vector.len(x1-x2, y1-y2).

Example:

-- get closest vertex to a given vector
-- slightly slower than the example using len2()
closest, dist = vertices[1], vector.dist(px,py, vertices[1].x,vertices[1].y)
for i = 2,#vertices do
    local temp = vector.dist(px,py, vertices[i].x,vertices[i].y)
    if temp < dist then
        closest, dist = vertices[i], temp
    end
end
vector.dist2(x1, y1, x2, y2)
Arguments:
  • x1,y1 (numbers) – First vector.
  • x2,y2 (numbers) – Second vector.
Returns:

The squared distance of two points.

Get squared distance of two points. The same as vector.len2(x1-x2, y1-y2).

Example:

-- get closest vertex to a given vector
closest, dsq = vertices[1], vector.dist2(px,py, vertices[1].x,vertices[1].y)
for i = 2,#vertices do
    local temp = vector.dist2(px,py, vertices[i].x,vertices[i].y)
    if temp < dsq then
        closest, dsq = vertices[i], temp
    end
end
vector.normalize(x, y)
Arguments:
  • x,y (numbers) – The vector.
Returns:

Vector with same direction as the input vector, but length 1.

Get normalized vector, i.e. a vector with the same direction as the input vector, but with length 1.

Example:

dx,dy = vector.normalize(vx,vy)
vector.rotate(phi, x, y)
Arguments:
  • phi (number) – Rotation angle in radians.
  • x,y (numbers) – The vector.
Returns:

The rotated vector

Get a rotated vector.

Example:

-- approximate a circle
circle = {}
for i = 1,30 do
    local phi = 2 * math.pi * i / 30
    circle[i*2-1], circle[i*2] = vector.rotate(phi, 0,1)
end
vector.perpendicular(x, y)
Arguments:
  • x,y (numbers) – The vector.
Returns:

A vector perpendicular to the input vector

Quick rotation by 90°. The same (but faster) as vector.rotate(math.pi/2, x,y).

Example:

nx,ny = vector.normalize(vector.perpendicular(bx-ax, by-ay))
vector.project(x, y, u, v)
Arguments:
  • x,y (numbers) – The vector to project.
  • u,v (numbers) – The vector to project onto.
Returns:

The projected vector.

Project vector onto another vector.

Example:

vx_p,vy_p = vector.project(vx,vy, ax,ay)
vector.mirror(x, y, u, v)
Arguments:
  • x,y (numbers) – The vector to mirror.
  • u,v (numbers) – The vector defining the axis.
Returns:

The mirrored vector.

Mirrors vector on the axis defined by the other vector.

Example:

vx,vy = vector.mirror(vx,vy, surface.x,surface.y)
vector.angleTo(ox, y, u, v)
Arguments:
  • x,y (numbers) – Vector to measure the angle.
  • u,v (optional) (numbers) – Reference vector.
Returns:

Angle in radians.

Measures the angle between two vectors. u and v default to 0 if omitted, i.e. the function returns the angle to the coordinate system.

Example:

lean = vector.angleTo(self.upx, self.upy, 0,1)
if lean > .1 then self:fallOver() end
vector.trim(max_length, x, y)
Arguments:
  • max_length (number) – Maximum allowed length of the vector.
  • x,y (numbers) – Vector to trum.
Returns:

The trimmed vector.

Trim the vector to max_length, i.e. return a vector that points in the same direction as the source vector, but has a magnitude smaller or equal to max_length.

Example:

vel_x, vel_y = vector.trim(299792458,
                           vector.add(vel_x, vel_y,
                                      vector.mul(mass * dt, force_x, force_y)))