1
\$\begingroup\$

I'm planning on creating a 2D platformer and doing some research to make sure that when I get started, I avoid making some poor decisions. Something I've seen come up multiple times is that I should consider writing my own physics for a 2D game instead of using the physics provided by an engine. For example, in this question, multiple answerers tell the OP to write their own character controller logic instead of using the physics built into Unity.

Why is this advice common? What benefit would I gain by writing my own physics code? And how do I decide whether writing my own physics is appropriate for my game?

\$\endgroup\$
  • \$\begingroup\$ Maybe you want to reword your question. Right now it sounds more like "Suggest me what should I choose" instead of "When we compare A and B what are the key differences". And even then, this will be opinion-based / where-to-start kind of question I'm afraid. \$\endgroup\$ – Kromster Feb 15 '16 at 4:59
  • 1
    \$\begingroup\$ You should write your own character controller instead of using engine physics only for the player controlled character. This provides better feeling controls, as the player doesn't want their avatar to be affected by real physics (unless that's the game's theme). \$\endgroup\$ – Evorlor Feb 16 '16 at 13:22
1
\$\begingroup\$

Using an off-the-shelf physics engine for unrealistic characters often leads to situations where you have to fight the system just to do things that would otherwise be simple. Here are two quick examples:

  1. Your character is probably best represented as a rectangle, however, rectangles do not slide along the ground smoothly, and rectangles do not remain axis-aligned on an incline, so characters in a rigid body physics engine are frequently represented as a rectangle sitting on top of a circular wheel that acts as the "feet" of the character. The circle is less effected by friction, but it's also not prone to "catching" on edges like rectangles are, and it can maintain even contact on an incline. In ad-hoc physics, you just make the character a rectangle and forget about it, because you aren't bound by the rules of physical reality.

  2. Thinking in terms of force: objects in motion tend to remain in motion. If you apply forces to set something in motion, you'll have to apply forces to curtail that motion. I.e. if you push a character forward to start them moving, you'll also have to apply a braking force to stop them. For the precision of a 2D platformer, working in terms of velocities is much more direct and flexible.

That said, most physics engines will allow you to opt-out of realistic simulation, you just lose the bulk of the purpose of the physics engine by doing that, apart from collision detection.

Ad-hoc 2D physics for a platformer is fairly basic, and is probably likely to provide a "tighter" end result. However, if any objects in your game do require realistic physical simulation, e.g. you want to throw a crate at the player and you want it to bounce and roll along the ground, I wouldn't recommend even trying to do that yourself. Just manage your player physics with your own solution, and other world objects with a "real" physics engine.

\$\endgroup\$
2
\$\begingroup\$

If you want to have unrealistic physics ( like in question you mentioned ) or you need to use relativity theory, then you should write your own physics engine, because most of public engines aim to deliver realistic Newtonian physics experience. And if you want to have normal, realistic physics, then using physics engine will be much easier than writing your own code.

\$\endgroup\$
  • \$\begingroup\$ It will always be easier. If you want unrealistic physics you can just use some very off values, like a gravity of 2, or a very high bounciness etc.... \$\endgroup\$ – KaareZ Feb 15 '16 at 12:45
0
\$\begingroup\$

You're likely going to want to write your own collision and physics for any type of 2D platformer, even if resembles real life. Otherwise you're going to need to apply phantom forces, or jack up friction in weird spots which usually ends up with hacky code.

That said, I suggest using a physics engine for any cosmetic stuff, like debris chunks in explosions, or if a table gets flipped in the background, etc.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.