Electric Motor and Generator
An electric motor consists of magnets that attract each other. In my view, the simplest electric motor is the permanent magnet synchronous machine (PMSM). It has the following basic design:

The rotatable rotor (grey disc) is shown in the middle. A bar magnet is glued onto the rotor. It has the magnetic poles north pole (N) and south pole (S). On the outside, the fixed stator is drawn (light blue). Six electromagnets made of coils are installed in the stator (dark blue). Electromagnets can generate a magnetic field with adjustable intensity and adjustable polarity (north pole or south pole). For this, a current of variable amplitude and variable direction is impressed into each coil.
The electromagnets are arranged at an angle of 60° to each other. The angles refer to the fixed stator, which is why we call it the stator angle Ω. The angle Ω = 0° is set at 3 o’clock (to the right). The angle coordinate system is therefore the same as for complex alternating current. In this example, the rotor is aligned with its south pole at ΩRotor = 30°. In the stator angle system, the rotor points to the stator angle Ω = 30°. The south pole of the rotor always indicates the direction in which the rotor is aligned (not the north pole).
Later, the electromagnets are to attract or repel the bar magnet. For this, opposite magnetic poles must face inwards towards the bar magnet. We achieve this with the following electrical connection, shown here for only one coil pair:

This connection of opposing coils is implemented for all three coil pairs. The current flows through the coils in such a way that one generates a magnetic north pole facing inwards and the other a magnetic south pole facing inwards when the same current flows through both. The polarity of the electromagnets is drawn for positive current I1.
Direction and intensity of the stator magnetic field
We want the rotor with the glued-on bar magnet to turn anticlockwise because of the magnetic polarity of the electromagnets. This requires the following polarities:

For the rotor to turn anticlockwise, the electromagnet at Ω = 0° must generate a magnetic south pole. It thus repels the south pole of the bar magnet. On the opposite side, at Ω = 180°, the electromagnet must generate a north pole that repels the north pole of the bar magnet.
The electromagnet at Ω = 60° must provide a magnetic north pole so that the magnetic south pole of the bar magnet is attracted. The electromagnet at Ω = 240° must provide a magnetic south pole. You can see that the required magnetic polarity of opposing electromagnets must always be opposite. The polarity of the electromagnets at Ω = 300° and at Ω = 120° plays no role in this rotor position.
As the rotor continues to turn, the magnetic polarities must change.

On the left of the figure you see the “old” polarities at rotor angle ΩRotor = 60°, which were still helpful at ΩRotor = 30°. This polarity hinders further rotor movement, because now the rotor is held in place by the electromagnets at Ω = 60° and Ω = 240°. The suitable “new” polarities for further rotation are shown on the right of the figure.
The two electromagnets at Ω = 60° and Ω = 240° should be switched off when the bar magnet of the rotor passes directly by them.
The magnetic fields of the stator electromagnets are superimposed. They are added vectorially to describe this superposition mathematically. Each electromagnet has an intensity and a polarity (direction). In the right figure, two north poles can be seen on the left and two south poles on the right. Overall, the north pole of the stator field points in the direction Ω = 150°. The stator generates a total magnetic field that acts on the rotor.
Sinusoidal current waveforms
During rotation, the bar magnet approaches the electromagnets and moves away again. The distance between the magnets is not constant. You know this from everyday life: the closer a bar magnet is moved to another bar magnet, the more intense the magnetic force becomes. The magnetic force changes with distance even though you do not change its cause. With a constant magnetic flux, the magnetic force is stronger the closer the bar magnet comes to an electromagnet. So with a constant magnetic flux, the force is not constant over one rotor revolution. The motor “judders”; it turns sometimes faster and sometimes slower. For the motor to turn evenly, the current must be reduced when the bar magnet comes close to the electromagnet.
For an even movement of the rotor over time, sinusoidal magnetic fluxes and thus sinusoidal electric currents are needed (without derivation). The required values of intensity and polarity of the current depend on the rotor position. To be able to specify the intensity and polarity of the current for every rotor position, the electromagnets are numbered. At the start of its movement, we place the rotor so that its south pole is at Ω = 0°. By definition, this is the rotor position ΩRotor = 0°.
From now on, for all directions we always orient ourselves by the magnetic south pole. This choice is arbitrary, but we have to decide on something. A direction of 60° therefore means that the south pole points in the direction 60°.

Let us first consider only the current at electromagnet 1. It is plotted against the angle Ω. The following waveform results for electromagnet 1 against the angle:

The rotor angle Ω is plotted on the x-axis. The required current i1(t) is plotted on the left y-axis. I would like to plot the magnetic fluxes of coils 1 and 4 in the same diagram, so I have placed a second y-axis on the right. This is only about the basic shape of the curves, so please do not pay attention to the peak values. You will learn to calculate them later.
Note: neither time nor the phase angle φ is plotted on the x-axis here. It is the rotor angle Ω. So what you see here is the current that must flow at a particular rotor orientation. If the rotor turns quickly, little time passes for one revolution. Then the current must change in the manner shown within a short time. In the end we do arrive at a time representation on the x-axis, but more on that later.
The positions of the coils on the stator have been added to match the angles in the time diagram. The red sinusoidal waveform indicates the intensity of the current through both coils. The polarity of the resulting magnetic field of coil 1 (light blue) changes from south pole (S) in the upper half to north pole (N) in the lower half. The magnetic field polarity is indicated to the right of the waveform by the vertical red-green block. The field of coil 4 (dark blue) has exactly the opposite polarity. The current i1(t) is proportional to the magnetic flux through coil 1. It is inversely proportional to the flux through coil 4.
At the rotor angles ΩRotor = 0° and ΩRotor = 180°, the current is off so that the rotor is not braked.
Let us look at the magnetic fields at ΩRotor = 90°. For an even force profile over the rotor angle, the current at coil 1 should generate a maximum south pole when the south pole of the bar magnet is at 90°. Coil 1 thus repels the rotor. With the same current at ΩRotor = 90°, coil 4 generates a maximally large north pole and attracts the south pole of the rotor. The following figure shows the magnetic polarities of the two coils at ΩRotor = 90°:

You can also try other rotor angles. Take a sheet of paper and sketch the magnetic polarities of coils 1 and 4 at different rotor angles for the given current waveform. You will find that the rotor is always turned.
Coil pair 3 and 6 is arranged on the stator Ω = 120° behind coil pair 1 and 4 in the direction of rotation. These coils need the same current waveform, only at a different rotor angle. The current waveform must be shifted in time by 120° (to the right).
For coil pair 5 and 2 we need a shift of Ω = 240°. All coil pairs do the same, only at different rotor angles. Suppose the rotor turns anticlockwise at constant speed. Then the angular position is proportional to time. We can then – as in the fundamentals of electrical engineering – convert angle and time into each other.
A positive current through coil 1 generates a magnetic north pole on the inside of coil 1 (towards the rotor). The same positive current through coil 4 generates a magnetic south pole on the inside of coil 4. This applies to all coil pairs.
Time and angle view
Coil pair 3 and 6 receives a current delayed in time by Ω = 120°. This is because coil 3 is arranged on the stator shifted by 120° in the direction of rotation, and the rotor passes there somewhat later than at coil 1. The “delay” is one third of a revolution or one third of a period. The current waveform depends on when the bar magnet passes a coil. The current i2 flows through coils 3 and 6. The current i3 flows through coils 5 and 2.

On the left of the graphic above you see the connection of the current sources to the coils. On the right you see the direction in which the magnetic fields resulting from the currents act on the stator. This leads to the following requirement for the current waveform:

Two x-axes are used in the figure above. One indicates the stator angle Ω over a complete rotor revolution. The coil positions are also drawn on this axis. Below it you will find the axis of the electrical phase angle from electrical engineering. The values of the axes are the same, because one rotor revolution corresponds to one electrical period of the sine signal of the current. From the lower axis you can read off the electrical phase rotation of the currents:
The electrical angles φ are rotated exactly the other way round from the rotor angles Ω. The position of coil 3 is at Ω = 120°. The current at coil 2 is 120° later in time, so it has a phase shift of φ2 = -120°. Please note this important difference between the two angle systems: the stator-related geometric angle Ω and the angle φ of the time shift of the current waveforms.
Next, let us look at the waveforms of all 6 magnetic fluxes during a complete rotation of the rotor from 0° to 360°:

The magnetic fluxes of the opposing coils each have opposite polarity but the same magnitude. Coil 3 needs the same flux as coil 1, only 120° “later”. That is why the sine waveforms are the same but shifted relative to each other. Let us look at the electromagnets at the angle Ω = 0°:

At rotor angle ΩRotor = 0° (on the y-axis in the left figure), coils 5 and 6 form equally strong magnetic south poles. Coils 2 and 3 form equally strong magnetic north poles. Coils 1 and 4 carry no current and therefore have no effect. This follows from the amplitudes of the waveforms in the figure above at Ω = 0°. All magnetic fields are superimposed; they are added to a total field. The total field of all stator electromagnets therefore points from bottom (S) to top (N). This is marked as an arrow labelled “stator field” in the right figure.
The stator magnetic field always lags 90° behind the rotor magnetic field in the direction of rotation. Even when the rotor continues to turn, with these currents the stator magnetic field always lies 90° behind the rotor field. In this way the stator pulls and pushes the rotor into its circular motion. For anticlockwise rotation (and only then), the following applies to the stator magnetic field:
Let us look at the currents mathematically. For this I anticipate that most electrical machines are operated at f = 50Hz. The sine waveform of the current has the frequency f = 50Hz. One rotor revolution then lasts the period of the frequency T = 1/f = 20ms. The following applies:

As with alternating current in the fundamentals of electrical engineering, two x-axes are drawn: one for the phase angle and one for time. One rotor revolution and one electrical period of the sine last T = 20ms. The peak values of the currents are equal. The phases are each rotated by 120° or 2π/3. Three currents of this kind are called three-phase current.
Direction of rotation
In our minds, let the motor turn the other way round. What changes? The rotor starts at coil 1 and then passes coils 6, 5, 4, etc. backwards. For this we only have to swap two cables. The current i2 now flows through coil 5, because the rotor passes here first. Only after that does it pass coil 3, to which i3 is connected. Again, you can work this out yourself with pen and paper.
Motor and generator operation
In motor operation, current is impressed into the coils from outside. Electrical energy from an electrical source is converted into mechanical energy in the form of a rotation of the rotor at the system output. For this we need three-phase current at the electrical input of the system.

The electric motor works bidirectionally. We can swap output and input. When the shaft is turned, a three-phase current is “pushed” the other way round out of the motor into the coils, so that it can be used for other tasks. Mechanical energy in the form of a rotation is put into the system. Electrical energy is available at the output of the system at the coils. The motor now works as a generator.
Electrical machines need three-phase current to turn the rotor. Operated the other way round, rotating electrical machines generate three-phase current when the rotor is turned by a power plant. This is one of the reasons why three-phase current plays a central role in drive technology and in power generation.