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!

No comments:

Post a Comment