SF had asked how to preserve speed while changing direction. I'll try to answer that question. But I'll also talk about changing direction without changing speed. My goal is to give a feel for hyperbolas and swing bys.
Preserving direction wrt sun but changing speed
For my examples I'll use earth as the body doing the gravity assist.
From earth's point of view, the space ship's path is a hyperbola. Incoming Vinfinity and outgoing Vinfinity have the same speed but different directions. I attempt to illustrate the notion of Vinf on page 36 of my orbital mechanics coloring book.

Since incoming and outgoing Vinfinity are the same speed, the velocity vectors are two sides of an isosceles triangle. Delta V is the base of the triangle. Top angle δ is the hyperbola's turning angle:

From earth's point of view, direction is changed.
But it's possible to preserve direction from the sun's point of view.
Earth's path with to the sun is nearly horizontal, a flight path of 0º. It's speed is about 30 km/s. Let's imagine a space ship that has flight path 9º and speed 28 km/s when it crosses earth path:
In this case, incoming Vinf wrt earth is about 3 km/s.
To preserve direction, delta V vector must be parallel or anti-parallel to the space ship's vector:

For this to occur, the incoming Vinf and outgoing Vinf need to form symmetrical angles. The center angle is the hyperbola's turning angle, δ. So the angle between spaceship's vector and the incoming Vinf vector must be 90º-δ/2.
In this example the space ship would maintain direction with regard to the sun but it's speed would be boosted from 28 km/s to 29.6 km/s.
Let's change the scenario to a space ship in a circular 1 A.U. polar orbit. Speed is 30 km/s, flight path is zero but inclination is 90 degrees.
In this case incoming Vinf wrt earth would be 42 km/s. δ would be 90º. Delta V would be 60 km/s. This would flip the space ship to a 270º inclination orbit and the space ship's speed would remain 30 km/s but in the opposite direction.
This sort of gravity assist isn't possible unless we drastically reduced earth's diameter. Turning angle increases as hyperbola's perigee gets closer to earth. Closest perigee we can get without lithobraking is an altitude of 0 kilometers.
With 0 km altitude perigee and a vinf of 42 km/s, turning angle is only 4º.
Preserving speed wrt sun but changing direction
If the ship's new velocity vector has the same speed as the old, the vectors are sides of an isosceles triangle.

The delta V vector is the base of this isosceles triangle.
So to preserve speed, the angle Vinf forms with earth's velocity vector needs to be half of the hyperbola's turning angle.