The initial bike started to form based on some tutorials aimed at creating a hover car. This starts with a basic block of physics material which will act as a body for the bike, and then it is kept up using ray-casts at each corner pointing down. These detect contact with the ground and compensate by applying an upward force. This acts as a rudimentary suspension that will keep the body off the ground but still be able to go over obstacles.
The initial balance is an issue. The 4 corners are held up which is a good start, but as the body is quite narrow it can fall over quite easily. To counteract this the center of mass is lowered which makes the body quite stable and difficult to fall over.
This body then acts as the base for the bike which will render a bike model in it's place. The rest of the behavior is all added by modifying the behaviors of this base block.
Turning and Leaning
Turning is similar to the movement in that it is applying a force to the base block in the form of a torque. While using the physics engine there is a limitation that the torque can only rotate the body around the center of mass, which is not exactly what we would want. A more realistic model would be to rotate around the back wheel as the front wheel leads the direction of the bike. I tried to implement a custom solution to make the bike behave like this but it wasn't very effective and had a lot of trade-offs. It was mostly a visual consideration, as as we moved to a first person camera it became less of a concern and became a very low priority issue. The bike also leans as the player turns which is not very functional towards the motion of the bike but it visually pleasing and feels more like riding a bike.
Motion in the air
While in the air during a jump I also added the capability to lean slightly and change the orientation of the bike. During the initial stages we only allowed input while the bike was on the ground, which worked functionally but felt strange as you lost all control while in the air. This added control in the air allows the player to slightly adjust themselves while in the air to be able to match up their landings correctly for an upcoming part of track and maintain their speed.
A lot of the initial inputs such as turning and leaning worked fine but had lots of room for improvement. One way this was address was defining custom curves for each of these values which could be dynamically adjusted on the fly. In practice this meant that things like the players speed could be used as in input for these details to create more accurate movements. An example of this is the turning rate of the bike. You have a quite small turning circle when you are travelling as lower speeds, which is gradually increase as you go faster on the bike.
Once we have the block it is quite easy to add actions such as moving forward, backwards, and braking. Different states are managed while the bike is simulated to determine if the backwards force applied will be a braking motion or the player is reversing. This is all done by adding forces to the base body in order for it to gain velocity. Different values were tested to find states that would simulate accurate acceleration. This became less of an issue to get 100% correct when we moved to testing on the actual level, as the bike is often accelerating while going down a hill.
The behavior of the bike's "suspension" is mostly reliant on the ray-casts it uses to keep the bike off the ground. The other main factor is the damping system that is a part of the engine which acts as a simulation of drag. This could be used to vary the "bounciness" of the bike and get it to feel right over different terrains. The in built solution was used for a long time, but there were some limitations that were undesirable in this model. The main one was that the drag must be the same in all directions. In our case we wanted to control each axis separately so that that forward movement of the bike could be separated from the up and down movement of the bike acting as the suspension. I implemented a custom system that allowed us to control these things individually which was great, and especially helped with getting behaviors like the bike flying in the air feeling good.
As mentioned previously the bike was initially made quite stable in that is was hard for it to tip over or lose control. This was desirable for the most part but we also wanted to be able to punish the player for not approaching an obstacle properly. This was done by monitoring the movement of the bike and the sudden changes in velocity. These virtual "g-forces" can be calculated and if this value broke a defined barrier then we said that this was a crash. When a crash is detected we reset the center of mass of the bike so that it becomes unstable and naturally falls over. This simulates a loss of control from the player where they are no longer able to balance upright.