3D-n-body-gravity for blender?

Has anyone developed 3D-n-body gravity for blender?

I have a few such apps online but written in vb6,
my python is quite rudimentary,
and using the built-in force simulations I found to be
both slow and inaccurate, even though they look much nicer
than my old vb code.

Any comments welcome.

1 Like

Welcome :tada:

you maye have look here:

2 Likes

Yes, I have done similar things, like that sample, but that is not quite real n-body-gravity, because its many bodies yes, but only one gravity field.

I got 2 gravity fields going, but it started jumping around, and got much worse with the 3rd gravity field. Thats just using the normal objects in blender. The problem beyond that, is that my python is just very far away from being able to ascertain if its any better than vb - as regards speed. It should be, because vb does not use multi-core processing.

But multi-core processing is inaccurate.
But vb6 is obsolescent.

I am mostly hoping that someone who knows python well can translate my vb code for n-body-gravity.
The issue being that blender itself does not offer an easy way to put in highly accurate starting velocities for specific objects.

This is the code I want to convert into blender:
http://www.flight-light-and-spin.com/n-body/n-body-build.htm

1 Like

Hehe… i love those sentences…

the code is from Visual Basic 6 , but should be easily translated into any other language.

…and then there is something missing and typos… but maybe somethign like this will help… ( i guess you want to move some object instead of drawing/ printing some values… and IDK how the txt file is used here ??..)

TimeRate = 1.0
Planets = []
PoX = []
PoY = []
PoZ = []
TotalX  = []
TotalY  = []
TotalZ  = []
RadBody = 1.0
Mass  = []
Gee = 1.0

# GRate is a time-scale variable that is a square of TimeRate!
###100
GRate = TimeRate ** 2
# set both rates to = 1 for simplest solution.

for Ss3 in Planets:	# zero is the sun's number.
	TraVX = 0.0
	TraVY = 0.0
	TraVZ = 0.0	# planet velocity Traversed XYZ
	for Ss2 in Planets:
		if Ss3 != Ss2: ## Then GoTo 200	# no gravity on planet itself.
			# Ss2 is the effective planet, Ss3 is is the effected planet.

			# PoX PoY PoZ are real positions in km for XYZ axes.
			# Preset the positions yourself prior to this routine.
			Xx = PoX[Ss2] - PoX[Ss3]	# Get the distance between the bodies.
			Yy = PoY[Ss2] - PoY[Ss3]
			Zz = PoZ[Ss2] - PoZ[Ss3]
			# 3d pythagorus, Dist = distance between bodies.
			Dist = ((Xx * Xx) + (Yy * Yy) + (Zz * Zz)) ** (0.5)

			# Radbody = radius of body. Exclude close encounters here:
			if Dist > RadBody:
				#3d Newton	# s equation * time-scale variable:
				Gfor = ((Mass[Ss2]*Gee) / (Dist ** 2)) * GRate
				#GRate = time-scale.
				#Gfor = virtual gravity force.

				Zp = Zz / Dist	# Zp is Z-proportion from Zz distance, ditto Xx & Yy.
				Yp = Yy / Dist
				Xp = Xx / Dist
				GraVX = Gfor * Xp	# Gravity distributed proportionally.	### TYPO !!!!!
				GraVY = Gfor * Yp
				GraVZ = Gfor * Zp
				#GraVX GraVY GraVZ are individual shifts in velocity caused by
				#gravity-force for each isolated interaction in quantum time.
			## End If

			TraVX = TraVX + GraVX
			TraVY = TraVY + GraVY
			TraVZ = TraVZ + GraVZ
			# TraVX TraVY TraVZ are accumulated velocities traversing.
			# for each planet in quantum time.

		###200
	## Next Ss2

	# TotalX TotalY TotalZ are combined shifts in velocity.
	# for a single step in time from all planets.
	TotalX[Ss3] = TraVX
	TotalY[Ss3] = TraVY
	TotalZ[Ss3] = TraVZ
## Next Ss3

# The first pair of for...next loops are now complete.
# They have determined what the changes to the planetary velocities will be.

# Now this is where the velocities are actually changed for each time unit.
# MoX MoY MoZ are motion variables for all time, so do not set them to zero.
# Starting motion/velocities not here included, define those yourself prior to this routine.
for Ss4 in Planets:
	MoX[Ss4] = MoX[Ss4] + TotalX[Ss4]
	MoY[Ss4] = MoY[Ss4] + TotalY[Ss4]
	MoZ[Ss4] = MoZ[Ss4] + TotalZ[Ss4]
## Next Ss4

# The velocity has changed, now we move the positions of each planet.
for Ss5 in Planets:	# positions moved by motion variables.
	PoX[Ss5] = PoX[Ss5] + MoX[Ss5]
	PoY[Ss5] = PoY[Ss5] + MoY[Ss5]
	PoZ[Ss5] = PoZ[Ss5] + MoZ[Ss5]
## Next Ss5

# now put the actual dots on the screen:
for Ss6 in Planets:
	# Zoom is distance scale,
	# balX balY balZ balance position on screen. Figure these yourself.
	PsX = (PoX[Ss6] / Zoom) + balX
	PsY = (PoY[Ss6] / Zoom) + balY
	# PsX PsY PsZ are screen positions in pixels.
	#balX, balY, balZ are your screen position variables balanced to center-screen.
	#-- PSet (PsX, PsY), PlanetColor	# draw pixel: top-view, main screen.
	print('# PsX, PsY')
	print(PsX, PsY)

	# zxaxisZ will also require your zoom and balance adjustments
	zxaxisZ = (PoZ[Ss6] / Zoom) + balZ
	# zxaxisY and zxaxisX are similar to PsX & PsY
	#- ViewXZ.PSet (zxaxisX, zxaxisZ), PlanetColor	# draw side-view (XZ-axis).
	print('# zxaxisX, zxaxisZ')
	print(zxaxisX, zxaxisZ)
	#- ViewZY.PSet (zyaxisZ, zyaxisY), PlanetColor	# draw side-view (ZY-axis).
	print('# zyaxisZ, zyaxisY')
	print(zyaxisZ, zyaxisY)

## Next Ss6

###Goto 100	# not required if using an event-timer that loops itself.

1 Like

well, in a few years i likely will get to python,
until then, the pythonites might get my code pythonized
and get to corroborate the core of astrophysics
it represents,

but dare i mention that the hegemony of the day
reckons it is logical that a ‘black-hole’ cannot
emit anything even if it moves at light-speed,
but then
give the nobel prize in 2017 for claiming that
it has been detected that gravity being
emitted AT LIGHT-SPEED was coming from
these very same ‘black holes’ !!

but i digress

Here is a video explaining the code a bit: