## Quick and dirty vector class

trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

### Quick and dirty vector class

Since I wrote this up in the support section, I figured I'd expand it a little into a lightweight vector class, and a (admittedly, quick) search revealed nothing similar in recent times, so why not share.
Don't get me wrong .. HUMP.vector is a fine lib. This is just a quick-and-dirty snippet which may be useful for simple stuff, with an example showing a circle following the mouse at a constant speed.

Warning : for simplicity, operators rely on vector being declared globally, as in the code below.

Function naming convention goes :
- d is degrees
- v is vector
-to- is a conversion from prefix (or vector if none) to suffix

Code: Select all

vector = {
load = function(this, x, y) return setmetatable((not x and {x = 0, y = 0}) or (y and {x = x, y = y}) or {x = x.x or x[1] or 0, y = x.y or x[2] or 0}, vectormt) end,
normal = function(v) l = v:len(); return vector(v.x / l, v.y / l) end,
len = function(v) return math.sqrt((v.x * v.x) + (v.y * v.y)) end,
tor = function(v) return math.atan2(v.y, v.x) end,
tod = function(v) return math.atan2(v.y, v.x) * (180 / math.pi) end,
rtov = function(r) return vector(math.cos(r), math.sin(r)) end,
dtov = function(d) return vector.rtovec(d * (math.pi / 180)) end,
}
vectormt = {
__index = vector,
__add = function(this, v) return (type(v) == 'table' and vector(this.x + v.x, this.y + v.y)) or vector(this.x + v, this.y + v) end,
__sub = function(this, v) return (type(v) == 'table' and vector(this.x - v.x, this.y - v.y)) or vector(this.x - v, this.y - v) end,
__mul = function(this, v) return (type(v) == 'table' and vector(this.x * v.x, this.y * v.y)) or vector(this.x * v, this.y * v) end,
__div = function(this, v) return (type(v) == 'table' and vector(this.x / v.x, this.y / v.y)) or vector(this.x / v, this.y / v) end,
__pow = function(this, v) return (type(v) == 'table' and vector(this.x ^ v.x, this.y ^ v.y)) or vector(this.x ^ v, this.y ^ v) end,
__unm = function(this) return vector(0 - this.x, 0 - this.y) end,
__tostring = function(this) return 'vector(x = '..this.x..', y = '..this.y..')' end,
}

mouse = function() return vector(love.mouse.getX(), love.mouse.getY()) end

love.load = function() player = {p = vector(64, 64), speed = 128} end
love.update = function(dt) player.p = player.p + ((mouse() - player.p):normal() * (player.speed * dt)) end
love.draw = function() love.graphics.circle('fill', player.p.x, player.p.y, 8, 8) end
Maybe I'll call it G-pöint

rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

### Re: Quick and dirty vector class

Nice

I will totally use this. Do you mind if I write an "addon" for this including a third vector which is pretty much scale if translated to 2D.

Call it something dirty like pretty much every single lib on here.
u wot m8

trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

### Re: Quick and dirty vector class

Not sure what you mean, but you're free to use it as you see fit.

rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

### Re: Quick and dirty vector class

I mean edit...
u wot m8

Party member
Posts: 100
Joined: Mon Dec 12, 2011 8:44 pm

### Re: Quick and dirty vector class

EDITED CUZ ROKIT BOY GETS BANNED A LOT AT THIS OTHER PLACE
Last edited by jradich on Wed Apr 04, 2012 3:12 am, edited 1 time in total.
Losing a friend's trust is the fastest way to lose a friend, forever. FOREVER!

rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

### Re: Quick and dirty vector class

rokit boy wrote:I mean edit...
Rokit boy. You thought you could escape this question?! http://forum.stabyourself.net/viewtopic.php?f=11&t=1266 explain!!

And trubblegum.... Good job.
THAT IS MY STACK LIBRARY WHICH I ABANDONED!
u wot m8

Party member
Posts: 100
Joined: Mon Dec 12, 2011 8:44 pm

### Re: Quick and dirty vector class

rokit boy wrote:
rokit boy wrote:I mean edit...
Rokit boy. You thought you could escape this question?! DELETED LINK CUZ IM NOT STOOPID explain!!

And trubblegum.... Good job.
THAT IS MY STACK LIBRARY WHICH I ABANDONED!
Get that link outta there, stoopid
Losing a friend's trust is the fastest way to lose a friend, forever. FOREVER!

rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

### Re: Quick and dirty vector class

What was I men't to explain anyway?
u wot m8

Party member
Posts: 100
Joined: Mon Dec 12, 2011 8:44 pm

### Re: Quick and dirty vector class

rokit boy wrote:What was I men't to explain anyway?
Take this to forum.stabyourself.net, man. (Lawnboyinajar)

Back to vector class. BRAVO, SIR.
Losing a friend's trust is the fastest way to lose a friend, forever. FOREVER!

mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

rokit boy wrote: