Using Chipmunk2d for Unity objects with a pool manager

Chipmunk2D Bindings for the Unity3D engine

Using Chipmunk2d for Unity objects with a pool manager

Postby Steve Oldmeadow » Wed Aug 14, 2013 12:51 am

It is very common in Unity development to use a pool manager to avoid garbage collection spikes. Has any thought been given to using C2D4U with pool managers? For example, I'd love to see the CrayonBall demo rewritten to work with pooled objects rather than instantiate and destroy.
Steve Oldmeadow
 
Posts: 17
Joined: Tue Jul 16, 2013 10:27 pm

Re: Using Chipmunk2d for Unity objects with a pool manager

Postby slembcke » Wed Aug 14, 2013 10:47 am

RANT WARNING: So my opinion of pooling as a optimization technique is pretty low. I do use it internally in Chipmunk in a few places not to avoid memory management costs, but to make things more cache friendly. It's one of the very last optimizations I usually try because it's generally minimally effective and maximally difficult. It's very easy to make really bizarre bugs due to the object lifecycle complications.

In Unity it's even more problematic since components cannot exist in a valid state in memory if they aren't in existence in a scene, and Unity's object lifecycle for components is already overly complicated. Since you can't move components around once they are created, you need to pool things at the GameObject level which makes it even a little more complicated when multiple MonoBehaviours are involved. You should be able to deactivate/activate entire GameObjects okay, but I still wouldn't. ;)

GC is an inevitable cost of using Unity in my mind. I feel like it's less work to use a different engine/framework that doesn't have that problem than to deal with that in Unity.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Using Chipmunk2d for Unity objects with a pool manager

Postby Steve Oldmeadow » Wed Aug 14, 2013 10:11 pm

@Scott - I understand your opinion but when Unity themselves tell people to use object pooling http://docs.unity3d.com/Documentation/M ... ement.html (near the bottom) then you have to expect that some customers will want to do it. In that document they mention using pooling for projectiles and that is one of my use cases, I'd also like to be able to use Chipmunk for some particle effects too.

There are also several 3rd party pooling libraries and PoolManager is currently number 1 in the scripting charts and the second most popular asset in the current "Madness" sale.

AFAIK all the pool managers work by having a pool manager object in the scene graph that instantiates prefabs and keeps inactive objects as children i.e. they are simply activating and deactivating game objects as you suggested. Chipmunk2D seems to work in this context but I haven't tested it extensively.

It is something I want to do so I'll plug away and see how I go. If anybody else is interested in getting Chipmunk2D to work with a pool manager please speak up.
Steve Oldmeadow
 
Posts: 17
Joined: Tue Jul 16, 2013 10:27 pm

Re: Using Chipmunk2d for Unity objects with a pool manager

Postby Steve Oldmeadow » Thu Aug 15, 2013 12:25 am

I've got the CrayonBall demo working with PoolManager. I've attached a Unity package with the modified code, it doesn't include PoolManager or Chipmunk2D (for obvious reasons) so you need those two packages installed for it to work.

The main changes were to move stuff from Awake into an Initialise method so that it can be called when an object is spawned and calling Chipmunk.UpdatedTransform when the object is spawned as per the code below.

Code: Select all
public void OnSpawned() {
   this.Initialise();
   despawned = false;   
   ChipmunkBody body = GetComponent<ChipmunkBody>();
   if (body != null) {
      Chipmunk.UpdatedTransform(this.gameObject);
   }
}   
Attachments
C2D4U_CrayonBallWithPoolManager.unitypackage.zip
(41.03 KiB) Downloaded 313 times
Steve Oldmeadow
 
Posts: 17
Joined: Tue Jul 16, 2013 10:27 pm


Return to Chipmunk2D for Unity

Who is online

Users browsing this forum: No registered users and 1 guest