Remember how in Super Mario World certain platforms would move back and forth along a path, which may be straight, circular, jagged, etc. (the paths were displayed as black lines)? I'm currently trying to figure out the best way to implement this in Box2D and am looking for suggestions. I've got some ideas but none of them are easy.
Straight paths could be implemented using prismatic joints, but obviously wont work for more complex paths. Maybe a path could be represented as a list of points, connected to a platform with zero density. The path object could interpolate the discrete positions from the point list and set the position of the platform directly. I can live without curved paths.
Can anyone think of an easier way to do this?
If you want to work with a set of line segments, you can create and destroy the prismatic joint as necessary. It sounds hacky, but really it is not too hacky. For example, contact constraints are created and destroyed all the time. Fortunately Box2D does this for you automatically. 🙂
If you take this approach, I recommend defining the prismatic joint in local coordinates (in version 2.0 (SVN)). This will prevent any long term drift.
I like that approach because the body could be dynamic - it's motion wouldn't be defined by the path but rather constrained by it.
I think the edge cases could potentially be a headache though - when the body is at the point that connects two segments, which one should I use to define the prismatic joint? If the body is moving this is easy - just destroy the current one and create the new one. But if the body comes to rest at a vertex...
EDIT: I guess I could just keep switching back and forth with each time step. This would be good enough as long as there isn't any overhead to rapid creating and destroying joints.
The overhead for creating/destroying joints is small.
I see two approaches:
1. use a finite state machine for your platforms and figure out transitions to give the best stability.
2. destroy/create the joint each time step. Find the closest point on the path to the platform anchor. Use the closest point to form the prismatic joint geometry.
Question: why use physics for this motion at all? I assume these platforms are static objects and not supposed to interact with the rest of the world, the player is supposed to interact with them. Why not just set the position of the object programmatically?
I would use physics for these to:
1. Use a motor with a finite strength in case a large load can block the platform.
2. Friction won't work if you set the position directly.
3. If the platform has a velocity then bodies on the platform will receive that velocity, making the simulation smoother.
If the platform cannot be stopped, then a kinematic body would satisfy 2 and 3. Box2D does not yet have support for kinematic bodies.
A kinematic body has inifinite mass, yet moves with a user specified velocity. Kinematic bodies don't collide with static or kinematic bodies.
mos - I've actually got an implementation working that does exactly that. It uses SetOriginPosition on each timestep to move a static body. But I think there would definitely be advantages to taking the Prismatic Joint approach. For example, say the path goes downhill, and you'd like the platform to accelerate as it falls, stop if it hits an obstacle, etc..
Also, it seems that when you set the position directly it really screws up collisions with anything that's resting on the platform. When moving downwards, anything on the platforms appear to "jump" as the platform is yanked from under them. When moving upwards, it seems that the bodies overlap for a bit... causes some weird effects.
My question was just a hypothetical. Those are all good reasons to use force instead of setting the position directly. --And now that I think about it, whenever Mario is on one of those moving platforms, he stays in the same relative position (to the platform). If you just moved the platform with SetOriginPosition, you'd have to move everything that was in contact with the platform.
One possible approach is to use a body with no rotation (now called fixed rotation) and connect it to a mouse joint. Then you can drive the x, y coordinates of the mouse joint however you wish. You can also adjust the max force applied by the mouse joint to allow the platform. Getting the content of the platform to stay would require some friction to the platform object.