• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Muzzy A

Problem getting correct orbits of planets

15 posts in this topic

Hey, I'm making a little solar system simulation for myself to have fun with.

Problem is, I have all the planets at the correct distance that they should be away from each other, and each given the correct average velocity
But when I apply the force of gravity 'G = m/d^2', they don't orbit correctly. All of the outside planets make their way in towards the sun and eventually get launched out of orbit forever. I've been plugging in random values into their 'mass' and trying to fake my way through it, but it's not working.

If everything is set the way it's supposed to be, mercury gets it's correct orbit, venus is a little off, earth is a little more off and etc.

What could I do to make this work the way it's supposed to? I have a book on Orbital Elements, but I'm trying not to use orbital elements, because I want it to be more dynamic, so I can add random objects that would affect everything like real life.

So what more could i do with 'G = m/d^2' to get a correct orbit for each planet?
1

Share this post


Link to post
Share on other sites
Did you try setting actual values for the masses, and using the universal gravitational constant? Also, you need to give each planet the correct [i]tangential[/i] velocity depending on where it's located at the orbit (because orbits are not perfectly circular, so the velocity of the planet is not constant).

[quote]But when I apply the force of gravity 'G = m/d^2'[/quote]
This m is the sun's mass, right? Not the planet's.

[quote]If everything is set the way it's supposed to be, mercury gets it's correct orbit, venus is a little off, earth is a little more off and etc.[/quote]
What is your timestep? If it's too big errors will quickly creep in the simulation and ruin it. This could also be due to floating-point inaccuracy, losing a bit of precision every tick.
1

Share this post


Link to post
Share on other sites
i feel like floating point inaccuracy is a part of the problem, but definately not all.

'm' is the mass of everything, including the sun. Gravity from all objects affect all objects.

I have no tangential velocity, i just have normal velocity being affected by gravity.

I'm using the Universal Gravitational Constant.

i scaled all the masses and the Gravitational constant just a bit to try to avoid floating point error.
I imagine that could be a problem as well?

[code]
// time step is based off the time between frames
float dt;
const double G_Constant;

Vec3 gravityAccel = (G_Constant*objMass/distSqu) * direction ;
velocity -= gravityAccel * dt;
[/code] Edited by Muzzy A
0

Share this post


Link to post
Share on other sites
[quote name='Muzzy A' timestamp='1350624899' post='4991685']
'm' is the mass of everything, including the sun. Gravity from all objects affect all objects.
[/quote]
Woh woh woh, what? Do you mean you sum up the gravitational accelerations from each object, right? You can't just take the sum of the mass of everything like that.

[quote]I have no tangential velocity, i just have normal velocity being affected by gravity.[/quote]
For objects in orbit, you can define a new coordinate system based on the tangent of the object with respect to the orbit. By definition, objects in orbit have velocity only in the tangential direction. Direction matters. If you don't set the velocity in the right direction the orbit will obviously be different (although in any case it will still form an orbit if the velocity is less than escape velocity).

[quote]Vec3 gravityAccel = (G_Constant*objMass/distSqu) * direction ;[/quote]
What is objMass?? Is it the mass of the object exerting the force, or is it the mass of the object being affected?
1

Share this post


Link to post
Share on other sites
no no no no lol, i do this algorthm for every object there is, i don't add them all up into 1 mass ^.^ lol.

and objMass is the mass of another object, not itself, sorry for making it a little cryptic lol.

distSqu is the distance between the 2 objects squared.

I may have to look up tangential velocity then
0

Share this post


Link to post
Share on other sites
The value of gravityAccel is actually a force, not acceleration. You need to multiply it with mass to get the proper acceleration. Also, you need to include the masses of both objects in your gravity equation. Finally, ALWAYS apply opposite equal forces, otherwise your simulation will not conserve momentum. For two interacting objects a and b; when adding force f to object a, always add force -f to object b.

Cheers,
Mike Edited by h4tt3n
0

Share this post


Link to post
Share on other sites
[quote name='h4tt3n' timestamp='1350627152' post='4991693']
The value of gravityAccel is actually a force, not acceleration. You need to multiply it with mass to get the proper acceleration. Also, you need to include the masses of both objects in your gravity equation.
[/quote]
No. F = ma. a = F/m, and the force is defined as F = G m1m2/r^2, so a = F/m = G m/r^2. This is correct. Gravitational acceleration is independent of the "receiving" object's mass. Edited by Bacterius
0

Share this post


Link to post
Share on other sites
You could experiment a little. Take two masses and draw them traveling at some random velocity (this implies direction too). Have a toggle button that either skips the gravitational force calculations or turns it on. When the button is toggled off all objects should continue in a straight line from their last computed velocity. This is an example of one of Newton's laws, an object at rest tends to stay at rest, an object in motion tends to stay in motion unless they are acted upon by an outside force. In particular this motion is linear. Then, click the toggle button to start computing gravitational force on each of the objects and soon they should be moving on a curved trajectory, assuming their velocity has some tangential component relative to the other.

Bacterius is correct. You have to supply the correct tangential velocity.
0

Share this post


Link to post
Share on other sites
[quote]No. F = ma. a = F/m, and the force is defined as F = G m1m2/r^2, so a = F/m = G m/r^2. This is correct. Gravitational acceleration is independent of the "receiving" object's mass.[/quote]

Yes, you are completely right - But only if you pick the right mass, and I suspect this is where the error lies. Bugs are often harder to find in optimized, compacted code, and that's why I recommend using the original Newtonian force based equation. Also, for the acceleration based calculation to work you need two equations, one for each body. A1 = G m2/r^2 and A2 = G m1/r^2. Otherwise your simulation will not conserve momentum. Sorry if my first post was a bit unclear, I was in a bit of a hurry when writing it.

Cheers,
Mike
1

Share this post


Link to post
Share on other sites
[quote name='Muzzy A' timestamp='1350625985' post='4991689']
I may have to look up tangential velocity then
[/quote]

Calculating the tangential velocity based on orbit parameters like distance/eccentricity:
[url="http://www.gamedev.net/topic/447624-calculating-an-initial-velocity-for-desired-planetary-orbit/page__p__3961958#entry3961958"]http://www.gamedev.n...58#entry3961958[/url]

Periapsis velocity:
v = sqrt((G*M/a)*(1 + e)/(1 - e)).

The planet Mercury's semi-major axis a = 57909176e3 metres, and has an orbit eccentricity e = 0.20563069:
v = sqrt((6.6742e-11 * 1.988435e30 / 57909176e3) * (1 + 0.20563069) / (1 - 0.20563069)),
v = 58976.3015.

That's very close to the maximum orbit speed of Mercury, as 58980 m/s on wikipedia.org.

(Note, 1.988435e30 == Sun's mass)

Oppositely, for the apoapsis velocity:
v = sqrt((G*M/a)*(1 - e)/(1 + e)),
v = 38858.47.

For a circular orbit, the eccentricity is 0.

These calculations help you find the length of the tangential velocity vector. As for finding the direction of the tangential velocity vector, the cross product operation will help you (if your orbit plane is nice and aligned with the coordinate system, it's very straightforward). Edited by taby
1

Share this post


Link to post
Share on other sites
[quote name='Cosmic314' timestamp='1350655499' post='4991780']
You could experiment a little. Take two masses and draw them traveling at some random velocity (this implies direction too). Have a toggle button that either skips the gravitational force calculations or turns it on. When the button is toggled off all objects should continue in a straight line from their last computed velocity. This is an example of one of Newton's laws, an object at rest tends to stay at rest, an object in motion tends to stay in motion unless they are acted upon by an outside force. In particular this motion is linear. Then, click the toggle button to start computing gravitational force on each of the objects and soon they should be moving on a curved trajectory, assuming their velocity has some tangential component relative to the other.

Bacterius is correct. You have to supply the correct tangential velocity.
[/quote]

Already did something similar to that a while back lol.

[quote name='taby' timestamp='1350666413' post='4991827']
[quote name='Muzzy A' timestamp='1350625985' post='4991689']
I may have to look up tangential velocity then
[/quote]

Calculating the tangential velocity based on orbit parameters like distance/eccentricity:
[url="http://www.gamedev.net/topic/447624-calculating-an-initial-velocity-for-desired-planetary-orbit/page__p__3961958#entry3961958"]http://www.gamedev.n...58#entry3961958[/url]

Periapsis velocity:
v = sqrt((G*M/a)*(1 + e)/(1 - e)).

The planet Mercury's semi-major axis a = 57909176e3 metres, and has an orbit eccentricity e = 0.20563069:
v = sqrt((6.6742e-11 * 1.988435e30 / 57909176e3) * (1 + 0.20563069) / (1 - 0.20563069)),
v = 58976.3015.

That's very close to the maximum orbit speed of Mercury, as 58980 m/s on wikipedia.org.

(Note, 1.988435e30 == Sun's mass)

Oppositely, for the apoapsis velocity:
v = sqrt((G*M/a)*(1 - e)/(1 + e)),
v = 38858.47.

For a circular orbit, the eccentricity is 0.

These calculations help you find the length of the tangential velocity vector. As for finding the direction of the tangential velocity vector, the cross product operation will help you (if your orbit plane is nice and aligned with the coordinate system, it's very straightforward).
[/quote]
you're going into orbital elements, which was what i was trying to avoid lol. But i see your logic. Edited by Muzzy A
0

Share this post


Link to post
Share on other sites
Forgot to mention Muzzy, I have loads of smaller code samples with gravitational interaction. I'll post some if you'd like some working examples to look at.

Cheers,
Mike
0

Share this post


Link to post
Share on other sites
[quote name='h4tt3n' timestamp='1350716109' post='4992064']
Forgot to mention Muzzy, I have loads of smaller code samples with gravitational interaction. I'll post some if you'd like some working examples to look at.

Cheers,
Mike
[/quote]

sure, it'll be nice to have something to compare to
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0