Swimming
Swimming is a mechanic that is often hard to
implement or avoided entirely.
In fact a lot of game developers find
implementing swimming not worth it. It tacks more time onto the
game, increases difficulty, and a lot of people don't find
swimming fun.
A lot of players don't like swimming
mechanics because of the lack of control, the slowness of the
character, having to keep track of not drowning (which can be
stressful in sonic games). Players also find swimming levels to
be dreaded because it is extremely boring (swimming forever). So
in games, swimming is taken care of in different ways.
0. No swimming-
The character dies upon contact (Smash Bros
3DS) or the character cannot ever interact with water (Animal
Crossing).
1. Basic swimming-animation only
Another option would be to only change
the character animation and nothing else. The first Mario
game I made I did swimming as animation only (picture shown
below). All I did was check if Mario collided with the water and
then changed his sprite image and animation-the gravity and
everything was the same, so he wasn't any slower.
The only reason this worked is because it was
only shown to biased family members, not the general public, so
I wouldn't recommend this at all, but it's there.
(Screen shot of my Mario game)
2. Realistic swimming-drag and Newton's laws
Now if you want your game to have real
swimming, not cheap swimming as shown above, the best way to do
so is to add some realistic physics into your game, the physics
of swimming. So let's learn some physics!
--Newton's laws--
Newton's second law states that an object
with a mass that has forces acting on it will have an
acceleration. This is stated as the equation F=ma. Where F is
the force in a given direction, m is the mass of the object, and
a is the acceleration in the force's same direction. (Usually F
is the net force which is the sum of all forces acting on the
object.)
To show the forces acting on an object in
water we can draw something called a free body diagram, which is
a picture of the object and the forces acting on it.
--Free Body Diagram--
As we can see, an object in water has 4
forces acting on it, buoyancy, gravity, drag, and the object's
own force of motion (which direction the character is swimming).
Further reading