Strange Platformer Bug

Hey all,

I've been working on the beginnings of a platform game in XNA using Box2D, and I can't work out what's happening here. I'll give a summary of my setup, and also let you see this video. The video's been cropped down from a 1280x720 game area.

The video is a SWF because I used Flash to quickly crop it - exporting as a video would make it worse quality again, so:
http://www.swfcabin.com/open/1277231066

For moving platforms/objects, I'm making them static bodies and moving at linear unchanging speeds with body.SetTransform. I do this because I searched around for ages on the best way to have moving platforms (which will probably only ever interact with the player, but it'd be nice to be able to have falling boxes etc. in future).

All of my moving platforms work off a Vehicle system - all platforms decide when mobile actors become 'passengers' on them. Then, they update all passengers accordingly - these moving platforms just move their passenger along at the same speed as them. How I'm doing that isn't the issue at the moment.

To move the player character I use body.ApplyForce. This same issue occurs when I use ApplyImpulse too. I don't think I can use SetLinearVelocity, it just wasn't as useful or as natural in several ways.

My issue occurs when two of these moving platforms are 'stacked up' vertically. Note that they're not interacting with each other, and one isn't resting on the other, they just happen to be close to each other. I've checked, and they're always at the same horizontal position.

When the player character jumps up as the platforms are moving towards and into the player, the player comes down to land on the lower platform, until it starts to move back in the other direction. So it's like the static (but moving) platforms' positions are being updated first, then the player is being told it's sitting on an object. But if that's the case, shouldn't it also be affected by the platform above, which would push the player far enough outside it so that it's no longer 'on top of' the lower platform?

I've tried updating the Box2D world both before and after my loop that updates all my game actors (it's in these custom actor update loops that I move moving platforms).

Does anyone have any idea why this might happen?
A couple things are going on.

First of all, collisions are based on a tolerance and there is a small, yet significant region, where bodies are considered touching. Therefore the character can touch both the side of the top box and the top of the lower box at the same time. Since you not allowing the character to rotate, it will not tumble off. You can prevent the character from resting on the lower box by using a circle for the character shape. You can use multiple shapes for the character if you want to approximate a capsule shape.

Second, you are teleporting the platforms around and Box2D does not recognize their velocity. This prevents any friction coupling between the character and the platforms. The current C++ version provides kinematic bodies for this purpose.
Thanks, very helpful answer!

As for the teleportation of the boxes, that's reminded me that they do, indeed, have no idea of velocity behind them. You mention kinetic bodies - I noticed the Kinetic body type, and I was actually using it for all my static bodies, walls and platforms etc. The bug still seemed to happen in that case, but what exactly are you saying it will do to help this matter? Or should I generally just use it for static bodies that I know I will be moving?

As for the capsule shaped player character - I've seen this occur/be used many times, and I was hoping to avoid it, but is this what you'd recommend, and most importantly, is this basically the de facto standard way to fix such a problem if a rigid body centric physics engine is being used? Won't it potentially become visible in slower games with larger scale that your player's colliding with things and being forced out by the circular capsule edges?

Thanks again.
For kinematic bodies you set the velocity and they will be moved by the solver. Calling SetTransform does not give them true velocity.

In my experience, most modern games use capsules for character collision. Older games with pixel based collision tend to use boxes.
Cool, helpful again! I can't remember why I ended up not using SetLinearVelocity for platforms. I think it could've been because I thought they'd be affected by or restricted by other game objects. I want them to always take priority. For this to happen, will I need to set the bodies' mass to anything in particular? Does BodyType.Kinetic do this?
You do not need to set the density for kinematic bodies. It will be ignored and the mass is set to zero (meaning infinite mass).

Kinematic bodies cannot be slowed (unless they are moving incredibly fast). Kinematic bodies only form contacts with dynamic bodies.
Great, this is much appreciated!
I've tried setting the moving platforms' body type to kinetic. I also started moving them using SetLinearVelocity, which stops the player from being able to land on stacked copies of the platform, at the sides, where they meet.

I have another question, though. Since the primary use of all this is for unmovable travellin blocks/platforms, the method I had for having these platforms modify the player/item on top of them was to add to their transform by the same amount as the platform is moving. However, the platforms will now be moving by having the linear velocities set, and the player will continue to move (unless I'm advised otherwise) by ApplyForce in the horizontal axis (walking left and right) and with SetLinearVelocity when jumping.

Firstly, do those sound like dodgy ways to be controlling the player, especially since I'll be having moving platforms moved in this way?

Secondly, does it sound like this is going to be safe? I'll be handling the player userdata (or any other mobile game actors, if I decided to have AI characters or if I end up having carryable or dropping boxes etc.) in the same run-through as the moving platform userdatas, which is after the Box2D World instance is updated. So, my original thought, which I'm about to try, was to have the player's walking controlled with ApplyForce, and when the moving platform 'vehicle' needs to update its passengers (the player) it takes their existing linear velocity and adds to it an amount equal to the moving platform's set velocity, by which it also moves in its own movement update method.
I would figure out your target velocity according to game rules and then use ApplyImpulse with mass * (target_velocity - actual_velocity).
Erin, that worked a treat. However, on a different note, my player character seems to be able to modify the course of the moving platforms. It happens on a per-platform basis, so if I have (and I do, in my sort of testbed at the moment) a group of 4 square moving platforms arranged in 2x2 (same type of platform as in my sample video in my original post), I'm able to nudge one I'm standing on out of sync with the others. I'm not sure if that's the exact bug, that it's the one the player is standing on, but more importantly, they're all kinetic bodies being moved by SetLinearVelocity, and the player has normal mass (I guess stating the mass is useless if it depends also on the physics scale etc.).

I also considered that it might be a problem with my logic on the moving platforms, however, I've tried a few setups with that and this same thing still happens. To make sure my moving platform doesn't get out of sync, and that all of them operate in the same way, I check for the position of the platform being past BlockWidth * BlockSpan, which is just 40 pixels width (the size of my blocks at the moment) multiplied by the number of grid spaces I want the moving platform to cover (tile based system and all that). If the platform gets past the designated X position, not only do I change the direction enum (I actually have waiting times at each end, so the states pan out as MovingRight, StoppedAtRight, MovingLeft, StoppedAtLeft, but the logic's all simple enough and I've looked at it carefully), I also set the position (via Body.SetTransform) of the moving platform to where it should be starting its journey back in the other direction. This wouldn't be an issue as the platform only stays at this point for a very short time - one tick. If I started to introduce time delays at these positions (which was the idea for other types of moving platform), of course, I might encounter that anomaly whereby the player can land on the midpoint between two platforms like in the video. That's not my concern at the moment, though!

To recap - my kinetic bodies seem to be getting affected by a rectangle-shaped player character with relatively low mass and max velocity.

Original thread: https://box2d.org/forum/viewtopic.php?t=5151