You will produce a game in which ball-like particles enter from one side of the screen. You must catch the balls with a collector to get points. The collector position will be controlled by an analog input. There will be a time limit to the game. Display will be on an 320x240 TFT LCD, 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.
The sound will be produced through the Vref output using a DMA channel.
Procedure:
In this lab and every lab, make sure you are running Protothreads 1_2_1!
There is fixed point animation example on the Protothreads page, including forces for gravity and drag. But note that the TFT display must be moved from SPI channel 1 to SPI channel 2 so that you can use the Vref output. Tahmid thoughtfully converted the TFT code to work with SPI2. Download tft_master_spi2.c and include it in the project, and, of course, remove the SPI1 master from the project. Move the TFT SCK input from pin 25 to pin 26 on the PIC32. All other TFT connections from lab 2 stay the same.
Use pins:
0 to 0.67*(AVDD–AVSS)
V
(where V
is 4 bits) load CVRCON with 0x806V
The game will be controlled by a trimpot potentiometer hooked
to an ADC input on the PIC32. Use the circuit to the left to
make a user-variable voltage. The Protothreads page shows how to set up the A/D converter to read a voltage in a thread.
Trimpot schematic: bottom view: image:
An example which reads the AN11 analog input and draws the voltage on the TFT using the SPI2 master and Protothreads 1.2 is ZIPPED here.
Dynamics:
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 approximate rij by checking:
if Δx and Δy are both less than 4
if (||rij||2 less than (2*(ballRadius))2 and hitCounteri is zero)
Compute vij
Compute Δvi
Add Δvi to vi
Subtract Δvi from vj
Set hitCounteri big enough to avoid particle capture
elseif (hitCounteri>0)
decrement hitCounteri
endif
endif
endfor
endfor
When I coded this, I did not bother to calculate the square root of the sum of squares when calculating ||rij
|| (too slow). Compare the squares.rij
||2 you can use the known value of (2*ballRadius)2
. In the assignment below, I set ballRadius=2
. This is equivalent to a shift-right by 4-bits.vx(t+dt)=vx(t)-vx*drag
and vy(t+dt)=vy-vy*drag
The drag should be small, perhaps drag=0.001
( but converted to 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 32 bit, signed numbers with the binary point set at the 16-bit boundary. I also suggest scaling velocity so that you can make dt=1, thereby avoiding a multiply. You can think of this as: (1) Units of distance are PIXELS, (2) Units of velocity are PIXELS/FRAME. As examples, any of the NTSC particle systems are 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.
Results:
2011: video
2014: video Shiva Rajagopal and Richard Quan
2016: video
Write a program in C using ProtoThreads with these specifications:
When you demonstrate the program to a staff member, you should play the game.
At no time during the demo should you need to press RESET.
Your written lab report should include the sections mentioned in the policy page, and :