Game Development Reference
In-Depth Information
if ( distance <= 2.0*sphereRadius ) {
double newVx1 = (mass1 - e*mass2)*vx1*tmp +
(1.0 + e)*mass2*vx2*tmp;
double newVx2 = (1.0 + e)*mass1*vx1*tmp +
(mass2 - e*mass1)*vx2*tmp;
vx1 = newVx1;
vx2 = newVx2;
}
The new location of the spheres is calculated and the display is updated. The simulation is
stopped if either sphere hits the outer edge of the display area.
// Compute the new location of the spheres.
double timeIncrement = 0.07;
x1 = x1 + timeIncrement*vx1;
x2 = x2 + timeIncrement*vx2;
// Update the display.
updateDisplay();
// If either of the spheres hits the outer edge
// of the display, stop the simulation.
Graphics g = drawingPanel.getGraphics();
int width = drawingPanel.getWidth() - 1;
int height = drawingPanel.getHeight() - 1;
if ( 10.0*(x1 + sphereRadius) > width ||
10.0*(x1 - sphereRadius) < 0.0 ||
10.0*(x2 + sphereRadius) > width ||
10.0*(x2 - sphereRadius) < 0.0 ) {
gameTimer.stop();
}
}
}
}
Play around with the Linear Collision Simulator. Change the input parameters around and
see what the spheres do after they collide. Set the coefficient of restitution to zero and see what
happens to the spheres.
General Two-Dimensional Collisions
The equations developed so far in this section have been for a two-body collision in which the
line of action is parallel to the x-axis. But what are equations for a general two-body collision
where the line of action can have any orientation? The fortunate answer is that the same equations
Search WWH ::




Custom Search