Heavy ion collisions are an important method to study the quantum chromodynamics. In the early stage of relativistic heavy ion collisions, an extremely strong magnetic field is generated. The magnetic field will induce novel phenomena such as the chiral magnetic effect. However, the magnetic field will decrease rapidly, so it is difficult to measure its effect on the system. Charmonium states which are created by the initial scattering will be affected by the magnetic field and carry the information about it. We use the two-body Schrodinger equation with magnetic field to study the influence of the magnetic field on the charmonium state. The magnetic field is introduced via minimal coupling and its effect breaks the conservation of momentum and the conservation of angular momentum as well. The energy of the charmonium state depends not only on the magnetic field, but also on the momentum of the charmonium, thereby leading the final charmonium yield to be anisotropic. For a constant and homogeneous magnetic field, using the method of angular momentum expansion, we numerically calculate the energy spectra of the charm quark bound states with different magnetic field strengths and total momentum. The method is used to expand the three-dimensional wave function on the basis of different orbital angular momentum and spin states whose wave functions are numerically calculated first. In the actual calculation process, it is found that a good accuracy is achieved when taking
$n\leqslant 2$
,
$l\leqslant 7$
. Furthermore, the dependence of the Hamiltonian on the magnetic field and total momentum is analytically determined to be
$H=H_0+(qB)^2 H_1+qBP_{{\rm{ps}},\perp} H_2$
. Therefore, only the coefficient matrices
$H_{1}$
and
$H_{2}$
need to be numerically calculated once and the Hamiltonian with arbitrary magnetic field and momentum can be determined. The inverse power method is then used to find the lowest eigenvalue in the angular momentum space. Such a numerical method significantly reduces the amount of calculation and still ensures the accuracy of the calculation as well. The calculation results show that as the magnetic field and the total momentum increase, the mass of the charm element increases. The increase of the mass can be as large as
$20\%$
, when we take
$eB = 20 m_{\rm{\pi}}^2$
and
$P_{{\rm{ps}}}=1.8 \;{\rm{GeV }}$
, which can be easily achieved in RHIC collisions. Therefore there should exist significant magnetic effect on the
$J/\psi$
production in heavy ion collisions.