Skip to content

gravity simulation problem

Not downloaded yet

i am trying to create gravity simulation model for a game. i need planets to move around a black hole in elliptical orbits.

first i calculate acceleration and then add it to velocity and according to that i calculate the next position of planet. i first tried with just calculating distance vector between planets and black hole but in that scenario planets directly gets sucked inside the black hole and i need them to move in elliptical orbits. i tried calculating acceleration due to gravity but i think formula is wrong so can you correct it or provide some other implementation.

https://i.redd.it/jmrp7u7ibs031.png

5 points6 comments
Open on Reddit
About this post
Posted
Archived
Score
5 · 100% up
Kind
Image
ID
btodvz

Author

1
posts
5
comments
1
media
View profile

6 comments

As pointed out by the other comment, planets need to start with velocity if this is gonna work.

​

Assuming you want to want to build an accurate simulation of gravity (and not just hard-code the paths of the planets to be ellipses, which perhaps could also work but wouldn't be a simulation then), if a planet starts out some distance away from a source of gravity and it's not moving, then it will start moving in a straight line towards that source of gravity. The way I understand it, for it to move in an ellipsis around that source, there are a few things that need to line up.

  • The starting distance between the planet and the black hole
  • The initial speed of the planet
  • The direction of the planet's initial movement

If any of these don't line up according to the equations of gravity and laws of physics, things are gonna play out differently from what you're trying to do. The planet might completely move past the black hole and just have its direction slightly altered towards it. Or, the planet might fall directly into the black hole without having the chance to complete a full ellipsis. Or, the planet might make a half loop around the black hole and shoot out in the direction it came from (I think?).

What the values need to be for us to get a nice ellipsis outcome, I have no clue about. I think it would take some careful tuning according to the equations we're using. But isn't that also the point of having a simulation? We simulate reality, so things are gonna be a bit messy, just like they are in the real world.

​

Here is an interesting explanation from NASA that I find a bit illuminating:

An object's momentum and the force of gravity have to be balanced for an orbit to happen. If the forward momentum of one object is too great, it will speed past and not enter into orbit. If momentum is too small, the object will be pulled down and crash. When these forces are balanced, the object is always falling toward the planet, but because it's moving sideways fast enough, it never hits the planet.

(https://www.nasa.gov/audience/forstudents/5-8/features/nasa-knows/what-is-orbit-58.html)

2 points

You are right. Planets do need some initial velocity. With some initial velocity it's working Thanks

2 points

you need calculate starting speed at the start of simulation

also, it will be better if you will use "x+=speed+accel/2 and then speed+=accel"

and where is your dt?

(and i see that you have no "speedx+=")

2 points

I feel that when calculating the acceleration due to gravity on one axis, you should be dividing by the cubed distance between the black hole and planet on just that one axis only.

​

You could simplify this problem greatly by using a linear algebra library

2 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.