HTML and CSS Reference
In-Depth Information
This should look familiar, because it's the first diagram! You can easily solve this using the plug-and-play
momentum formula. When you apply the formula, you wind up with two new vx values. Remember that the
vy values never change, but the alteration of the vx alone changed the overall velocity to look something
Figure 11-7.
Figure 11-7. New x velocities and the same y velocities with the result of a new overall velocity
Now you just rotate everything back again, as shown in Figure 11-8, and you have the final real vx and vy
for each ball.
Figure 11-8. Everything rotated back
That's what the process looks like as a diagram, but now we have to convert all this into code.
Writing the Code
To begin, you create a base document that enables two balls to move at any angle and eventually hit each
other. Starting off with the same setup as before, you have two Ball instances: ball0 and ball1 . Let's
make them a little larger now, as shown in Figure 11-9, so there's a good chance of them bumping into
each other.
 
Search WWH ::




Custom Search