A platformer with Box2D

Jan 29, 2010 at 12:07 AM

If you've tried to make one, you've probably encountered a certain problem. It's impossible to stand on a moving object. When an object below another moves, the object above does not.


Not too difficult. When you detect that your Actor is standing on the ground, attach it to the object it is standing on. When your scripting system (or whatever) moves the object, move all the attached objects too. The only complicated part is tracking all the attachments.

Jan 29, 2010 at 12:36 AM

Cool video, are you using Kinematic bodies for the moving platforms (new way) or are you teleporting a static body (old way)?

Depending on the feel of the game (more realistic vs more platformy) you could also get away with using friction or even temporary joints, but I haven't experimented too much with that yet.

One example would be an elevator that moves up and down quickly, it could even move down faster than gravity.  Realistically, any bodies on the platform would fall with gravity and not with the elevator (so they would lift off and fall then land on the platform after it has already reached the bottom).  Non-realistically, you would need an attachment system like you mentioned.

For most of my games I tend to let the entities (or "actors") think on their own and handle their own logic.  So an elevator would know about its associated kinematic physics body and be able to set velocity when told to "go up" or "go down".  Also, the entity would be notified of any contacts that get added or removed and through that be able to track which other entities it should be "attached" to so that when it sets velocity it can apply it to those entities as well.  This keeps the logic local to the entity and makes it easier to compose larger scenes with more entities together.

Jan 29, 2010 at 3:19 AM

I'm using a static body for the platform itself in that video - but the box is a dynamic body, and so is the player, and it works if the player stands on the box as well. I don't know how a kinematic body would change things.

I described it vaguely before, I didn't mean to imply that some central place kept track of all this. The entities (More accurately, their physics component) is responsible for handling the attaching, and for updating attached entities. I haven't yet made entities detect their own motion, and move attachments in response to more than just the scripted motion. Currently strange things can happen when things on moving platforms collide with static world geometry in certain ways.

For a kinematic body, would friction cause a kinematic platform to move objects resting on it with it? If it did, that would be great. But it unfortunately wouldn't solve the problem of a dynamic body sitting on a dynamic body sitting on a kinematic platform.

I considered using a joint for it, but I'm not all that sure what joint to use, or how to detect when I need to turn it off. Does Box2D have breakable joints? A zero-axis-of-freedom joint that's easily broken might do it.

Ideally something like this would be part of the engine. (Well beyond the scope of a port) I don't intend to worry about the effects of differing friction, and how a low friction and a high velocity platform can break the attachment. But if it was done by the physics simulation itself, it could handle all those details.

Jan 29, 2010 at 5:12 AM

Joints are somewhat limited in Box2D but there is a weld joint which is the zero axis of freedom joint you were asking about.

Kinematics will apply forces to dynamic bodies and are controlled by setting their linear/angular velocity.  Friction should also apply.

As for "breakable joints", they are destroyable but they won't break of their own accord (you have to tell the joint when it should go away).

If you have a good system for doing platforms that works for you then there is no need to mess with joints/kinematics.  If it works really well and you want to donate it we could include it as a sample in the Box2D.XNA solution.  We don't have much in the way of documentation so providing samples is probably the best route for now.

Jan 29, 2010 at 1:42 PM

Haha, I wasted all that thinking solving a problem that wouldn't have existed if I had used kinematic bodies properly to begin with.