Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Orbit ?  (Read 2528 times)
0 Members and 1 Guest are viewing this topic.
Offline GNecro1
« Posted 2013-08-27 19:37:54 »

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 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 ?

Java freak! Cheesy
Offline lcass
« Reply #1 - Posted 2013-08-27 19:56:13 »

You would need to understand basic phsyics principles such orbital mechanics, something like that would just assume the data.
Offline GNecro1
« Reply #2 - Posted 2013-08-27 20:53:35 »

yea i can make something orbit but when i have like 5 objects my frame rate drops by half

Java freak! Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PaidGEEK

Senior Devvie

Medals: 5
Projects: 3

« Reply #3 - Posted 2013-08-27 21:16:36 »

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.

Improvisational programmer.
Offline namrog84

JGO Ninja

Medals: 46
Projects: 4

Keep programming!

« Reply #4 - Posted 2013-08-27 21:34:29 »

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?  Huh

"Experience is what you get when you did not get what you wanted"
Offline Several Kilo-Bytes

Senior Devvie

Medals: 11

« Reply #5 - Posted 2013-08-27 21:56:51 »

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.
Offline kramin42
« Reply #6 - Posted 2013-08-28 03:27:08 »

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:
        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
    planet[i].x += planet[i].vx
    planet[i].y += planet[i].vy

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:'s_law_of_universal_gravitation

"All science is either physics or stamp collecting." - Ernest Rutherford.
Offline zngga
« Reply #7 - Posted 2013-08-28 03:37:54 »

My code never has bugs... it just develops unexpected features!
Offline kramin42
« Reply #8 - Posted 2013-08-28 03:54:15 »

That works perfectly for two bodies, but if you want a simulation of three or more its not as simple as that.

"All science is either physics or stamp collecting." - Ernest Rutherford.
Pages: [1]
  ignore  |  Print  

EgonOlsen (47 views)
2018-06-10 19:43:48

EgonOlsen (29 views)
2018-06-10 19:43:44

EgonOlsen (49 views)
2018-06-10 19:43:20

DesertCoockie (214 views)
2018-05-13 18:23:11

nelsongames (130 views)
2018-04-24 18:15:36

nelsongames (129 views)
2018-04-24 18:14:32

ivj94 (870 views)
2018-03-24 14:47:39

ivj94 (131 views)
2018-03-24 14:46:31

ivj94 (782 views)
2018-03-24 14:43:53

Solater (146 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!