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 : Limbo principal character standing on a slope Limbo principal character standing on a flat ground
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 : We can see that the sprite follows the ground on which it stands, so the feet of the character are on the floor but its spine isn't perpendicular to the horizon but more to the ground

If I disable the rotation I would get something more like this : Here the spine is perpendicular to the horizon but the feet of the character doesn't touch the floor. In fact there is only a corner of the sprite that touches the floor
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 : enter image description here
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 ?

  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Eejin Jun 22 '14 at 9:40
  • \$\begingroup\$ @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 ? \$\endgroup\$ – Trevör Jun 22 '14 at 11:39
  • \$\begingroup\$ 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? \$\endgroup\$ – Eejin Jun 22 '14 at 15:28
  • \$\begingroup\$ 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. \$\endgroup\$ – jzx Jun 23 '14 at 17:05
  • \$\begingroup\$ 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. \$\endgroup\$ – 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.

enter image description here

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.

| improve this answer | |

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.