Saturday, March 22, 2014

Hacking shoes for kids education (almost) – part 2: events in Unity3D

This is the second part of my report about the Code4Play hackathon. You can read part 1 here.

Let's dig into some technical details!

The FootMoov SDK includes a demo Unity project, that displays the current sensor values. The main Controller class exposes raw sensor data expressed as integer values.

To ease the job of the team on the game side, I wanted to wrap the given API with an event dispatcher. You can make components talk in different ways in Unity, but events represent the most efficient pattern for this purpose.

I never used events in C# before, but I was able to make a working abstraction layer in a reasonable time.

The behaviour is pretty simple: I publish a tap event when the pressure sensor passes a threshold value with a certain velocity.

Here are the distilled instructions of what you need to create an event dispatcher in Unity3D and, more in general, in C#.

The basic components are:
  • event delegates
  • events
  • listeners

First of all, you must define the delegate. It's a special C# type which is a sort of function wrapper. You can use the wrapped function as a callback.
public delegate void OnFrontTapEvent(object sender);
Then you create the event object, which is also part of C# syntax. When you define this object, you use the event keyword and the above-mentioned delegate as its type.
private event OnFrontTapEvent OnFrontTap;
The event acts as a container. You can add listeners to it with the += symbol.
OnFrontTap += new OnFrontTapEvent (listener.OnFrontTap);
The event is triggered by calling it like a method. This causes all the attached listeners to be called.
OnFrontTap (this);
You can find the complete code in the GitHub repo of the game projects. In particular, FootMoovAdapter is the class which uses the event system.

Be careful with scopes and locations where these components are initialised!

Hacking shoes for kids education (almost)

Code4Play is a hackathon that took place last Saturday in Milan. The goal was to hack old toys and recyclable material into innovative educational games. Many of the teams used Arduino and Raspberry Pi to give smartness even to the the simplest plastic toy.
The hackable toys

The project that I liked most is a game that teaches kids mathematical operations. It consists of a Raspberry Pi in a box, attached to a big four-pin piece of a block-building game. When you shake the box, it says something like "Compose the number... Six!". The game pieces represent numbers and mathematical symbols, and you have to match them on top of the box to produce the given number. The system is able to recognise every single block by the value of the resistance wired to it.

So, what did my team do? We decided to use a pair of FootMoov, a set of shoes with pressure and gyroscope sensors. They came together with an SDK for Unity, which we used as platform for the two games we developed.
Our team name and logo on a balloon. Many thanks to the girl who kindly drew it!
Unfortunately, we did not find inspiration for an educational game. Yet, we took this opportunity to create two games with this uncommon controller.
The first game is FlappyPong. It's like Pong, but you tap on the shoe to control one of the bars which jumps like Flappy Bird on every hit.
The second game was a bit different: on the screen you see the image of an animal and the morse alphabet. You have to use the front and back pressure sensors of the shoe as dot and dash to compose the name of the animal in morse code. Maybe a bit awkward to play, but it was funny to show at our presentation.
In the next post I will dig more into the technical details.