Vectors for a dummie

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Zulsorai
Prole
Posts: 16
Joined: Fri Jun 20, 2014 1:57 am

Vectors for a dummie

Post by Zulsorai »

Recently I have decided to get back into programming. Since all of ny knowledge is self taught there are lots of gaps in it. It seems that vectors are used a lot in peoples examples I have seen and was hoping that maybe someone with more knowledge on the subject could explain the benifits of them.
All I know is that for example vec3 would contain 3 different numbers, but why not just use a table instead? I am aware that there are some vector functions like there are table functions, but I am unaware of how to properly use them or for what purposes to use them.

Cheers.
Life without internet is life without food, you won't last long.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Vectors for a dummie

Post by Robin »

I think you're talking about the [wiki]Shader[/wiki] language. That is not Lua, so it has different concepts.
Help us help you: attach a .love.
Zulsorai
Prole
Posts: 16
Joined: Fri Jun 20, 2014 1:57 am

Re: Vectors for a dummie

Post by Zulsorai »

That would probably be it, like I said many holes in my knowledge. Thanks for the clarification.
Life without internet is life without food, you won't last long.
GloryFish
Prole
Posts: 19
Joined: Tue Jan 11, 2011 4:43 pm

Re: Vectors for a dummie

Post by GloryFish »

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.
Zulsorai
Prole
Posts: 16
Joined: Fri Jun 20, 2014 1:57 am

Re: Vectors for a dummie

Post by Zulsorai »

Thank you for this post, definitly a lot of interesting stuff in here. I wish that I knew more about this more when I first started messing around with Love. It would of made some things a lot easier for me since everything for me is self taught and I would of never really looked into this stuff before.
Cheers.
Life without internet is life without food, you won't last long.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 46 guests