# Archive for February, 2012

One of the fun results from my computer graphics class last semester was our computer animation project. The assignment was to model the firing of a cannon, including the cannon recoil and the projectile’s parabolic trajectory. The end goal was to use this model to drive a 3D animation.

I worked with 2 other group members, and in all I spent 54 hours in group meetings and coding individually. The project resulted in about 4000 lines of code in 34 Java files.[1]

## Physics modeling

For each frame of our animation, we make slight changes to the position of each object on screen. These small changes are based on simple physics calculations.

For example, for a wheel to roll to the left, it needs to both rotate counter-clockwise and move to the left. The angle of rotation $\Delta\theta$ is related to the shift to the left $\Delta s$ by the following equation:

$\Delta s=r\Delta\theta$

where $r$ is the radius of the wheel.

Firing the projectile gives the projectile some initial velocity. This velocity can be used to compute a change in position for the projectile. The velocity changes over time due to acceleration due to gravity.

## 2D animation

First, we produced a 2D animation of this model. This involved several steps:

1. Read in 3 objects : the projectile, and the wheel and the barrel which make up the cannon. These objects can be drawn on-screen in a certain order so the layering appears correct
2. Move them to their initial position.
3. For each screen refresh, update the positions of the objects based on the equations.

Initially, the projectile has 0 velocity, so item #3 above really means that nothing will change in each screen refresh. As soon as the user presses the fire key, the projectile is given a fixed amount of velocity (and the wheel is given an amount of recoil velocity).

## 3D animation

Making the 3D animation was the more challenging part of the project. While the 2D animation was written in about 10 hours, the 3D animation took 44 hours to write, including a 13 hour rabbit trail. I spent about 13 hours working on a version that ended up not working.

The 3D animation extends the work of the 2D animation. Instead of a 2D wheel, barrel, and projectile, we now have 3D objects. These objects are first generated and then stored in .dat files. There are now 6 objects: the ground, 2 wheels, the axle, the barrel, and the projectile.

Based on the source code from our textbook, the 3D objects are put into a scene which can be viewed from different perspectives. We added code to rotate and move individual objects based on the amounts specified by our physics model.

Here is a demonstration:

The video demonstrates two methods of rendering that our 3D cannon animation supports. The first is z-buffering, and the second is hidden line elimination. These two methods are described in my first post about computer graphics class.

[1] This is a raw count, produced by doing wc -l *.java. Not all 4000 lines are original code for this project. Some of this code was first implemented in our textbook,  Computer Graphics for Java Programmers, 2nd ed., by Ammeraal and Zhang. There are also 6 Java programs for generating the data files for the various on-screen objects. These Java programs have a lot of code duplication.