# How can I make a character stand on slopes?

First of all, note that I want to understand the principle, so I would prefer explanations in plain english (but of course I have nothing against some code to complement these explanations).

I am wondering what is the way to make a character step correctly on a slope, let's start with two screenshots of the game Limbo to show you what I mean :
So, as you can see on these pictures, it looks like the spine of the character is actually perpendicular to the horizon and not to the surface of the ground and this observation leads me to my question:
How can you reproduce such a behavior with Sprite Kit ? Because if I attach a physic body to a node, I have the possibility to let it rotate, so I would get something like this :

If I disable the rotation I would get something more like this :
As you can see these ways to do both present important issues and doesn't looks natural at all. So how should I do to make it look natural ? I thought to create several sprites and several physics bodies and then attaching them all together with joints I don't think that it will look natural because if you look at this :
Although I can physically link these nodes, how will I do to make them look real graphically ? Because in the reality, for example, the thigh and calf are linked by a knee...

Any ideas ?

• Try standing on a slope yourself and see how that goes. When you put one leg in front of the other then the leg higher up has to bend to compensate for the slope. So your approach with multiple joints is a good option. – Eejin Jun 22 '14 at 9:40
• @Eejin Ok thanks, and what about the graphical realism at the level of the joints ? What can I do link them naturally ? To not make it look like there are several nodes ? – Trevör Jun 22 '14 at 11:39
• It depends on some aspects of your game. Of course well drawn body parts help. Could you give a bit more information about sizes and how important perfect detail is? – Eejin Jun 22 '14 at 15:28
• If your ground is always, say, tall grass, just using the low point of the sprite might be okay since the characters legs will be obscured to the point where you couldn't tell how they were standing. – jzx Jun 23 '14 at 17:05
• I don't know about SpriteKit, but a lot of games represent the player as a "capsule" or "pill" shape rather than a rectangle. Also, the physics object is usually smaller than the sprite itself. – Thane Brimhall Jun 24 '14 at 14:07

To expand on Josh Petrie's answer, finding the angle of the foot is also pretty straightforward.

The angle of the feet is equal to the angle of the inclined surface. Since we conveniently have two points on that slope, we can find the angle of inclination (it's also possible to use two corners of the slope's triangular shape, but gradual slopes would become over complicated; for rolling hills, this is the best solution). Ultimately the slope will be H/P (assuming the heel of the bent leg is directly beneath the corresponding hip), or arctan(H/P) rad. Using cosines, one can easily find that H = (F+T) - (Fcosθ + Tcosφ).

The angle of the foot becomes arctan((F(1-cosθ) + T(1-cosφ)) / P)

PS: If you want to REALLY get into, you could also find the added width to P.

PSS: The foot will only bend to far up, so keep that in mind before you try to climb an 80 degree slope.

PSSS: People don't climb hills on their heels, but they do rest on them. Keeping the foot perfectly flat up the hill could somewhat accurately depict the player standing on the balls of their toes, and allowing the heel to sink to the ground while idling could make for some very realistic movement.

There are simple solutions and tricks you can use (for example, if your slopes will all be a fixed angle, just author additional sprites for sloped movement and idle, provide a mechanism for your game code to know if you are on sloped surface, and render accordingly -- this is usually what older sprite-based games did).

A general solution, expanding on your multi-jointed sprite idea, is to use inverse kinematics to solve for the placement of a characters feet (see also).

This involves defining a logical skeletal structure for your characters, represented visually by several distinct sprites (for the torso, as many leg parts as you need, and foot). The end-effector in this system is the character's foot position on the slope, which can be computed from the (fixed) torso position and some idea of which foot is in front, based on the current walk cycle frame. Between those two known positions, an IK system can solve for the parameters to the leg joints and create a reasonable approximation of how any real biped might stabilize itself on a sloped surface.

IK is often expressed as a 3D problem, but it can be reduced to 2D space relatively easily. In 2D you don't need to futz around with nearly as many joint rotation constraints to create realistic movement as you would in 3D, which is nice.

This is the method used by many modern 2D games, especially given the trend of skeletal-based sprite compositions for characters, rather than the older technique of hand authoring every frame.