Wednesday, 18 April 2012

Unity Experiment with Light Sensors!

Well as I said yesterday, today I'll post up some images of what I'm doing with using light as a way to control games, just a few though since I'll also put up a video demonstration at some point too.

So, I'll start with the ship model I'm using for my experiment, this is what is being controlled in Unity by the light.  Everything you see in this blog, if not noted otherwise, has been made by me.  I'll endeavour to put links up to any external resources I use too.


Above you see my spaceship model and it's landing pad.  The pad is 1456 triangles and the ship is 2688 triangles.  I'm not really fussed about being topology efficient here or having a specific polygon budget, the main purpose of this ship is to give me something interesting to work with.  I'd much rather work with a spaceship than a cube, making a cube move isn't very exciting but applying something like this model to my experiment makes it a little more interesting and inspiring.  I've yet to texture it, but I'll get round to doing that sometime in the future if I use it for a proper demo or game!

Next is the program I'm using to conduct my experiments.  I'm currently using Unity 3.4.1, as 3.5.1 has bugs in it which means I can't actually carry out my experiments with it.  The specific bug is one where I am not able to get axis data from customised games controllers, which is very annoying, but anyway!  Below is my setup for the experiment, very simple, just the ship and landing pad, a ground plane and two rows of cubes to help tell me how fast I'm moving.  I've also got a light in there too and a couple of cams.




To finish, I have my hardware, in this case what I need to produce different axis/analogue signals for my experiment based on light.  The hardware is as follows: 1 U-HID G, 1 Lilypad light sensor, 3 small brass screws, 3 crocodile/alligator clips, 3 female to male jumper wires and a male Mini USB-B to male USB-A cable.  For those of you who don't know, when I refer to cables by gender, it means whether you can plug them into something or whether they can be plugged into, if you get my meaning!  Below are the items I'm using and how they are set up, I've included close ups of the U-HID G (Top Left) and the Lilypad light sensor (Top Right) .


I've mounted my light sensor on a small wooden based I quickly put together so that later on if I wish I can put longer screws through the wood and have the cables attach to the screws at the bottom instead of the top; just to keep things tidy.  Plus the light sensor is now secure, instead of moving about with the wires that are attached to it as it would if it weren't screwed down.  I will no doubt make something that looks a little cleaner for a final product, something a bit more sci-fi!  In the bottom image you can see everything put together, the three crocodile clips are attached to the screws that touch the three different fixtures/mounting holes of the Lilypad light sensor.  The mounting holes represent 5V power in, Ground and Signal.  The other ends of the crocodile clips are attached to the male to female jumper wires which attach to the U-HID G USB interface board.  The U-HID G is then plugged into a computer via the male Mini USB-B to male USB-A cable.


The U-HID G USB interface board comes with it's own software allowing you to configure it's input/output.  For those of you who don't know what a USB interface board is and didn't read my last entry, it basically allows you to plug different kinds of sensors and output devices such as motors and lights into you computer.  You can then controls the motors and light with your PC or use the sensors attached to the interface board to control different aspects of your computer or a game.  With the U-HID G, only the top left set of nine pins are used.  The top left is orange, the bottom left is purple and the top right is blue.  The U-HID G has only nine pins and the top right is always used for ground shown in blue, you cannot change it, but that works just fine for me.  So, from the labels you can see that orange represents a 5Volt output and purple represent an Analgoue Axis.  Refer to my last entry to learn about analogue axis!  With these pins set to power and ground my light sensor as well as receive it's signal via the cables and crocodile clips, I can now move onto the actual experiment.


The image above shows the basic concept of how this experiment works.  To the left of the image we have my hand holding a small LED torch; in this case our light source.  Below that we have the light sensor that is now plugged into my PC and is acting as a controller.  I will show the code for how this works below.  To the right we have my model spaceship and it's landing pad below it.  The arrows represent movement, in this case it represent me moving my hand up and down which results in the light sensor getting more or less light.  When I move my hand down and the light sensor gets more light, the spaceship moves down and when I move my hand up and the light sensor gets less light, the spaceship moves up.  This way I can control the spaceship by moving my hands and the light source.  I actually made a small light strap for my hand, but I blew out the LED and it went up in smoke, hence the LED torch! 

Anyway, from this image you can now see (if you didn't see it a while ago) what I mean by using a light as a game controller.  This experiment is limited to one light sensor at the moment, I do have another one, but I've been busy working on just one so far.  I also need to get some proper light straps made for my hands so it's easier to control the light before I begin to use two sensors.  As mentioned in my previous entry, the Lilypad sensor can be put in the shade and won't pick up much, if any, light if you are indoors which makes the sensor's default analogue value -1.  This experiment is very basic, there are much larger USB interface boards that could be used which would allow for multipe light sensors to be used, maybe you could use one of each of your hands and feet.  I'm controlling height in my experiment, but anything can be controlled by the light sensor, like rotation or acceleration or rate of fire or.. anything!

Here's the simple bit of code I'm using to control my ship using the light sensor.  You'll need a bit of Unity know how to figure out how this works though!


So that's it for now, that's all of the experiment covered, from the idea to the software to the hardware to the experiment itself.  I'll get working on a short demo video to show it in action, not sure when I will be putting that up though!  I'll have to set my webcam up first then edit together the footage from that and the footage from whatever I use to record my screen, so it could take a while.  Please comment if you're interested in anything you've seen here, I know it's a very long post to follow, but I believe in detailing things as thoroughly as possible =D

Here are some links to the items I mentioned in this entry:

U-HID G (Buy and Use): http://www.u-hid.com/home/uhidg.php
Lilypad Light Sensor (Buy): http://proto-pic.co.uk/lilypad-light-sensor/
Lilypad Light Sensor (Arduino use): http://web.media.mit.edu/~leah/LilyPad/08_sensors.html
Jumper Wires and Croc Clips: http://proto-pic.co.uk/

Hope these help and that you found this all interesting, ttfn!

Tuesday, 17 April 2012

So, tonight is when I make my first blog post.  I'm not going to upload any images or anything just yet, I'll no doubt do some of that tomorrow, but I wanted to get a first post in anyways to just talk a little about what I'm doing.

I actually thought a lot about trying to patent this idea or make people sign an NDA to view it, but you know what?  Screw it, I'd rather have people know what I'm doing and be able to talk about it than keep it "secret" or have people keep quiet about it.  So if you like it, trumpet it about and make sure you at least whistle my name too!

The idea is to use light to control games; yes, it's been done before to an extent.  It's not something entirely new, but no one seems to be taking advantage of it which I can't understand, it's very easy to do and has many cool applications.  So, what do I mean by using light to control games you ask? I'll explain, but first an explanation about axis that will in turn help my own explanation!  Of course, feel free to skip the next paragraph if you know how these things work.  When you use a handheld controller, like one for most kind of video games consoles these days, they have at least one analogue stick.  These analogue sticks have plenty of uses and the way they work is simple:

The stick has two axis that it moves on or two "directions".  The two axis are x (left and right) and y (up and down).  The x and y axis both have a number assigned to them, when the stick is not being used the axis value is 0, because the stick is centred.  When the stick is pushed all the way up or all the way to the right, the axis value is 1 whereas if the stick is pushed all the way down or all the way left the axis value is -1.  If the stick is not pushed all the way in any direction, then the axis value can be anywhere between the number -1 and 1 depending on where the thumb part of the stick is moved to or what angle/direction it is moved towards.  An analogue stick has 360 degrees of rotation normally, so for example if the stick was pushed towards an angle of perhaps 45 degrees then both the x and y axis would have positive values, values above 0.  If the analogue stick was pushed towards an angle of 285, then the x axis would be negative (because the stick is facing left) and the y axis would be positive (because the stick is facing up).

These axis values can then be used by programmers/coders to control certain aspects of a game, whether it be the movement of a character, vehicle or in-game camera.  Thus concludes my explanation of analogue sticks and axis.  Now that you understand a little more about axis, I'll back to my idea of controlling games with light and how that works.  Currently I'm experimenting with a light sensor, they're used in things like corridors where lights need to come on automatically when it gets dark.  By supplying 5 volts of power to one and attaching it to a PC via some form of USB interface board (a USB interface board is like an experimental games controller in this case, you can attach all forms of sensors, lights and motors to it) you can get axis values from it depending on the brightness of the room it's currently in. 

The sensor I'm using is not very powerful, which is perfect, as it doesn't take in light that isn't directly applied to it.  What this means is, even if you have curtains open and light coming into your room, you can put the sensor in a shadow of an object and it won't take in light.  So, instead of moving an analogue stick to change axis values, we now have light.  Covering the sensor up completely gives an axis value of -1 and shining a bright light into the sensor gives an axis value of 1.  The sensor in this case only has one axis since the way the value is changed can only be changed in one way, light or not light.  Using these values is just the same as using axis values from a typical handheld console controller, but way more fun and way more frustrating.  I'll be posting up some images from my test application/game made with Unity tomorrow and then I'll get putting a video demonstration together as well!

For now, I hope this has outlined the work I am currently doing, if you find all of this difficult to understand then keep an eye on this space; the images and video demonstration I will put up will explain it much more easily than words!  Ttfn!