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.
About this post
- Posted
- Archived
- Score
- 5 · 100% up
- Kind
- Image
- ID
- btodvz
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.
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:
(https://www.nasa.gov/audience/forstudents/5-8/features/nasa-knows/what-is-orbit-58.html)
You are right. Planets do need some initial velocity. With some initial velocity it's working Thanks
Is the formula right for acceleration?
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+=")
I will try that
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