Page 1 of 10

PÄSSION: object-oriented LÖVE

Posted: Sat Nov 07, 2009 1:33 am
by kikito
Hi everyone,

For the last couple of months I've been toying with the idea of adding object oriented functionality to LÖVE.

I've developed one (still very experimental!) library called PÄSSION (passion in the code). Its main features are:
  • There's an Actor class
  • Actors can have an associated image and a physical body - both optional
  • You can create "types of actors" (subclasses - for example a class called "Bullet") and create instances of them(each individual bullet)
  • Actors have an "update(dt)" function (the update function of a bullet would be "advance dt*v, check for hits, if you are out of the limits then destroy yourself")
  • Actors with a body have methods of body. So you don't have to do bullet.body:setMassFromShapes() - you can do bullet:setMassFromShapes() instead
  • Similarly, passion has world-delegated functions. So you can do passion.world:setGravity(), but also passion:setGravity()
  • By callig passion:update(dt) you update the physical word and then call "update" on each actor instance (so no need to have a loop to update the bullets and another one for the enemies, for example)
  • Object-orientation is implemented by a lib called MiddleClass. This provides subclassing, instantiation and a very interesting super() facility (on all methods, for calling their parent version) among other things, like crude mixin support.
  • There's allways one world, and one ground body, for physical objects. They are created at the same time, by the PÄSSION lib.
Maybe you would preffer to see it in action. I'm attaching a PASSION-remade loveavalanche demo. It contains the current version of the PASSION lib (which in turn contains MiddleClass).

I've made the code on this demo a bit longer than the strictly necessary in order to show how inheritance can be used - there's a class Ball, and then 3 subclasses: GreenBall, SmallBall and BigBall. In reality, the Ball class is all that is needed on this example, but I wanted to show inheritance.

I'd love to hear from you any feedback from you guys - any feedback will be greatly appreciated. Opinion away!

For example: one of the things I'm not sure about is how I used love.filesystem.require - should I be using "absolute paths" in all files, or is there a better way of doing it?

Finally, two important disclaimers:
  • Updated to love 0.6
  • This has evolved enough to be considered a "very early alpha"
Thanks a lot!

Re: PÄSSION: object-oriented LÖVE

Posted: Sat Nov 07, 2009 2:53 am
by TechnoCat
Windows XP SP3 Love 0.5.0 crashes on load. Love window blinks on and off without warning. No error in stdout.txt

Re: PÄSSION: object-oriented LÖVE

Posted: Sat Nov 07, 2009 12:22 pm
by kikito
Hi technocat,

Thank you for spotting this.

I didn't build the zip file correctly (I left the top folder - this is what you get for progamming at 2 in the morning). I've corrected this now.

Re: PÄSSION: object-oriented LÖVE

Posted: Sat Nov 07, 2009 1:56 pm
by TechnoCat
The inheritance/super commands are sexy.

UPDATE: Leave it open long enough and the actors stop colliding
Picture 2009-11-07 09_31_49.png
Picture 2009-11-07 09_31_49.png (98.78 KiB) Viewed 19394 times

Re: PÄSSION: object-oriented LÖVE

Posted: Sat Nov 07, 2009 8:08 pm
by kikito
Hi Technocat,

Again, thanks for testing.

If you are interested, I based my "super()" implementation on one of the most recent changes in ObjectLua(http://lua-users.org/wiki/ObjectLua) - a lua OO lib for grown-ups.

I really don't have a clear idea why the balls stop colliding after time. The the actors aren't "loosing their bodies", since they continue falling down. Maybe this is a consequence of using 0.5 - rude mentioned on other post that there were some issues on physics on that version.

I'll see if I can export it to 0.6 - give me two days.

Re: PÄSSION: object-oriented LÖVE

Posted: Mon Nov 09, 2009 11:20 pm
by kikito
Ok I got it working with 0.6!

A couple notes:
  • Since images are not "centered" any more on 0.6, I had to add some setCenter and getCenter helper methods. I'm still not satisfied with the results (the borders are not exactly "pixel-perfect" with the images), but I believe I'll leave them as they are now
  • I transformed the parameters used on Actor creation. Now you have to do

    Code: Select all

    passion.Actor:subClass('YourActorClassName', {hasImage = boolean, hasBody = boolean})
    instead of

    Code: Select all

    passion.Actor:subClass('YourActorClassName', boolean, boolean)
    This is a bit more verbose but also more flexible - future options(i.e. hasMultipleBodies) will work without breaking backwards. I also like that you don't have to remember the parameters' order. The whole {} stuff is optional.
  • Actors now have Images by default; but they don't have bodies by default any more.
  • The simulation runs much faster than before ... but again, after some time, collisions seem to stop working. I suspect that this is related with the increasingly-faster speed. However, I'm not able to test this because body:setVelocity seems to be missing in current version. I've posted this on the main 0.6 thread
I'll stop with the passionavalanche stuff now - I'm going to try porting Rude's "no project" presentation to OO. I'll keep you posted.

Re: PÄSSION: object-oriented LÖVE

Posted: Mon Nov 09, 2009 11:26 pm
by TechnoCat
The balls stopped rotating. Not sure if this is a body thing or an image draw thing.
Also, they still stop colliding at a certain time and get very fast.

Re: PÄSSION: object-oriented LÖVE

Posted: Tue Nov 10, 2009 8:39 am
by kikito
Hi Technocat,

I deactivated the rotation in order to debug the centering more easily. I've activated it again. Thanks for the testing!

I don't know why the bodies stop colliding - I may do more tests when 0.6 is more advanced.

Re: PÄSSION: object-oriented LÖVE

Posted: Tue Nov 10, 2009 9:09 am
by Tenoch
The bodies that stop colliding is very likely do to garbage collection. Check that you keep references to the shapes somewhere, otherwise they'll be destroyed. The easiest way is to put them in a table somewhere.

Re: PÄSSION: object-oriented LÖVE

Posted: Tue Nov 10, 2009 11:12 pm
by kikito
Tenoch,

Your advice was spot on. I wasn't storing references to the generated shapes, so they were being garbage-collected.

I've modified PÄSSION so actors with a body hold information about their shapes automatically - and now the balls keep bouncing forever! It is updated on the first post.

Kudos to you!

I'm starting to think that names containing permutations of "techno" mean "very helpful contributor" on some forgotten ancient language, or something :ultrahappy: