Interactive Media & Game Development
Worcester Polytechnic Institute

IMGD


IMGD-3100: Novel Interfaces for Interactive Environments
Project 2
Due: Tuesday, September 10, 2013 at 6pm

Objective: THIS PROJECT SHOULD BE DONE INDIVIDUALLY!

In this project, you will implement bidirectional serial communication between your Arduino and an application: a game using the Unity game engine.


Where to get parts If you need common components, you can go to the ECE shop in Atwater-Kent, Room 112. They have lots of small parts, such as all kinds of resistors, capacitors, etc. They know you are coming. You will need to pay them (cash) for the parts you need. Also, there are links at the bottom of the course Web page to other sources of parts.

What to do: The project objective is to help you understand how communication between a serial device and piece of software happens. In this case, the application is a video game, but the approach is general enough for you to use for just about any piece of software. For this you are going to build a joystick with a single axis of control and a button, and integrate the device into a simple spaceship game in Unity. The game is already implemented, and the source code will be provided to you.

Here is a Web version of the game. Use the arrow keys to play.

  1. Part 0: Project Materials
    The game to be used in this project consists of a 3D spaceship hovering over the sea surface. The spaceship must pass by an infinite number of "gates" that appear in front of it, like a slalom skier.

    In order to run this game, you will need to install the free version of Unity into your account, or use one of the lab machines. During the installation process, make sure to select an installation directory from your own private account or your desktop. You might get a warning saying that you should have administrative priviledges in order to install the engine, but Unity should correctly install anyway. After the installation, you should be able to run Unity and the island demo that comes with it.

    Next, you are going to download and run the game, contained in this ZIP file and whose password was provided in class. Extract the ZIP file for the game in a directory of your choice. In Unity, click in "File -> Open Scene". Go to the extracted game folder and open the scene "WaterWorld" located in the subfolder "Assets -> ShipGame -> Scenes". By pressing the play button on top of the Unity interface you should be able to control the spaceship by using the arrow keys on the keyboard.

    As the last step of this preparation, you are going to donwload example files that will serve as a starting point for the serial communication you are going to implement:

    1. Unity code example: This ZIP contains a sample C# project with code that simulates what Unity is going to do in order to be able to send requests and information to the Arduino. This code was built and run in Visual Studio 2005, but "Program.cs" should also be able to be inserted into a default console project and run in other IDEs as well.
    2. Arduino code example: this ZIP contains Arduino code that listens to the COM port to which the board is connected and sends a default "Hello" message in response to the "Hello" message sent by the Unity code example above. This code should be directly loaded, compiled, uploaded and run under the Arduino IDE.

    First, compile and upload the Arduino code to your board and then run the Unity code example. Do not forget to set the right COM port in the Unity example. By running the Unity code example with the Unity IDE's console window open, you should see something like this:

    
              "hello!"
              "Hello, Unity!"
            

    Once you have run the code example, you should have all you need to start implementing the project iself.

  2. Part 1: Moving Forward
    In this part, you will use the button from your kit to activate the thrust of the spaceship. While the button is pressed, the spaceship should move forward. So your Arduino button should replace the up arrow key button on the keyboard.

    Next, you are going to use the potentiometer to replace the left and right arrow keys. If you twist the potentiometer to the left, your spaceship should increasingly turn and move to the left. If you twist the potentiometer to the right, your spaceship should increasingly turn and move to the right. If the potentiometer is twisted to approximately the middle of its twist range, the spaceship should not turn at all. Make sure you carefully adjust the range of values for these three possible states. It is ok if you don't use the full range of values provided by the potentiometer to control your spaceship as long as your controller provides the player with good control for the spaceship, preferrably better than the keyboard arrow keys.

    To implement the changes in the spaceship behavior, you are going to change the file "ShipMovementController" located in the subfolder "Assets -> ShipGame -> Scripts". This script can also be directly accessed from inside Unity. Firstly, open the game scene in Unity. Then access this script by clicking on "Player" on the "Hierarchy" tab, and then double-clicking on the script's small icon beside the "script" option in the "Inspector" tab. This and all other scripts for this game are written in C#.

    For this sub-project, the equipment you are going to need is:

    1. Potentiometer
    2. Button
    3. 2 x 330Ohm resistors for the potentiometer to prevent short-circuits
  3. Part 2: Sound Feedback
    You are going to make the Arduino play a sound whenever the spaceship sucessfully passes through a gate. For that, you will have to make changes to the script "CheckpointFieldScript" located in the same subfolder as all other scripts ("Assets -> ShipGame -> Scripts"). Once again, this script, can be accessed from inside Unity once the game scene is opened. Make sure you implement a decent sound effect, with at least three distinct beeps, not necessarily with different pitches, but that can provide the player with good feedback from the game.

    For this sub-project the equipment you are going to need is:

    1. The buzzer
    2. A 330Ohm resistor for the buzzer to prevent short-circuits.

    References for how to play sounds with Arduinos are the same as in Project 1:

    Extra points: Alert the player with a sound or a bunch of LEDs - or whatever else you can imagine - whenever the spaceship collides with a gate post.


Attacking
the Problem:
Your goal here should be to understand how serial communication works between the Arduino and an application.

Find a simple way to represent different messages using a header byte and a body for your messages.
Hint: To make your life a little easier read about these methods in C#:

  • Convert.ToString and Convert.ToDouble
  • Debug.Log(string s).

Before you even touch the Unity part, you should make sure you understand what is going on with the sample serial communication code.
Once you understand this, you can focus on the (non-trivial) task of integrating it with the Unity code.


What to Submit: All documents are to be submitted electronically via turnin by 6:00 pm on the day the assignment is due.

The following items should be submitted for this project:

  1. The code for each (sub)project part
  2. The circuit schematics for each (sub)project
  3. Photographs of your circuit (you can get a digital camera from the ATC for this)
  4. A brief description of any work you did to earn you extra points
  5. A brief report on problems encountered during the implementation of the project and what approaches were used as final solutions.
Name each file something that makes sense.

When you are ready to submit, zip up all your files into a single archive file.
Name the file LastnameFirstname_proj2.zip.

You will use the new Web-based "Turnin" facility to submit your work. Information about submitting can be found here:

http://web.cs.wpi.edu/~kfisler/turnin.html.

Your WPI user ID should be used to login, and you should have been emailed a password.
The Turnin assignment ID is proj2.


Academic
Honesty:
Remember the policy on Academic Honesty: You may discuss the project with others, but you are to do your own work. The official WPI statement for Academic Honesty can be accessed HERE.


Back to course page.