Step 1: Step 1. What's Our Concept? Part A
But we wanted something more.
As a result, I decided to create a virtual space pilot game for Ben. The only guiding principles I wanted to stand by are:
1. It had to be a cooperative game- meaning 2 players had to play together.
2. It had to be fun and interesting
3. It had to be geared towards 5 year olds
4. It had to play quick, so we could rotate through kids pretty rapidly at a birthday party.
Step 2: Step 2. What's Our Concept? Part B
1. You are in a spaceship, defending your solar system from invading alien spacecraft
2. While your field of view is 3D, your play area is 2 dimensional, roughly in line with the planets in the solar system (I felt that 3D piloting would just be too hard for a 5 year old pilot)
3. The game will be levelled. Each level will mean that an additional alien spacecraft spawns, and it will be faster (so will their shots)
4. When the spacecraft spawn, it will be in a random place, at a minimum distance from where you are.
5. You start with 3 shields. Each strike on your ship, either from an alien spacecraft or one of their shots, will knock off a shield. if you have no shields, a strike ends the game.
6. The only object you can run into is the sun. You'll get verbal warnings before this happens, but if it does, the game is over no matter how many shields you have.
That was pretty much all I started out with, figuring time was short and everything else would become obvious as I developed it. Turns out, I was able to stick to this pretty closely. Read on to find out how!
Step 3: The Spaceship
Step 4: The Hardware Part 1: the Laser
This puts us at .001S exposure time if you stick your eye on the front of the laser (it grows in area from the front of the laser, so it lessens in intensity), and the time increases a lot as the beam diverges towards the screen (where the area is about .8cm^2 and the power number you use is .006). Note this number is for class 1 MPE, which means it is 10% of the dose at which you have a 50% chance of incurring damage.
If this bothers you, no problem. Stick with a visible laser! The hardware I have outlined here will work fine with any color laser, you'll just have to set the sensor threshold higher to block room light and whatever color your projector is throwing.
Step 5: The Hardware Part II: Locating the Laser
The quick answer is: A camera.
Turns out, as long as there's not too much extra infrared light in your room, you just have to put a nice IR pass filter over your average everyday cheap CCD camera, and you've got a detection device! This picture below shows the camera that I used, mounted next to my projector.
Next, I'll detail the hardware that I designed to translate what the camera is telling us in a way that the PC can understand.
Step 6: The Hardware Part III: an Arduino-based Translator
Here's how it works. composite TV signals are all timing based, and interlaced. I used an LM1881 NTSC decoder to tell the AVR if we were on an odd or even line, as well as when the Hsync and Vsync occurred. For those of you not electronically inclined, I used the LM1881 to give me an index of where we were getting picture information from. The laser is filtered by U1, which I'm just comparing to the voltage at the wiper of R3 to pick up the first pixel on screen bright enough to pass my manual threshold. Hopefully, this is our laser. The AVR picks this up as an interrupt, and remembers the time from the last reported Hsync. We've been counting vertical lines since the Vsync, so now we have an exact position (well, +/- quarter inch). Interested in the source? Here it is:
The end product of this board is to register as a serial port at 115200, and crank out 2 bytes (X and Y) every time it sees a pixel bright enough to be considered a laser. You set the threshold with R3.
Other pictures on this step illustrate NTSC composite signal information, and a picture of the final PCB.
Step 7: The Software Part I: Choosing a 3D Engine
So I started downloading, compiling, and looking into some of the bigger free 3D engines available like Ogre, Crystal Space, and Irrlicht. My home theater PC is linux based (Mythbuntu) so I needed to be able to compile for that.
After a bit of research and tinkering, I chose Irrlicht. Mostly because it seemed to have a quicker learning curve on getting started, and the folks on the Irrlicht forums are absolutely great at handling questions from newbies and experienced programmers alike.
Although I have written various assembly and C (although mostly embedded stuff) for years, this was to be my first adventure in C++. What's the best way to learn a new language? Try something in it!
I grabbed one of the simple examples, tore out the old code and in no time at all I had a gray world full of spheres that I could create on the fly, and navigate around (and through) them using the mouse and keyboard.
Step 8: The Software Part II: Try Something!
I started by creating the skybox in Gimp (GNU Image Manipulation Program- it's free! Try it!) using this tutorial:
Next, I turned it into a cubemapped skybox by making it seamless using this tutorial:
At first I thought I would simply hang the planets in a static location, with the sun in the middle of the universe, but it turns out that with Irrlicht, it was very easy to create the planets, and to make those planets circle the sun. It was also easy to create moons, and assign those moons to a planet so they would orbit the planet. Of course, it's way faster than real time, in that it only takes about a minute for the earth to completely orbit the sun. To see exactly how I did this, proceed to the end of this instructable and I'll provide the source code which remains in the final version of the game.
How about those planet textures? THOSE aren't mine. With permission, I used the 'Free to Download' versions of James Hastings-Trew's planet textures, and they are terrific. He even had a few moon shapes and textures (Phobos and Deimos) which I put in orbit around Mars. You can find those here. For the remainder of the moons, I fabricated imaginary textures using LunarCell by FlamingPear software. This is a plugin for Photoshop or Gimp (I'm using the gimp) that allows you to make imaginary planet and moon textures based on features and factors that you choose. There's a full version, but I limited my use to the time limited demo.
Step 9: The Art
The alien bullets I created from scratch in the gimp. I created a blank transparent image, then followed THIS lightning tutorial, then superimposed a few of these on top of each other. Afterwards, I superimposed a gradient flare in the center. After creating 3 or 4 of these, I had a nice graphic that I could rotate through to look like an animated electric ball. A little 'zzzt' sound from freesound.org we have a scary alien bullet.
Next was the guided missiles. I created these pretty quickly as static 3d models in Blender (picture below). Same for the alien spaceraft.
So now I needed some cool hyperspace and explosion animations. How to do this? Animated billboard. How to create it? LHfire, by LordHavoc (originally created for the darkplaces engine)
And finally, lumped in with the art, is the sound. I used drPetter's sfxr for many of the effects sounds, freesound.org for some of the more complicated ones, and I recorded my family for voiceovers and wingmen ("Captain! You're getting too close to the sun!")
Step 10: The Animation
So how does the animation work? How about that nice hyperspace flare when alien ships are warping in? And those animated explosions and electric bullets? All of these are done with an animated billboard. You load a series of pictures (usually in a format that supports transparency) and tell the engine how fast to cycle through them. The billboard will always be oriented towards you, so it's very easy to make the object or event seem 3D instead of 2D, even if you're moving.
Step 11: Pulling It All Together
And here's a video of the game in action. Please excuse the poor video, I just pointed a flip at the screen:
The birthday was a blast! The kids automatically lined up behind the spaceship, and took turns as gunner and pilot. The game lasted an average of about 5 minutes, the kids would switch to the next position, and play would continue.
Step 12: The Source
You'll need to get irrlicht here:
You'll need IrrKlang (Sound engine):
You'll need the svn source. Go where you want it to populate, and from the command line type:
svn co https://alienplanetwars.svn.sourceforge.net/svnroot/alienplanetwars alienplanetwars
You'll need the source for the hit detect board
That's it! Got any questions, fire away!