Moving platforms and platform games. I'm surprised there aren't more blog-posts about that out there, actually, it's a really tricky problem I think
Yeah, it's very interesting! In fact I felt inspired to go into more detail about how my system works. It's not perfect, and what I've written here is probably an oversimplification of the little niggles and edge cases of my code, but on the whole this is how the platforms basically work.
Platform ParentingWhen Leilani (or any other object) lands on a platform, the platform entity becomes her parent. As the parent, the platform is always updated before Leilani is, so when Leilani updates the platform has already done its movement for that frame.
When Leilani is stationary on a platform, her velocity is actually zero, even though relative to the rest of the game world she is moving. This is so the movement code can work the same both on platforms and on solid ground - if the player stops pressing buttons, Leilani decelerates to zero velocity, which means she's standing still relative to what she's standing on.
So when Leilani's entity updates, let's say her xVelocity is 5.0, and the timeStep (length of the frame) is 0.016. The Leilani entity will move to the right by (xVelocity * timeStep), which is 0.08. But, the parent platform moved a distance of 0.05 this frame (remember the parent platform is guaranteed to have been updated by this point) - this gets added to Leilani's movement too. So in total she will move a distance of 0.13.
Leilani's collisions are then handled as normal, using her total movement this frame, so the platform could carry Leilani into a wall or a platform and she'll collide with it correctly.
Parenting and Velocity Inheritance ExamplesLet's look at a couple of examples, using debug info to see velocities!
Here we can see orange boxes which outline each entity. (Ignore the jiggly box hovering around Leilani, this entity handles the bubble sprite for the water powerup.) The yellow line drawn down the middle of Leilani's box indicates when she is standing on the platform, at this point the platform is her parent object.
1. Initial she's standing still. Her velocity is zero.
2. She jumps - so the platform is no longer her parent. At the moment of jumping she inherits the platform's velocity of 1.88, so she moves at the same speed through the air. In this game there's no automatic mid-air deceleration.
3. She lands on the platform again. At the moment of contact, the platform's velocity is taken away from hers - resulting in her returning to zero.
4. She can run at max speed (5.00) on the platform.
5. When she runs off the edge, she loses contact with the platform, and once again inherits the platform's velocity. Now she's moving at 6.88, faster than normal!
6. Once she lands on the floor, she can't maintain the running speed, and decelerates back to 5.00.
7. I let go of the controls, she decelerates to 0.
This example features some rolling!
1. Max rolling speed is 7.50.
2. When Leilani rolls into a wall, she slides down it. Notice at this point the horizontal yellow line indicates that the platform is her parent, but she's colliding with the side of it, not standing on top of it. For most purposes this doesn't make any difference to standing on top of a platform; the relative velocity and movement works the same.
3. While sliding her xVelocity is 1.0, this is just to ensure that she stays safely stuck to the wall.
4. She jumps off the wall. Normally this would set her velocity to -7.50, her maximum rolling speed. However, the platform's velocity of 1.88 is taken off, so she travels more slowly.
5. She slides down the opposite wall, then jumps off again. This time, yep, she travels faster because of the velocity inherited from the platform on the left. New speed is 9.38.
6. She lands on top of the platform on the right. Her velocity reduces to 7.50, the normal max rolling speed, because that's the speed she's moving relative to the platforms.
7. Finally she inherits velocity again when leaving the platform and lands on the floor. This time, because she's rolling, she can maintain a higher-than-maximum speed. One of the advantages of rolling
So when Leilani inherits velocity from a platform, or loses velocity when landing on a platform, she's not actually moving any faster or slower - it's more like it's converting between world-relative and platform-relative velocity.