# 2D Physics Engine for Platform Games - Riding on a Platform

I have a number of questions relating to using a 2D physics engine in a platform game, but one main one that I'm grappling with at the moment. I managed to somewhat integrate the Physics2D.Net engine into a Scrolling Game Development Kit 2 project. I'm very pleased with the performance and the realism of the physics. However, I've lost some things that my original SGDK2 physics engine (oriented more specifically to platform games) made easier:

1. Riding on a platform is very difficult now. This is my main question. It's not difficult to implement, but difficult to play. Assuming I even do manage to get the player onto a platform (which is hard enough in itself), as soon as the platform moves, the player falls off because there's not enough friction between the player and the platform or something. Do I just need to tweak things like friction, inertia, linear velocity dampening and the like to make it easier to ride on a platform, or is there something fundamentally difficult about riding on platform is the "real" world? I've tried reducing the force applied to the platform that makes it move (so it would allow more time for the rider to match the platform's velocity), but then that's not enough force to keep the rider aloft, and the platform sinks to the ground. I'm simply applying forces to move the platform.
2. What is the best way to stop the platform from rotating? I've tried setting the Angular momentum and the angular position to 0 on each frame, but this can result in little twitches that cause anything on the platform to spring into the air a bit, just long enough to fall off.
3. Running along the ground: How do most platform games with realistic 2D physics implement the player? My player is currently a square, but will often tumble and spring into the air due to friction hitting one of the corners. My guess is that the player should be a circle and always drawn upright. Is this standard practice, or is there another way? If I use a circle I expect that riding a platform will be even more difficult! The player will roll right off. I'm thinking of Little Big Planet, which also seemed to keep the player on the ground pretty well. Do I just have to put a lot of velocity damping on the player, or something like that, if I want to keep them grounded too?
4. Since SGDK2 is a tile-based engine/IDE, I have writen code that analyzes all the shapes of the terrain/tiles and turns them into physics bodies with infinite mass (merging them horizontally, but not vertically where appropriate; merging two directions was too difficult for me to figure out because I couldn't figure out how to trace and represent the hole in a torus-like formation). Is that a good way for a platform game to implement something for the sprites to interact with to represent the solid ground? Am I better off turning each tile into a physics body (few simple shapes, many bodies and internal surfaces between tiles), or merging them as I am doing (fewer bodies, but more shapes that are more complex)?
5. I'm having a hard time figuring out the optimal vertex subdivision level and grid spacing to use. I don't really understand these concepts and documentation on Pysics2D.Net is sparse to non-existent as far as I can tell. I am generally subdividing to about 6 and using a grid spacing of about the same. My sprites and tiles are mostly 32x32 units (pixels), all square for the moment.
-
Does your game require such specific physics interactions? Many games that don't (and I venture that nearly every 2D platformer doesn't) will use a very much more simplified physics model for characters and terrain. – chaosTechnician Aug 1 '11 at 15:38
According to another question (Good 2D Platformer Physics) using a proper physics engine is a painful way to go for platformers. You're much better off using simplistic physics that does exactly what you need and no more, like chaosTechnician said. Tetrad suggests this in his answer and the other answers only seem to support his claim. – doppelgreener Aug 2 '11 at 5:50
– bummzack Aug 2 '11 at 7:10
I'm not designing a specific game, but experimenting with updating my engine/IDE to support more realistic physics. One reason I am exploring this is that I have had difficulty specifically with allowing the player to properly push bunches of objects around, and combine that with the fact that real physics just looks so cool :). I want to play with it. – BlueMonkMN Aug 2 '11 at 12:38
@bummzack having created my own simplistic platform physics implementation in my crude physics engine, I'm quite familiar with the notion that moving the plaftorm by setting its position won't work because it doesn't apply any forces. That's why I tried to treat and move the platform as a normal physics object, with forces. – BlueMonkMN Aug 2 '11 at 13:11

1) Friction is the correct solution. It's what keeps you on a moving thing in real life. Once the person has accelerated to the same speed as the platform, inertia will keep them on it. If a platform moves down and sideways at the same time, a player might still appear to 'float off' if they don't fall fast enough to keep up with the platform.

1.5 and 2) The platform should (probably) be a kinematic body. As one, it won't respond to collisions, so it will never rotate, and the player won't be able to push it to the ground. If your physics library doesn't support kinematic bodies, it might have angular inertia, which can be set to an absurdly high value.

3) I've never done this with tiles, but I have with worlds that had little corners the player could get stuck on. My solution was to make the player out of two shapes, a circular 'foot' and a rectangular body. The round foot slid easily over small corners and up slopes.

4) You're probably better off with fewer, larger shapes. Depending on the library, it may perform better with slightly more convex shapes rather than fewer concave ones. A torus is impossible if you use only convex shapes.

5) This sounds like a value you'll have to experiment with. Not knowing specifics of the library, I would guess that a smaller value trades performance for accuracy.

-
I'm relatively new to 2D physics. So I have some questions about your kinematic body suggestion. I think Physics2D.Net has the ability to handle kinematic bodies in that there is a property "IgnoresCollisionResponse" which implements collision detection, but does not react to it, and "IsCollidable" which bypasses collision detection entirely. But then I lose the ability of the player to react to the platform too. I wonder if I set the mass to infinite if I would still be able to move the platform and have the player react or if that would launch the player into outer space. – BlueMonkMN Aug 2 '11 at 12:46
On point number 3, does the circular foot rotate or is it rigidly attached to the body? I assume you also had to do something to prevent the player from rotating. I wonder if Physics2D.Net has some angular inertia setting like you suggest; I haven't seen one. Although there is something called AngularDamping. I wonder if that would prevent it from rotating entirely, or if it would allow instantaneous forces to affect the angular position before it stops rotating. – BlueMonkMN Aug 2 '11 at 12:50
On point 4, I'm not needing to implement a torus per se, but rather a space enclosed on every side. Physics2D.Net has a function that wil automatically trace a bitmap and turn it into a polygon, but it only traces the exterior, so I couldn't use it on a tilemap that has any enclosed holes. But my tiles could always by split into convex shapes somehow (indivisual tiles are always convex). It's just very difficult for me to come up with a general mechanism to do this (combine the largest contiguous block of tiles I can into a polygon) in 2 dimensions. – BlueMonkMN Aug 2 '11 at 12:55
By the way, Physics2D.Net also has a MultiPolygon shape. I tried adding all the merged polygons to one MultiPolygon shape and that was clearly the wrong choice. It got stuck for maybe a minute during startup doing some sort of scan through the entire space occupied by all the polygons in that shape... must have been some kind of nested loop too because it took much longer than a once-over scan would have, I think. But when I added each polygon as its own shape and body, the time went down to about a second to initialize all the bodies. The actual runtime speed was fast in both cases, though. – BlueMonkMN Aug 2 '11 at 13:06
I don't know where chat is, but I think I'll cut this off here for now since I need to take some time to go experiment more given the discussion we've had. I'll mark this as the answer since it's the only response, and if I have further questions, I'll ask them separately. – BlueMonkMN Aug 3 '11 at 22:39