Building Love2d with Vectors
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- seeker14491
- Prole
- Posts: 9
- Joined: Thu Nov 29, 2012 10:47 pm
Building Love2d with Vectors
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?
Re: Building Love2d with Vectors
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.
Lua-vec seems quite tricky to use too, since it's a modification and extension of the language Lua.
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
- seeker14491
- Prole
- Posts: 9
- Joined: Thu Nov 29, 2012 10:47 pm
Re: Building Love2d with Vectors
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?
Re: Building Love2d with Vectors
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:
Vector functionality can be implemented as a separate module, something like:
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).
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
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 )
- 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
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.
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.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Building Love2d with Vectors
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:
There's very little overhead compared to using native vectors, and the gc will perform much better.
Code: Select all
function sumVectors(x1,y1,x2,y2)
-- do something here
end
When I write def I mean function.
- seeker14491
- Prole
- Posts: 9
- Joined: Thu Nov 29, 2012 10:47 pm
Re: Building Love2d with Vectors
Thanks for the suggestions, I'll try them out. I think they'll suit my needs.
- seeker14491
- Prole
- Posts: 9
- Joined: Thu Nov 29, 2012 10:47 pm
Re: Building Love2d with Vectors
This is what I was doing. I wanted vectors because they would make my code shorter, easier to write and understand, and hopefully faster.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:
There's very little overhead compared to using native vectors, and the gc will perform much better.Code: Select all
function sumVectors(x1,y1,x2,y2) -- do something here end
Re: Building Love2d with Vectors
Did you notice that luajit is faster than lua-vec? Just use luajit-based love2d executables (as I always do).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?
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
- seeker14491
- Prole
- Posts: 9
- Joined: Thu Nov 29, 2012 10:47 pm
Re: Building Love2d with Vectors
Yeah, I did notice, but I didn't know it could be used with Love2D. Is everything compatible when used with Love2D?miko wrote:Did you notice that luajit is faster than lua-vec? Just use luajit-based love2d executables (as I always do).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?
Who is online
Users browsing this forum: Bing [Bot] and 3 guests