Tuesday, June 28, 2011

In the Lab


I've been working on a lot of experimental things. Many of them have been in the builds, and you might have seen some of them. Some others, I have been working on in other scenes and tests. Since I haven't set up the ability to play with previous builds (still coming), I figured I should give a quick run down of some of these experiments.

On the ball


Probably the most difficult challenge so far has been to get the endless spherical world working. I started by creating the obvious. I made a sphere and the built in sphere physics collider, and put the truck on it. That was an absolutely spectacular failure. The truck would go shooting off in random directions, jiggle violently in place, or fall trough the planet (sphere). Often it would do all three. I figured that this wouldn't be adequate to the task, but it never hurts to try for very simple straightforward solutions.

Next I tried setting up a sort of artificial gravity. The physics engine has a built in gravity that applies a force of 9.8 newtons along the negative y vector. Simply put, stuff falls down. I needed stuff to fall toward the center of the planet. Lucky for me a lot of other people over at the unity forums had done a bunch of that work, so I only needed to modify it. Once I adapted some of the other artificial gravity systems and simplified them for my needs, it actually wound up being one line of code. The object with gravity applied to it simply looks for the center of the planet and then moves toward that with 9.8 newtons of force. Done.

So the artificial gravity worked fine, but stuff would still keep flying off the sphere collider, and would occasionally end up in orbit around the planet. Literally. I could hit a garbage can with enough force that it would fly off the planet ending up in a perpetual free fall. It was really funny, and I might use it for something later on, but it meant that, given enough speed, the truck would also leave the surface of the planet, and that was pretty much the end of the fun. The truck would also freak out pretty regularly and usually fall through the planet The main culprit turned out to be the sphere collider. For any math nerds out there, this is probably pretty obvious. A sphere is not a really a stable shape to set things down on. So I ditched the sphere.

The next obvious road was to create a mesh collider. The physics engine and the graphics work off of separate models. The physics engine really only works with very simple objects. The graphics on the other hand can be very complex with little to no slowdown on most devices. For every object that you want to react to gravity or collisions, you will need both a physics and a display model. Using a mesh collider, you can effectively, use the same mesh for both, if the display model is simple enough. Making a decent looking sphere requires a pretty dense and complicated mesh. So that was a no go.

The best collision objects in the engine tend to be cubes or boxes. So I hit on an idea that worked out really well. I figured, why not have the truck just bring the ground along with it. I made a box that's a little larger than the truck and placed it just under the wheels. Then I made a script that told the box to follow the truck around  so no matter where the truck drove in the world, it would bring its piece of ground with it. On a flat surface it worked fantastically. I went about adapting my artificial gravity code to make the collider rest on the surface of the planet sphere and rotate smoothly around the center of the planet to stay positioned under the truck. That also worked very well, but caused some other problems with the camera flipping over and the the controls would sometimes jank out when the truck passed over the poles due to an unfortunate side effect of Euler angle math called gimbal lock. Here is one of the best explanations I've ever seen of gimbal lock.


So I could go back and change all the code I had in place for making the truck move around, or I could change the rules of the game world. So I did that. Instead of driving the truck around a ball that sits stationary in space, the truck drives around on a flat XZ plane and the planet follows around underneath it rotating in the opposite direction. And the rotations are all using quaternion math, so gimbal lock won't be a problem. Any object that doesn't require player input, like the garbage cans and blocks aren't affected by gimbal lock, so they just roll around on their own little ground colliders and everything looks like it is resting on a sphere.

So that is how I went from driving around on a flat plane to driving around on a ball. That's pretty much the stuff I think about day to day working on TurboGarbageTruck. I'm certain that there are better ways to do all the things I'm doing, but development is an iterative process and I should gradually figure out what works best for this game.

I've set up a twitter account so you can follow development over on the right there. I'll tweet any time a new build goes up or I do anything significant with the development, like posting up concept art, videos, or blog posts. You can also follow me @Owen_McManus for dev and non dev related tweeting.

No comments:

Post a Comment