I''m in the process of writing a game, where there are a number of planets floating about, with various ships, and other space debris. I''d like to have the ships attrache to the planets, the planets to go around in a realistic way, and be able to calcuate the amount things should move based on the amount of force applied to them. My question is, how accurate should I be? Should I use the full equations from my physics book, and do the calculus for each object, or will this take to long? I guess my question is, where do I use the real equations, and where do I go for the hacks to save cpu time?
Well, for orbits etc, you could just set the planets going around in a circle, and use trig to find the next position it''s supposed to be etc. The correct way would be to use the gravitaional equation with centripetal acceleration, which would also produce spiralling decents into planets/suns etc if the orbiting objects are not going fast enough aswell as orbits ( all though setting up an orbit would be more differcult ). As for gravitational attraction, I can see no other way other than using the gravitational equation. It''s not complicated, so I can''t see it eating up the many CPU cycles either. You could also optimize it by discounting the gravitational force from bodies that are further than a designated distance away etc.
For your game, you want a stable dynamical system. If you were to simulate each of your celestial objects and the interactions between them all, it would be extremely difficult to produce a system that did not decay or explode (ie all come crashing together or explode off into space). Escpecially given rounding errors in computers!
So, if you want to simulate a bunch of planets, asteroids and objects, then you''re going to have to cheat.
What you can do is this.
1) Simulate the orbits of objects using Keplar''s laws. These state that: 1) The path of an orbit of one object around another is an ellipse, where the central object is located at a focus of the ellipse; 2) That the straight line joining the two objects sweeps out equal areas in equal length timesteps; and, 3) that the square of the period of the orbit is proportional to the cube of the average radius of orbit. The average radius is equal to the length of the semi-major axis of the ellipse (which is just half the length of the major (longest) axis of the elipse).
You can use these facts to simulate objects orbiting one another where the mass of one object is significantly larger than the other. Works for planets and asteroids orbiting stars, space ships orbiting planets, stars or large asteroids, etc,...
Now, when you want to figure out how the motion of a space ship is perturbed by the gravitational effect of a bunch of planets, then you need to compute the force applied on the ship by each planet. Each planet exerts a force inversely proportional to the square of the distance between it and the space ship. The actual equation is:
F = GM1M2/R2
where: G is the Universal Gravitational Constant M1 is the mass of object one M2 is the mass of object two R the distance between them.
Project all forces as acting radially outward (in 3-D space) from the space ship and sum them to find the resultant force. Add to this the force generated by the space ships engines and you have the resultant force for the direction of motion of the ship. From this compute the acceleration and use a suitable integration routine to compute the new position of the space ship given its current position.
At each time step of the integration you will need to update the ships position and that of all of the planets. The computational cost of this method will be directly proportional to the number of celestial objects you are simulation.