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

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

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

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!
Attachments
passionavalanche.love
PassionAvalanche, updated to passion 0.6.2rc1
Last edited by kikito on Wed Mar 24, 2010 10:28 pm, edited 5 times in total.
When I write def I mean function.

TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

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

Windows XP SP3 Love 0.5.0 crashes on load. Love window blinks on and off without warning. No error in stdout.txt

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

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

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.
When I write def I mean function.

TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

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

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 (98.78 KiB) Viewed 10554 times

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

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

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.
When I write def I mean function.

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

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

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})

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.
When I write def I mean function.

TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

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

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.

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

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

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.
When I write def I mean function.

Tenoch
Citizen
Posts: 76
Joined: Mon Jul 21, 2008 7:49 am

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

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.
"When in doubt, use brute force." Ken Thompson

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

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

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
When I write def I mean function.

### Who is online

Users browsing this forum: No registered users and 3 guests