x


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.

more ▼

asked Apr 22 '10 at 08:05 PM

Squeegy gravatar image

Squeegy
250 8 10 21

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

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.

more ▼

answered Apr 22 '10 at 08:49 PM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

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.

Apr 22 '10 at 09:48 PM duck ♦♦

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.

Apr 23 '10 at 01:31 AM Squeegy

@Squeegy: Could be, because any test I make has physics clearly winning the speed race.

Apr 23 '10 at 02:00 AM Eric5h5

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.

Apr 23 '10 at 08:25 PM Squeegy

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.

Aug 09 '12 at 03:24 PM agamedesigner
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Dec 07 '11 at 10:58 PM

jon.creighton gravatar image

jon.creighton
16

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 22 '10 at 08:43 PM

duck gravatar image

duck ♦♦
41.4k 95 152 415

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 22 '10 at 08:44 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

30 ships + 90 projectiles on an iPhone. Does that count as "a lot"?

Apr 22 '10 at 09:06 PM Squeegy
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Dec 07 '11 at 10:58 PM

jon.creighton gravatar image

jon.creighton
16

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2584
x1947
x1862

asked: Apr 22 '10 at 08:05 PM

Seen: 5306 times

Last Updated: Aug 10 '12 at 06:10 PM