I cant find and cant think of how do make one object orbit another and another object orbit that object
it is to complex look at planet simulator http://dan-ball.jp/en/javagame/planet/ doesn’t matter how much objects are there it runs smooth and it is stable. I was always been interested in simulation games and sandbox games
but to make one that is so hard
does any of you people know any way to make this kind of simulator work ?
You would need to understand basic phsyics principles such orbital mechanics, something like that would just assume the data.
yea i can make something orbit but when i have like 5 objects my frame rate drops by half
Just make a “planet” class where you have vector for position and velocity. In update method you can iterate through other planets, calculate angle to this one and apply some speed into that direction (of course you have to keep in mind that a=m/s^2). You could also work with mass for more realistic simulations. For user’s control you’d just apply some custom speed to any planet.
If you are having 5 objects and dropping frame rates like that, there is something much more seriously wrong there.
you should be able to many hundreds, possibly thousands without too much of an issue.
Post some sample code and maybe we can help identify the issue? ???
position = x
velocity = x’
acceleration = x’’
Newton’s first law: F/m = x’’
Force due to gravity is proportional to the mass of each object and inversely proportional to the square of the distance between two objects.
The equations work for scalar and vector numbers. Use vectors and you won’t have to worry about angles. The number of interactions is O(n^2), so something is wrong if that is too slow for values between 1 and 30. You could choose to ignore the pull of small or very distant planets and make very massive objects unaffected by acceleration, but that should not be necessary for small numbers of planets.
Edit: What namrog84 said.
For each planet/star have at least position, velocity and mass. Also have a gravitational constant, G (just arbitrarily set this to something that works).
Then you can update the objects like so (pseudocode):
for i=1 to n: //go through all the planets
for j = 1 to n: //add up the gravitational effects of all the other planets
if i==j:
continue
end
dist = sqrt((planet[i].x - planet[j].x)^2 + (planet[i].y - planet[j].y)^2)
accMag = G*planet[j].mass/(dist^2)
accX = accMag*(planet[j].x - planet[i].x)/dist
accY = accMag*(planet[j].y - planet[i].y)/dist
planet[i].vx += accX
planet[i].vy += accY
end
planet[i].x += planet[i].vx
planet[i].y += planet[i].vy
end
dist = distance between the planets, accMag = acceleration magnitude, accX = x component of the acceleration, vx = x velocity
Remember, planet i is the planet you are calculating the acceleration of, and planet j is the planet it is being attracted to.
Also, I’m not sure what you’re doing already, but x and y need to be floating point numbers, not integers.
Check out these wikipedia articles too:
That works perfectly for two bodies, but if you want a simulation of three or more its not as simple as that.