Quick and dirty vector class

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Quick and dirty vector class

Post by trubblegum » Tue Apr 03, 2012 9:48 pm

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 :
- r is radians
- 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,
}
setmetatable(vector, {__call = vector.load})
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 :crazy:
Comments? Suggestions? Etc?

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

Re: Quick and dirty vector class

Post by rokit boy » Tue Apr 03, 2012 9:56 pm

Nice :D

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

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

Re: Quick and dirty vector class

Post by trubblegum » Tue Apr 03, 2012 10:08 pm

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

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

Re: Quick and dirty vector class

Post by rokit boy » Tue Apr 03, 2012 10:09 pm

I mean edit...
u wot m8

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

Re: Quick and dirty vector class

Post by jradich » Wed Apr 04, 2012 2:53 am

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!

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

Re: Quick and dirty vector class

Post by rokit boy » Wed Apr 04, 2012 3:06 am

jradich wrote:
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!
PLEASE DELETE THAT LINK OF THERE!
u wot m8

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

Re: Quick and dirty vector class

Post by jradich » Wed Apr 04, 2012 3:13 am

rokit boy wrote:
jradich 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!
PLEASE DELETE THAT LINK OF THERE!
Get that link outta there, stoopid
Losing a friend's trust is the fastest way to lose a friend, forever. FOREVER!

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

Re: Quick and dirty vector class

Post by rokit boy » Wed Apr 04, 2012 3:22 am

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

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

Re: Quick and dirty vector class

Post by jradich » Wed Apr 04, 2012 3:52 am

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!

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

Re: Quick and dirty vector class

Post by mickeyjm » Wed Apr 11, 2012 1:58 pm

rokit boy wrote:
jradich wrote:
rokit boy wrote:I mean edit...
Rokit boy. You thought you could escape this question?! [REDACTED] explain!!

And trubblegum.... Good job.
THAT IS MY STACK LIBRARY WHICH I ABANDONED!
PLEASE DELETE THAT LINK OF THERE!
Lol. He deleted the link but you left it in the quote
(I removed it in my quote)
Your screen is very zoomed in...

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests