I am doing a planetary simulation and I want to draw vector fields for the planets.
Currently I have calculated the force and plotted it like this:
But since the force is bigger as the separation gets smaller, it gets too big near a star and leads to the cluster. I want to achieve a similar effect as this:
Source: https://www.statology.org/matplotlib-quiver/
My Code:
- The user can set density for the vector field plot
- We simulate having a planet of a certain mass at that location
- Then calculate the force experienced by that planet as a result of all the other planets
- Plot the arrows
def vectorField(density):
xmin, xmax = simulation_plot.get_xlim()
# get the bounds of my graph
ymin, ymax = simulation_plot.get_ylim()
dif = xmax - xmin
# get the length of the graph
xmin, xmax, ymin, ymax = int(xmin), int(xmax), int(ymin), int(ymax)
step = int(dif / density)
x = [i for i in range(xmin, xmax, step)]
# x-y coords for the arrows
y = [i for i in range(ymin, ymax, step)]
X,Y = np.meshgrid(x,y)
# make a meshgrid
xForceArr = []
yForceArr = []
for y_ in y:
for x_ in x:
force = calcForce(x_,y_)
# calculate the vector force experienced by a planet at that location
xForce = force.x
yForce = force.y
# resolve components
xForceArr.append(xForce)
yForceArr.append(yForce)
# save it
xForceArr = np.array(xForceArr)
yForceArr = np.array(yForceArr)
u = xForceArr.reshape(X.shape)
v = yForceArr.reshape(Y.shape)
# use the components to find direction and size
simulation_plot.quiver(X, Y, u, v, units="xy")
# plot the quiver plot
I have also tried to use angles in the quiver plot (e.g. set angles = 90)
And it yields this result:
While similar to what I desire, a problem lies in the fact that when the simulated planet is very close to the star, it creates a very large force and by tkinter’s scaling (as it uses the average force to scale), everything else becomes very small and there are no arrows. This creates an almost pulsating wave as the heavy star moves near an arrow and cause everything to be smaller in comparison.
In essence, I need a way to make the angles of the arrows point out of the screen and scale correctly.
I want the arrows to point out to the screen while still being 2d, if that makes sense.
- Tried using “angles” settings of quiver –> not in z dimension
- Tried scaling the forces by the magnitude of the biggest force –> all arrows have same size