There are plenty of use cases for vectors that are not at all related to Shaders.
For context, read through the
Linear Algebra for game developers series. Those articles will answer the general question of what a vector
is, what it's used for, and what some of those vector functions are.
Note that a vector is just a mathematical concept. You can certainly represent a vector2 or a vector3 as a table of values. However, I think your question is actually more along the lines of "why not just use a table
instead of a vector library?".
The short answer is because it can make your code more clear (with all the benefits that come from that).
For a longer answer, I'll show you an example using plain tables and one using a vector library.
Imagine we have a player spaceship, with a position, a direction, and a constant speed in pixels per second:
Code: Select all
local player_position = {200, 100}
local player_direction = {-1, 0} -- Facing left
local player_speed = 100
local enemy_position = {300, 50}
Then you can create vector math functions which can operate on those values:
Code: Select all
function addVectors(a, b)
return { a[1] + b[1], a[2] + b[2] }
end
function multiplyVectorAndScalar(vec, scalar)
return { vec[1] * scalar, vec[2] * scalar }
end
function distanceBetweenVectors(a, b)
local dx = a.x - b.x
local dy = a.y - b.y
return sqrt(dx * dx + dy * dy)
end
And then use them to move the ship around the screen:
Code: Select all
function love.update(dt)
local player_movement = multiplyVectorAndScalar(player_direction, player_speed * dt)
player_position = addVectors(player_position, player_movement)
local distance_from_enemy = distanceBetweenVectors(player_position, enemy_position)
end
function love.draw()
-- For simplicity our ship will just be a circle
love.graphics.circle('line', player_position[1], player_position[2], 30, 10)
end
This is perfectly functional and is definitely a pattern you'd see in some shipping games, particularly those written in C. However, it's a little clunky. We have to refer to the x and y components in the vector with numeric keys. The mathematical operations are a little hard to parse. We'd need to add many more functions to handle all of the vector math operations we want to support (e.g. multiply two vectors, normalization, etc.) Also, there's no checking of the inputs to the functions above so it could be hard to catch mistakes.
Lua offers us a fair bit of flexibility which will allow us to express the same functionality much more clearly and safely.
Let's rewrite the example above using the
HUMP vector library:
Code: Select all
require 'vector'
local player_position = vector(200, 100)
local player_direction = vector(-1, 0) -- Facing left
local player_speed = 100
function love.update(dt)
local player_movement = player_direction * player_speed * dt
player_position = player_position + player_movement
local distance_from_enemy = player_position:dist(enemy_position)
end
function love.draw()
-- For simplicity our ship will just be a circle
love.graphics.circle('line', player_position.x, player_position.y, 30, 10)
end
Note how in love.update the movement calculations are much easier to read and look much more like the examples in the article.
The HUMP vector library isn't doing a bunch of incomprehensible magic to make this work. The file itself is only about a screens worth of code. If you read through it you'll notice that internally it's just storing the vector as a table with two values. It's relying in some Lua idioms to make the syntax clean and easy to work with and reason about.