Simulating Spiders

March 5, 2014 by Terry

Spoilers ahead. If you do not want to learn about some of the more revolting monsters in Retrobooster, stop reading now.

Cave-flyers commonly have stationary and flying enemies but not enemies that crawl along surfaces. These are probably left out because they are a lot of extra work, but they allow for fun new enemy types that give Retrobooster much of its flavor. Before starting on this game I had a recurring dream about a level where you could blast enormous alien spiders with your tiny thrust ship. The game has evolved a lot since then, but this initial inspiration remains. The most important part of the dream was that these spiders would be, unlike common video game spiders, actually creepy. This article describes how I simulated spiders for maximum creepiness.

Spiders need two main components to move about in a convincing manner: a way to navigate terrain and quality leg animation. You can see both in effect in this video. Note that all of this works in 3D despite the gameplay existing in a 2D plane.

Method

Studying some videos of spiders and insects walking, the first thing I noticed is their efficient motion. Their bodies rarely bob up and down like carefully animated cartoon characters–they glide smoothly over surfaces leaving all the intricate motion to the legs. This would be even more true of a spider the size of a spaceship because its body would have too much weight to throw around unnecessarily. Bobbing around like a cartoon character also diminishes creepiness. What this all means is that the body can be animated on its own without involving the physics of the legs. This is fortunate because it breaks apart and simplifies the whole problem.

Following terrain

Moving the spider body along the terrain actually became one of the most difficult programming problems in Retrobooster. I wanted to generalize the physics of the game as much as possible because the realistic physics is an important part of the interactivity and game feel. For spiders, generalized physics requires not simply repositioning the body each frame but applying forces to it based on the distance to and speed of surrounding terrain. (I mention speed of terrain because sometimes spiders encounter moving obstacles, and they need to walk on these surfaces as naturally as they would stationary terrain.) All this work allows spiders to realistically bump into other entities and crawl all over one other. You can even get underneath small ones with the player ship and push them into the air.

It took a great deal of iteration to make an acceptable algorithm for walking on terrain. The final solution is generalized so that it can be used for multiple types of walking entities by setting a few parameters. The first step is to search for nearby terrain using segments extending from the center of the spider body. The segments are changed each frame in a pseudorandom pattern to slightly randomize the final forces on the spider body, which does a lot to prevent it from getting stuck in concavities and help it find its way around convexities.

The segment intersection points and their accompanying surface normals are used to determine the average position of the nearest terrain and its slope. A force is applied to accelerate the body toward a desired distance from the terrain, and another force is applied to accelerate the body toward a desired speed across the terrain. Depending on the type of creature and its mass, a desired speed and force multipliers can be chosen as input to the walking algorithm. For small spiders, the forces are strong enough that they effortlessly climb up walls against the pull of gravity. Larger spiders and some other creatures are made to look like this is a struggle.

The final algorithm does a reasonable job of following terrain. Spiders and other creatures still make mistakes sometimes and fall off when trying to climb around sharp points on the terrain. This happened much too often with early versions of the terrain following algorithm. Once I reduced this problem to a minimum it stopped bothering me because it adds some variety and realism to what appears to be a difficult climbing task.

Animating the legs

A long-standing problem with video game animation lies in the disconnection between animal legs and the terrain beneath them. This is caused when animation is done entirely with art software and the actual game terrain is ignored. In the worst cases, animals plant their feet firmly in the air or underground. And when these animals turn, their legs slide along the ground as if they were wearing Teflon shoes. This type of lazy animation absolutely kills creepiness. The spider legs in Retrobooster are animated algorithmically to accurately connect them with the terrain.

I made two assumptions at the start which both worked out well. First, nobody would notice if the spider legs passed through one another sometimes, allowing me to ignore leg-leg collisions. Second, my spider legs would only have one knee joint and one hip joint. Actual spiders that I observed have three knees, but keeping it to one simplifies the math and cuts the number of leg transforms in half. I modeled each leg half as two leg quarters with a slightly bent static joint, which successfully gives the illusion that there are three joints in each leg. I was initially concerned that a hefty swarm of spiders would put so many transforms on the screen that only the most powerful graphics cards would be able to cope. This may have been true ten years ago, but it is not now. Even modest PC graphics cards handle all these transforms with ease, so I was not forced to explore other options such as animating the legs with vertex shaders.

In my opinion, the creepiest spiders pick up their feet quickly and set them down gently. (I’m throwing out words like “knee” and “feet” as if spiders actually have these. An arachnologist would probably hate reading this.) To simulate this motion, you can solve for the coefficients of a simple polynomial where t is the interpolater in the range {0.0, 1.0} and x is the resulting position.

x = at3 + bt2 + ct + d

The derivative of this polynomial where dx is the velocity:

dx = 3at2 +2bt + c

You have a position and velocity at the start of the step (t=0) and at the end of the step (t=1). If you plug them into this polynomial and its derivative you get four equations and you can solve for the polynomial’s four coefficients. The final function is one-dimensional, and it is used in triples to account for the feet moving in three dimensions.


// p = s at t = 0
// dp = ds at t = 0
// q = s at t = 1
// dq = ds at t = 1
float interpolate(float p, float dp, float q, float dq, float t){
   float a = 2.0f * (p - q) + dp + dq;
   float b = (dq - 3.0f * a - dp) * 0.5f;
   float t2 = t * t;
   float t3 = t2 * t;
   return (a * t3) + (b * t2) + (dp * t) + p;
}

A new foot position is calculated each frame during a step, and inverse kinematics is used to compute the leg transforms. The step speeds vary slightly from one spider to the next and are slower for larger spiders. This gives each creature a little character.

When at rest, a spider foot matches the velocity of the surface on which it is resting. This part of the animation could be better because the velocity is only given to the foot at the moment it is planted, but the surface might be accelerating. If the velocity were updated more often there would be less chance of anyone noticing a spider leg floating above or sinking into a surface. In practice, most surfaces are stationary and most spiders are small enough on the screen that a player will probably not notice this.

Each leg operates almost independently. When a foot is moved a specified distance away from its hip, the leg automatically takes a step. With each step the foot reaches for a point ahead of the spider body. This effect really shines when a spider turns to move a different direction. Each leg takes the appropriate number of steps, bringing the creature to life.

The legs cannot be left to move completely independent of one another or they might end up all taking steps at the same time. To prevent this there is a little code that biases the speed of each step, keeping all of the legs out of sync. The legs are purely cosmetic and do not affect the movement of the spider body, but they appear to give it good support as long as a few of them are on the ground at all times.

There are also many small details that add life to the simulation. When navigating rough or highly convex terrain, sometimes a leg can struggle to find a place to step. When this happens, the leg does not pause. It keeps moving as if it is trying to find a place to land a step. Similarly, when a spider falls through space its legs flail about as if it were trying to find its footing.

Stomping softly

It took too long to find the right sound effect for spider steps. I tried many different stepping and stomping sounds made from things like elephants and sand bags. In the end I decided the creepiest sound was very little at all. I just used a decent stomping sound and turned the volume way down. These creatures are supposed to appear efficient in the way they move, so they should sound efficient as well.

Making models

As for modeling, the most important compromise was already mentioned: the legs contain some fake joints that never move. If you don’t know to look for it, you would probably guess there are more animated joints than there actually are. If I had more time I would have also liked to put a joint or two in the body so that it could flex to follow terrain contours and absorb impacts with some visible squashing and bending.

The spider texture was designed to be colorful and full of contrast so that all the animation would stand out. I tried some different patterns on the legs and decided that stripes emphasized the leg movement the best. The gloss is turned all the way up to make the spiders look slimy and wet.

P1120718

I ran into a lot of these guys on Mount Tai in China. It was amazing how much they look like Retrobooster’s spiders.

Conclusion

Spiders in video games rock! The more the better, and they should be as disgusting as possible. After about a week of programming these spiders I started having nightmares about them, and I hope they give you nightmares too. My only regret is not having time to make more spider varieties, but that is not to say Retrobooster does not contain other gross creatures with legs. Bon app├ętit!

Leave a Reply