PDA

View Full Version : APE 2d Physics Engine in ObjectPascal...



Vinzvega
25-01-2009, 02:55 PM
Hi everybody !

Several monthes, I convert 2d alpha-stage physics engine APE, from Flash ActionScript to Object PAscal.
This engine was initialy wrote by Alec Cove (http://www.cove.org/ape/)

I made first conversion version (many Memory leaks yet !), and then, I'll send a mail to Alec and ask him to add the conversion on his repository, but no response :(
Therefore, the Ape web site have no more activity during last year. --> perhaps (unfortunaly) neglected ?

On APE site, you'll find Java and c++ conversion too.

I'll release now this code as is, perhaps it could be usefull for somebody ?
I think It could be cool for a 2d casual game.

I'll continue the translation (079 is on site, but I feel this is R&D... so, I prefere wait and see)
when free time occurs :)) but feel free to add stuff and show it to us !

quick Features :
- Circles, Rectangles and Wheels particles.
- Spring constraints.
- Groups processing.
- Groups Collision.

The base concept is simple :
On grouping Particles with constraints, you'll build model with a real physical response, then, you register Collision interaction and you'll get a "physical Word"
See examples :)

Notes :
- In ActionSCript, it is a reasonable fast engine, so, in Pascal, throught Delphi compiler, it is *more* than a reasonable fast. ;)
- I feel the Original ActionScript code very well written : try to keep its Object Model in Pascal version.
- In ActionScript, there is a Garbage Colector. Not such stuff in Delphi (language for big boys :)) --> So, there are certainly few (many) memory leaks, cause code is mostly a direct translation.


To do :
- I think the collision treatement is not bestest method --> It should be best to base it on cross line with geometric approach.
--> This will avoid completely the object's cross over behaviour.
- Event's should be spread in code, in order to link easely with a game. But Status can be made too.
- there are a lack of usefull methods to handle easely object.


Here the looks of Pascal Version, in Delphi :
http://www.objectifstudio.com/Delphi/ApeDelphi/previsu.JPG


You'll find other inline (flash) examples on Alec's Web site.

Download here Delphi version (Sources and binaries) :
http://www.objectifstudio.com/Delphi/ApeDelphi/Ape_a045_Delphi.zip

Have fun !

Vincent Gsell,-
:idea:

chronozphere
25-01-2009, 07:16 PM
Hey man... That looks very interesting.

I tested your demo's and my overall impression was good. However, Sometimes the boxes will get stuck inside eachother and once i saw an object that spontaniously lifted and moved upwards like a helium baloon. :lol:
Moreover, the demo crashed when i added too many objects and it took two minutes to terminate it because it slowed down my entire system. But i guess that's because of the many memory leaks you mentioned.

I think it's very nice. Okay, it needs some work (bugfixes), but apart from that, it's quite impressive. ;)

Vinzvega
26-01-2009, 06:38 AM
Hi,

How, 2 minutes slow down. Keep Ctrl Alt Del nearest you :) :) :)

You right, as I mentioned, slow down come from 2 things :
- Memory leaks
- And, in the demo which let you add many object, the object are not freed, even if they are outside the screen !! :)

In fact, this demo doesn't exist in Flash : I made for stress test :)

I think many things could be do with this stuff, but several control code must be implemented.

jdarling
26-01-2009, 05:19 PM
In this case interfaces are your friends. Granted they will be more difficult to work with in some cases, but they will give you the garbage collection you desire. Also a generic collection within each unit that keeps track of created/freed is another good idea. This way on unit finalization you can cleanup the objects created that were not freed. You could also generate a report from the list that would help with debugging your "memory leaks".

In general, I think I'll take a look at this one. I started working with APE and a conversion a while back but gave up due to time. This looks like a good starting point, and I'd be interested to see a finished product come of it :)

jdarling
27-01-2009, 05:49 PM
Ok, so I got bored and converted your project over to Lazarus as well as cleaned up the problems with memory leaks (at least with the objects) and gave everything proper Pascalish names (IE: added T in front of the existing names).

I completed the abstraction of the render, and added in the ability to set the render offset properties. This allows the same APE instance to be painted to multiple renders all at once if need be.

Also added a logger unit for checking that all resources that are created are freed (look at top level parents).

As long as the log is clear you have no leaks on exit! Converted the Car and Proto (Complete) demos just for testing. All works great.

Some of the headers got lost along the way, and a few comments, but that isn't hard to put back in.

Download zip contains the original files and new Laz folder with Lib and Demos. Fairly fun thing to play with I must say.

Download at http://www.eonclash.com/PGD/ape.zip (http://www.eonclash.com/PGD/ape.zip)

savage
27-01-2009, 09:40 PM
Belatedly bumped to news item. Though it will appear a little ways down now.

arthurprs
27-01-2009, 10:47 PM
Awesome, downloading to make some tests :D

good work

Vinzvega
28-01-2009, 06:54 AM
Ok, so I got bored and converted your project over to Lazarus as well as cleaned up the problems with memory leaks (at least with the objects) and gave everything proper Pascalish names (IE: added T in front of the existing names).

I completed the abstraction of the render, and added in the ability to set the render offset properties. This allows the same APE instance to be painted to multiple renders all at once if need be.

Also added a logger unit for checking that all resources that are created are freed (look at top level parents).

As long as the log is clear you have no leaks on exit! Converted the Car and Proto (Complete) demos just for testing. All works great.

Some of the headers got lost along the way, and a few comments, but that isn't hard to put back in.

Download zip contains the original files and new Laz folder with Lib and Demos. Fairly fun thing to play with I must say.



Hi Jeremy,

Hey cool, good work for the log, Since it is a direct ActionScript direct translation, I'm curious to make test with it.

For the "T" adding for type, why not. I'm not a fan of "T", I see too many of them every day ;)
It should be cool to keep at least Alec's original header in your version.

Great for Lazarus port : It would work on Linux, then ?
A notice that in "car" demo, I couldnt drive the car (key a and d - country dependant) anymore ? Surely a keyb map problem.

When I got a little time, I'll see the collision system : I m sure it could be better : It's mainly responsible of the "object cross over each other" behaviour that occurs.
And this behaviour trigs the "helium balloon effect" descript before, cause of the dynamic falsly evaluated in "constraint" step.

Savage : Thanks to put in News :)
And the others : Thanks for the good words too :)

bye

jdarling
28-01-2009, 03:33 PM
I didn't add the A and D keys until just recently and I haven't updated the zip on the server with the new code yet. I'll do that some time today.

The version I put up runs in Linux with no problems, I don't have access to my MAC currently so I couldn't test it there. Maybe we can get Savage to be nice and give it a shot :)

The collision issue comes simply from not predicting or checking history for collisions. This should be easily remedied, I will look back at my Verlet system and see if the same "fixes" won't work here since its about the same idea.