Skip to content
rsanchez1 edited this page Sep 14, 2010 · 1 revision

Gravity

Grav is an n-body simulation that implements a fourth-order Runge-Kutta method to integrate Newton’s equations of motion. Basically, this method takes the derivatives of the path a body would take given its current position, velocity, and acceleration due to gravity. Four derivatives are taken between the current position and the next position due to the body’s velocity, the four derivatives are added up, and their weighted average is taken. This weighted average is added to the current position and velocity of each body for each time-step.

Collisions

While Grav integrates the motion of each body, it also checks if any two bodies are intersecting. Any bodies that are intersecting have their indexes in the main bodies array recorded in two separate arrays: one array for the more massive body of the pair and the other for the less massive. The collision detection technique used here is to add the radii of both bodies, check if the combined radius is greater than the distance squared, and designate the bodies as colliding if this condition holds. After Grav takes all derivatives but before updating the position and velocity of each body, it performs the collision response.

Grav has two methods for collision response: combine both bodies (currently disabled), and collide both bodies. For combining, it basically sums the momenta of both bodies, applies this new momentum to the new body, and increases the radius based on a constant density and the combined masses of the bodies. For collisions, Grav first calculates the minimum translation distance, or the minimum distance both bodies need to move, and separates the bodies based on this distance and based on their respective masses (if the masses are equal, they move the same distance, otherwise the smaller body always moves further than the larger one). After separation, Grav calculates the impact speed and collision impulse, then changes the velocities of both bodies according to how much momentum was transferred between both bodies. Grav conserves momentum in this step, which can be seen by selecting the “Newton’s Gravity Cradle” system in the simulation.

Rendering

To be continued…

Clone this wiki locally