You will produce a game in which ball-like particles enter from one side of the screen. You must deflect the balls with a paddle (while conserving momentum) to keep them from hitting your side of the screen. The paddle will be controlled by an analog input. There will be a time limit to the game. Display will be on a TV, with sound effects. The balls will follow standard billards-type dynamics, with zero friction between balls. An example of billard dynamics is shown here and slower.
Procedure:
You will probably want to review the code examples given
in Video Generation with AVR.
Use this example as a basis for your
program. Before compiling, under the Project
menu, choose
Configure...
, then in the Compiler
tab. Set "Optimize
for Speed
", reduce the Data Stack size
to 100 bytes,
and turn off the "char is unsigned
" option. This example uses fixed-point arithmetic in animation.
Build the video DAC shown below and connect it to the yellow connector on the TV using clip leads and a RCA phone jack. Make sure the TV is set to video input. Test the TV connection using the example program linked above. The power supplies on the TVs and development boards are NOT interchangable.
The paddle position will be controlled by a potentiometer hooked to an A/D input on the MCU. Use the following circuit make a user-variable voltage. I suggest setting Vref to Vcc on the A/D converter. This example shows how to set up the A/D converter.
You are going to be programming in the equations of motion for the balls. Remember that the video coordinate system has x increasing to the right and y increasing downward. We will step the billards system forward in time by calculating the total change in velocity from a collision, without worrying exactly how forces change the velocity.
The change in velocity during impact can be derived for frictionless balls of equal mass by noting that the the impact force must act in a direction parallel to the line connecting the centers of the two impacting balls. The change in velocity must be parallel to the connecting line also, with the velocity component parallel to the line having its sign reversed by the collision and the velocity component perpendicular to the line unchanged. Projecting the initial velocity onto the line connecting the centers, negating the result, and resolving it back into x and y velocity components gives the velocity change. If i and j are the indices of the colliding balls, define:
then delta v for ball i is given by the following where the right-most term represents the projection of the velocity onto the line and the other term converts the projection back to x,y coordinates.
The calculation procedure for each time step is:
For each ball i from 1 to n
For each ball j from i+1 to n
Compute rij
if (||rij|| less than 2*(ballRadius) and hitCounter is zero)
Compute vij
Compute Δvi
Add Δvi to vi
Subtract Δvi from vj
Set hitCounter big enough to avoid particle capture
elseif (hitCounter>0)
decrement hitCounter
endif
end
end
When I coded this, I did not bother to calculate the square root of the sum of squares when calculating ||rij
|| (too slow).
Instead, in the if
statement, I just used the approximation that to be within hit range, the absolute value each component of rij
was less than 2*ballRadius
.
When dividing by ||rij
||2 you can use the known value of
(2*ballRadius)2
. In the assignment below, I set ballRadius=2
. vx(t+dt)=vx(t)-vx*drag
and vy(t+dt)=vy-vy*drag
The drag should be small, perhaps drag=0x0001
(in fixed notation).x(t+dt)=x(t)+vx*dt
and y(t+dt)=y(t)+vy*dt
Clearly, v and x all need initial conditions, which you will set, according the specifications below. It is doubtful that you will have enough time between frames to do all of the calculations in floating point. I suggest using 16 bit, signed numbers with the binary point set at the byte boundary. I also suggest scaling velocity so that you can make dt=1, thereby avoiding a multiply. There is an example of a bouncing ball with drag done with fixed point numbers.
The previous analysis is adapted from: Studies in Molecular Dynamics. I. General Method, by B. Alder and T. Wainwright, Journal of Chemical Physics, Vol 31 #2, Aug 1959, pp 459-466. See also Hard-Sphere molecular dynamics. One final project in 2005 used a different scheme to calculate collisions.
You will need your digital camera to document your project.
Write a program in C and possibly assembler for the microcontroller with these specifications:
When you demonstrate the program to a staff member, you should play the game.
Your written lab report should include: