Wednesday, 6 August 2014

The Ride of the Rover

I'm at my third iteration for the colony design system and I'm still not happy with it! I was getting sad so I decided to try something completely new and fun. Introducing, the Mars rover! I am shocked how well this turned out. It only took me five hours or so, and hardly any planning. The 3D model is temporary and thus hideous. Here it is!


Like always, I've made the code flexible so it should work with vehicles that have any number of wheels. And now that I think of it, in any configuration too... Hmmmm!

Linear algebra is not just for school
I had to whip out my knowledge of linear algebra, friction, dampening coefficients, torque, and kinematics. As always Unity3D was a great help. For example: I didn't have to code my own 3D vector projection function: it already existed. Always ask yourself whether you are writing code that already exists. Re-using code is faster for you, clearer for other programmers who read your code, probably faster for the computer, and far less likely to have errors and bad fringe cases.

Each wheel applies an upwards force to the rover, parallel to "up" on the rover, linearly proportional to how compressed the suspension is for that wheel. If it is fully compressed, it applies 100% of its force.

Each wheel applies an extra friction force proportional to how much the vehicle movement is sideways - but only if that wheel is touching the floor. That force can potentially make the vehicle roll over. Rollovers are critical to gameplay because they are a very real and serious concern. I needed a rover that will flip if you're reckless. It opens up all kinds of fun things like damage to the rover, and getting a jack to flip it back.

Gizmos
You may notice a green or red glow inside the tires. Those are Unity "Gizmos". They are helpful things that appear in the 3D world, but not in the final game.

    void OnDrawGizmos()
    {
        if (wheels == null)
            return;

        foreach (Wheel wheel in wheels)
        {
            if (wheel.IsTouchingSomething())
                Gizmos.color = Color.green;
            else
                Gizmos.color = Color.red;
            Gizmos.DrawSphere(wheel.transform.position, 0.2f);
        }
    }

Test scene
The first scene in the video helped me debug. I just had to press play to see four cases on the same slope:

  1. Rover upright.
  2. Rover aligned to slope.
  3. Sideways rover upright.
  4. Sideways rover aligned to slope.

I strongly suggest setting up a "test scene" for cases like this. It consumes far too much time manually driving around to find issues - and it's impossible to reproduce exactly the same event.

Ugly rover?
So how about that ugly rover? That model (made in 5 minutes) is temporary because it's possible I will be getting help! Oscar Mathews who is a "nuclear engineer at Norfolk Naval Shipyard and a LCDR select flight test engineer and Mission Commander for the Navy Reserve as an officer" has offered to help. He is learning blender and produced these drawings for us to collaborate:

They also serve as good evidence that he has technical and artistic skill. So I eagerly await attaching my rover scripts to his beautiful rover!

Mars Colony Challenger
In my quest to learn all things Mars, I've found a Mars colonization game called Mars Colony Challenger. While I sympathize with how much work must have gone into it, it didn't rate very well and there are clearly some major gameplay issues. Still, I watched a couple hours of gameplay videos with commentary and learned a ton about how people experienced the game. I have a very good idea of what that game is missing and I intend to move Martian Agora in that direction.

No comments:

Post a Comment

Be polite.