- Home /
RigidBody/Collider physics, or simple script?
I'm making a game that, gameplay wise, is completely 2D. A plane with 3D models on it. It's set in space with inertial physics.
My physics needs are pretty simple Newtonian stuff. An object with some mass value is applied a force of (x,y)
. And all objects are considered circular, so if 2 objects are within a distance of a.radius + b.radius
, then a collision occurs, forces are applied to both objects and some logic is triggered.
So my question is wether it's worth using RigidBody
or the various collider components or to roll my own scripted solution for these physics. It seems like RigidBody
is overkill. I don't need gravity, or center of mass, or friction or 3D polygon collision detection. I just need proximity based collision detection and 2D billiard ball style newtonian physics.
And I am betting that Unity physics and colliders would make this easy, but I would pay extra for them in performance due to all the features I'm not actually using on these components that it is processing anyway.
Answer by Eric5h5 · Apr 22, 2010 at 08:49 PM
The built-in physics are better-optimized than what you can do in scripting. Just moving an object in a straight line is 30% faster with a rigidbody and AddForce vs. using Translate() in FixedUpdate, and that's obviously without any collision detection code or anything else. Adding collision and other features makes the physics engine increasingly faster by comparison.
Most physics features that you're not using aren't applied anyway...friction only matters if two colliders are in constant contact, so that would never be calculated in the first place. 3D polygon collision detection only applies if you're using a mesh collider, which you wouldn't be doing anyway, you'd be using sphere colliders. There's no reason not to use the built-in physics, unless you need specific behavior that those physics can't provide.
Nice answer, good to hear - and I guess this settles the speculation about whether a full 2d engine like box2d would outperform unity's physics constrained to 2d.
So I converted my game objects to rigid bodies, and changed my manual accel/vel/pos code to use AddForce
and friends and I went from 80FPS to 40 in my test scene with 1000 objects. So I think I'm going to handle my own physics but use colliders to trigger events. It's also possible that my code just wasn't too efficient since I don't know Rigidbodies very well.
@Squeegy: Could be, because any test I make has physics clearly winning the speed race.
After more experimenting, I found you are 100% right. With simple AddForce
stuff, the difference isn't so clear, but when even the simplest collision detection is added in, then Unity's physics seems to be orders of magnitude faster than by scripts could ever do.
Eric, looking at your answer I was thinking that maybe it would be more efficient for me in my game to replace a CharacterController with some kind of rigidbody setup. But I'm not sure if that would behave properly. So what do you think would be the best and most efficient alternative to a CharacterController? I'm on iOS Pro, btw.
Answer by jon.creighton · Dec 07, 2011 at 10:58 PM
This developer has done some comparisons of Box2D running in Cocos2D and PhysX running in Unity and found that for the same 2D test scene Box2D is roughly twice as fast as PhysX.
http://flyclops.com/battle-of-the-ios-physics-engines-197#comment-114
Answer by duck · Apr 22, 2010 at 08:43 PM
I've often wondered about this comparison too. I've seen a lot of 2d physics games which use the 3d engine constrained to 2 dimensions, but I've never seen any performance comparisons which weigh that up against using a scripted 2d-only physics engine.
I would say if your game is genuinely only 2d circle-circle collisions & responses, a roll-your-own approach could possibly perform better. This is a guess though, and would largely depend on how you implemented your 'early-out' techniques for limiting the number of distance tests (eg, by using a large 2d grid of 'bins').
If you were to also require other arbitrary 2d shapes for collision, I'd be much less confident, but like I said, I've never seen the two methods compared for performance side-by-side. I'd be very interested to see your results.
Answer by e-bonneville · Apr 22, 2010 at 08:44 PM
Well, performance isn't a huge issue, unless you're planning on having lots of gameObjects. So, yes, it's worth having colliders. Rigidbodies are also good, and turning off settings doesn't really effect performance, as I said before, unless you're working with large amounts of objects.
In fact, you can disable all the settings and work with rigidbody.AddForce
to simulate gravity, and add forces on collision, which you can figure out using collision info and the connected rigidbody.
Also, scripting your own physics isn't worth the time when you have the Ageia PhysX physics engine at your fingertips.
30 ships + 90 projectiles on an iPhone. Does that count as "a lot"?
Answer by jon.creighton · Dec 07, 2011 at 10:58 PM
This developer has done some comparisons of Box2D running in Cocos2D and PhysX running in Unity and found that for the same 2D test scene Box2D is roughly twice as fast as PhysX.
http://flyclops.com/battle-of-the-ios-physics-engines-197#comment-114