# How do I handle moving platforms in a platform game?

I don't want to use box2d or anything, I just want simple moving platforms (up-down, left-right, diagonal and circular paths). My question is to handle the update order / control hierarchy so that the player stays fixed to the platform solidly, without bouncing or wobbling?

-

You might fix the player position not "absolute in the level" but "relative to the platform", then have a function that gives his actual position. I don't like this though.

It would be probably better (and simpler) to bind the player to the platform so that when the platform moves +x/+y, the player moves as much.

-

My solution would be to treat every physical entity in the game map as a tree node, and the map itself as the root node. Nodes can contain other nodes. Every node is contained (directly or indirectly) inside the map node.

For example, let's say your character is standing on a platform. The platform's node contains the character's node. So, whenever the platform moves, the nodes it contains (in this case the character) will move too.

For this to happen, every node should have an (x,y) position relative to the parent node. For example, your character could be located at (1,0) relative to the platform, which itself could be located at (10,10) relative to the game map. That way, if the platform moves, your character will move along implicitly.

In this hierarchical tree, the position of every node is equal to the sum of his position plus each of his parent nodes' positions. Let's say we want to get the character's position on the map. We calculate:

(character's position) + (parent node's position) + (root node position) = (1,0) + (10,10) + (0,0) = (11,0)

OK, so all this time your character has been standing on the platform. What about jumping? Well, the moment he jumps, you detach him from the platform and attach him to the node containing the platform; in this case, the map (root node).

When he lands again, attach him to whatever node he landed on.

-
I would also use the x and y speed from the node to affect the player. Example 1: platform moves really fast up and stops, player has upward momentum and is launched. Example 2: platform is moving down slightly slower than the player falling at a deadly speed. The platform should be safe to land on instead of SPLUT – AttackingHobo Sep 20 '10 at 17:46
that's a interesting approach but seems quite a full-on change to my engine just to allow moving platorms. Maybe if I create objectOnPlatform as a component that I can to any object that just temporally gives this effect? – Iain Sep 21 '10 at 10:54

I would suggest treating this as a pseudo-physics problem and solve it by using ground friction.

You're already using gravity to hold the character on the ground, this handles the situations where platforms move up and down. To handle the left and right movement, the ground should exert a great deal of friction on the character. Typically this friction level would be 1.0, or 100%.

If the character is standing still, then their velocity is 0, and they instead inherit 100% of the velocity of the platform because of the friction. Something like the following:

``````character_velocity = friction * platform_velocity
``````

The benefit of this approach is that it easily lends itself to additional platforming mechanics, like being able to overcome the friction on steep slopes, allowing your character to slide. It also allows you to easily reduce the friction to create ice that the character will slide on.

It also is easier to deal with issues like low hanging walls that can knock your player off a moving platform. If you simply treat the character as a child of a parent platform, there's a good chance it won't interact very easily with the rest of your collision detection without a lot of work.

-