Building Love2d with Vectors

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.
User avatar
seeker14491
Prole
Posts: 9
Joined: Thu Nov 29, 2012 10:47 pm

Building Love2d with Vectors

Post by seeker14491 »

I'm making a simple physics engine with Love2d to learn some physics. A lot of the calculations involve vectors, so I want to have vectors in Love2d. I know you can simulate vectors with just lua, but the performance isn't good. I found lua-vec http://code.google.com/p/lua-vec/ , which looks simple to use and has good performance, but I don't know how to make it work with Love2d. Can someone explain what I need to do to get it to work?
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Building Love2d with Vectors

Post by T-Bone »

You could just use tables as vectors. The performance will then only depend on how you implement the vector operations. You should be able to get at least decent performance without too much effort.

Lua-vec seems quite tricky to use too, since it's a modification and extension of the language Lua.
User avatar
seeker14491
Prole
Posts: 9
Joined: Thu Nov 29, 2012 10:47 pm

Re: Building Love2d with Vectors

Post by seeker14491 »

Using tables is very slow, as seen here: http://code.google.com/p/lua-vec/wiki/Benchmarks . Is there a way to build a modified version of lua and then make a modified Love2d?
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Building Love2d with Vectors

Post by ivan »

The biggest problem with vectors in my experience is the overhead on the garbage collector. If you have a function that returns a 'new' vector every frame your memory consumption can easily get out of hand and the GC cycles will become longer. Certain C implementations use a memory pool but it's still something I find quite unnecessary.
Lua has the option of returning multiple values so why not just use that to your advantage and avoid using vectors altogether:

Code: Select all

object.getPosition = function ( object )
  return self.posX, self.posY
end
Vector functionality can be implemented as a separate module, something like:

Code: Select all

x, y = object:getPosition()

vec.rotate ( x, y, math.pi )

x2, y2 = object2:getPosition()
x3, y3 = vec.add ( x, y, x2, y2 )

dist = vec.distance ( x, y, x2, y2 )
Lastly, are you sure that vectors are the bottleneck of your application or are you projecting ahead? If you are really insistent on using vectors, just implement them in Lua and if it becomes an issue you can always optimize by moving the code to a C extension later on (although I highly doubt the result will be noticeable).
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Building Love2d with Vectors

Post by Roland_Yonaba »

I am inclined to think the same. Are you really sure that the problem is related to that built-in vector feature?
Anyway, I'd like to add that emulating vector with Lua is not really complicated. You can grab some examples you can adapt for your needs.
For instance, i have a simple implementation i am using for an on-going project.
There is also a very complete and clean emulation of vector operations inside vrld's hump (docs here). And I found any of them fast enough.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Building Love2d with Vectors

Post by kikito »

If you are using 2d vectors only, you can get away with using them as pairs of integers. So for example, a function that accepts two vectors looks like this:

Code: Select all

function sumVectors(x1,y1,x2,y2)
-- do something here
end
There's very little overhead compared to using native vectors, and the gc will perform much better.
When I write def I mean function.
User avatar
seeker14491
Prole
Posts: 9
Joined: Thu Nov 29, 2012 10:47 pm

Re: Building Love2d with Vectors

Post by seeker14491 »

Thanks for the suggestions, I'll try them out. I think they'll suit my needs.
User avatar
seeker14491
Prole
Posts: 9
Joined: Thu Nov 29, 2012 10:47 pm

Re: Building Love2d with Vectors

Post by seeker14491 »

kikito wrote:If you are using 2d vectors only, you can get away with using them as pairs of integers. So for example, a function that accepts two vectors looks like this:

Code: Select all

function sumVectors(x1,y1,x2,y2)
-- do something here
end
There's very little overhead compared to using native vectors, and the gc will perform much better.
This is what I was doing. I wanted vectors because they would make my code shorter, easier to write and understand, and hopefully faster.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Building Love2d with Vectors

Post by miko »

seeker14491 wrote:Using tables is very slow, as seen here: http://code.google.com/p/lua-vec/wiki/Benchmarks . Is there a way to build a modified version of lua and then make a modified Love2d?
Did you notice that luajit is faster than lua-vec? Just use luajit-based love2d executables (as I always do).
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
seeker14491
Prole
Posts: 9
Joined: Thu Nov 29, 2012 10:47 pm

Re: Building Love2d with Vectors

Post by seeker14491 »

miko wrote:
seeker14491 wrote:Using tables is very slow, as seen here: http://code.google.com/p/lua-vec/wiki/Benchmarks . Is there a way to build a modified version of lua and then make a modified Love2d?
Did you notice that luajit is faster than lua-vec? Just use luajit-based love2d executables (as I always do).
Yeah, I did notice, but I didn't know it could be used with Love2D. Is everything compatible when used with Love2D?
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 3 guests