Monthly Archives: July 2009

Orthographic Camera in Unity

Unity is an amazing tool and engine for making games. It is specifically designed for making 3D games, but since 2D is just a subset of 3D it is still incredibly powerful for 2D games. While looking for advice on making 2D games in Unity, I came across a very helpful post by Josh Sutphin. He has a lot of great advice there, and with a few tweaks his UV mapping algorithm helped me a ton.

His explanation of the orthographic camera mode is a bit confusing though. I looked for a while trying to find a good explanation of how this camera mode works. Josh has two in his post, the first of which is a good top level explanation of what’s going on:

“The orthographic size expresses how many world units are contained in the top half of the camera projection. For example, if you set an orthographic size of 5, then the vertical extents of the viewport will contain exactly 10 units of world space. (The horizontal extents are dependent on the display aspect ratio.)”

Now for a concrete example of what he’s saying:

Orthographic camera settings

Here we have a camera set to orthographic perspective at 0, 0, -10 with no rotation and the orthographic size is set to 5. Therefore it is pointing down positive z, with (0, 0) at the center of the screen. Positive x goes right and positive y goes up the screen. Since the orthographic size is 5, it means you have 5 world units above and below the x-axis. If you are using a 1×1 quad, you will be able to fit 10 of them vertically from y = 4.5 to y = -4.5 (the quad is positioned by its center, so this would put the top of the top quad at y = 5 and the bottom of the bottom quad at y = -5.) If your aspect ratio is 4:3, you will be able to fit 10 * (4/3) or 13.33 of your 1×1 quads horizontally, from x = -6.133 to 6.133. As a side note – changing the aspect ratio of Unity’s Game mode to the same that you’re developing for is really useful and can be done with a drop down in the upper left when you’re in the Game mode. This will also change the aspect ratio of the camera’s preview window.

Aspect Ratio setting

Now that you have the size figured out, you will want to decide how many pixels one world unit will be. For Unity you should be using textures that have height and width measurements that are a power of 2. Since you’ll be dividing by the height and width of your texture to get UV coordinates, your sprites should also be sized to powers of 2 to avoid floating point errors. In Josh’s post, he uses a ratio of 64 pixels to 1 world unit because he uses 64×64 pixel sprites. This means that his 64×64 pixel sprites map directly onto his 1×1 quad. If he had a sprite that was 128×64 pixels, he would scale his quad to be 2×1. This isn’t a Unity setting, it is just a convention that you will use when sizing your world objects to make sure your sprites will not be scaled or distorted.

Posted in Games, Unity | Leave a comment

Human Detection

I used to have a CAPTCHA on my blog to try to prevent comment spam. It didn’t work. I’m sure you’ve noticed that CAPTCHAs are becoming more and more difficult to read; this is “necessary” because computers are getting better and better at figuring them out. The CAPTCHA I had was very simple, and it has become clear that the bots could easily bypass it. That’s why I’m excited to update to a game as my human test. It’s an intuitive and cultural test rather than a test of reading skills.

Now, if you try to comment on a page, a little game will pop up before the comment goes through. That pop-up will ask you to do something like: “give the players their sporting equipment” or “feed the food to the baby.” Yes, it’s a little more time consuming for you, but it only takes a few seconds to play and it saves me from mounds of spam! In fact, here in Australia, it take longer for the game to load than for me to complete it.

I think this is a super cool way to filter humans from robots. And when robots can get around this, I’ll just be excited that we’ve created an AI that can complete this kind of cultural test!

Posted in Blog | 3 Comments

Australia (Oz)

I’ve started a grand adventure: my girlfriend and I have moved to Australia. She will be doing genetics research similar to what she did in the States while I pursue some independent game development. I will be creating a new page (Oz) and separating my blog for this purpose. With all the extra time I will have, I’m going to try to spruce up the place a bit and improve my site. I will try to write to the technical side of my blog more often, but since I am going to be doing a lot of game development these posts may end up focusing more on my games and game design challenges.

We have been in Australia for a week now, and it has been a crazy whirlwind. I have managed to write a few blog posts covering the some of what has happened. The first of those will follow (pre-dated) in 3…. 2…. 1….

Posted in Uncategorized | Leave a comment

CodeMash 2012

I’m back from CodeMash 2012 and it was great! I want to give a huge thank you to all of the organizers for putting so much time into making such a great conference. I also want to thank them again for the opportunity to speak.

Thank you to all of you who came out to my talk, it was a lot of fun giving it and I hope you took something useful away. I’ve posted my slides and demo code to BitBucket mercurial here:

https://bitbucket.org/benbarefield/kinectcm2012

The HoverBugz code will be available on the SRT website in the next couple of days. If it’s not there yet, check back soon.

Posted in Uncategorized | Leave a comment

Answering a Couple Scala.Net questions

Bill Wagner recently asked me a couple of Scala.Net questions after reading my recent blog posts.

What is the debugging story for Scala.Net?

A pdb file is required for debugging in Visual Studio, just like with C#. The pdb file contains all of the debugging symbols that Visual Studio uses to track the run of the program. A pdb file can be generated when running ilasm:

ilasm /PDB <MSIL file>

After running ilasm, open Visual Studio and create a new Blank Solution (found under Other Project Types –> Visual Studio Solutions.) Then right click the Solution and click Add –> Existing Project. Change the filter to show executable files and add the executable generated by ilasm. Now the executable can be run through Visual Studio in various ways; for example, right click the executable –> Debug –> Start new instance.

Before running the executable, make sure that “enable address level debugging” is set by going to Debug –> Options and Settings.

A break point can be set in any of the Scala files associated with the executable by opening the file in Visual Studio (File –> Open) and setting a break point as if it was any other .Net language file.

If you don’t need a using statement, does that mean that all the Scala code goes into the global namespace?

In my Calling Scala from C# post, I said:

… [it] surprisingly doesn’t require any extra using statements

The reason a using statement was not required was because I did not put the Scala code I wrote into a package. If I had, a using statement would have been needed for the package.

For example, if my Scala code has been:

package Speaker
class ScalaSpeaker {
    def Speak() {
        println("Hello");
    }
    def Speak(name: String) {
        println("Hi, " + name + ", glad you could join us!")
    }
}

Then my C# would need “using Speaker;”

Posted in .Net, Scala | Leave a comment

Scala XNA

One of the things that I thought would be a really cool demonstration of Scala.Net was to have an XNA application that was coded completely in Scala. I have mixed news on the subject. I did successfully get an XNA window opened and the game loop running. However, as far as I can tell, all of the ways to draw in XNA rely on generics, which are not yet supported in Scala.Net.

After a short email exchange with Miguel, I was able to get Scala compiling against some of the XNA assemblies. This allowed me to extend the Game class and open an XNA window. The problem I was having was compiling the Scala against multiple dependent dlls. I expected the “Xassem-path”argument to take multiple parameters, one for each of the assemblies. Instead, the solution was to add another –Xassem-extdirs path that contained the XNA assemblies (and mscorlib.) My compile looks like:

<Scala.Net directory>\bin\scalacompiler.exe 
–target:msil 
–Xassem-extdirs <Scala.Net directory>\bin
-Xassem-extdirs <directory with XNA assemblies>
-Xshow-class <Scala class with main defined>
-Xassem-name <Name of MSIL file to output>
<scala files>

After running ilasm on the MSIL file generated, corflags needs to be run on the resulting executable to put it into 32 bit mode. Running the executable opens the game window.

This is certainly a proof of concept; unfortunately that is as far as I could reasonably get. Miguel seems committed to getting Generics support in Scala.Net, so it should be possible to build a full game soon.

Posted in .Net, Scala, XNA | Leave a comment

x86 .Net Assembly, Scala, and a x64 processor. Oh My!

While playing around with Scala.Net today I ran into an interesting problem. I was trying to build some Scala code against an assembly built for x86 and I got an exception when I ran the executable:

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly […]. An attempt was made to load a program with an incorrect format…

After a bunch of internet searches, and some help from Bill Wagner, I got this working. The executable needs to be set to run in 32 bit mode. This can be done with the corflags tool. corflags will exist in your path from a Visual Studio terminal. The trick is:

corflags <Executable name> /32Bit+

After that everything worked.

Posted in .Net, Scala | Leave a comment

Calling Scala from C#

Yesterday, I figured out how to call some C# code from Scala. It is a small leap to then call Scala from C#. The process just gets turned around: instead of creating a C# class library and referencing it from Scala, you create a dll from Scala code and reference it in .Net.

First, I built a class in Scala similar to my C# class from yesterday:

class ScalaSpeaker {

    def Speak() {

        println("Hello");

    }

    

    def Speak(name: String) {

        println("Hi, " + name + ", glad you could join us!")

    }

}

Just like before, the Scala needs to be compiled to MSIL. There are fewer arguments needed because the Scala is not referencing any other dlls and does not contain the entry point for the executable:

<Scala.Net directory>\bin\scalacompiler.exe

–target:msil

–Xassem-extdirs <Scala.Net directory>\bin

<scala files>

Where:

  • <Scala.Net directory> is the directory where the Scala.Net SVN repository was checked out to

After the MSIL has been generated, it can be turned into a dll:

ilasm /DLL <MSIL file>

Notice the /DLL flag for ilasm, this is where the magic happens.

After running ilasm, the new Scala dll can be referenced in a .Net application. I created a console application and added a reference to the dll. For the application to run propertly there are a few more dlls that need to be referenced, all of which can be found in the bin directory of the SVN repository:

CSharpFilesForBootstrap.dll

IKVM.OpenJDK.Core.dll

jfisl.dll

scalalib.dll

scalaruntime.dll

The C# is very straight forward, and surprisingly doesn’t require any extra using statements:

using System;

namespace CallScala

{

    class Program

    {

        static void Main(string[] args)

        {

            var speaker = new ScalaSpeaker();

            speaker.Speak();

            speaker.Speak("Jim");

        }

    }

}

Posted in .Net, C#, Scala | 3 Comments

Getting Started with Scala.Net

Reporting from the Programming Summer Camp in Crested Butte, Colorado. This has got to be one of the better places in the world to code. We had a great warm up bike ride this morning, then settled in for an afternoon of coding. I spent my time figuring out Scala.Net with the goal of calling some C# code from Scala.

The Scala.net compiler is in an SVN repository located at: http://lampsvn.epfl.ch/svn-repos/scala/scala-experimental/trunk/bootstrap

I started by writing a small C# class library. I created one class with a couple of methods. This is pretty standard “getting up to speed” stuff:

using System;

 

namespace Echidna

 

{

 

    public class Speaker

 

    {

 

        public void Speak()

 

        {

 

            Console.WriteLine("Hi there.");

 

        }

 


 

        public void Speak(string name)

 

        {

 

            Console.WriteLine(string.Format("Hi, {0}, glad you could join us.", name));

 

        }

 

    }

 

}

 

Next, I dove into the Scala. For this experiment all I needed was an object whose Main created an instances of my C# class and called its methods:

import Echidna.Speaker

 


 

object Test {

 

    def main(args: Array[String]) {

 

        val speaker = new Speaker()

 


 

        speaker.Speak

 

        speaker.Speak("Joe")

 

    }

 

}

 

I’d like to note here that the import is a Scala import statement (not a C# include), so the class name needs to be included. I could also have written “import Echidna._” to get everything from the Echidna namespace. The compiler error did not make it especially obvious that I had messed this up.

With all my code built I could start compiling. Currently, there are two steps to create an executable, but there are plans to get rid of the second step. First, MSIL is created using the Scala.Net compiler:

<Scala.Net directory>\bin\scalacompiler.exe

-target:msil

-Xassem-extdirs <Scala.Net directory>\bin

-Xshow-class <Scala entry class>

-Xassem-path <Dependent Assemblies>
<Scala files>

This is the bare minimum required to compile the Scala into MSIL. I inserted the new lines for ease of reading and to make it more obvious what is going on. Notes:

  • <Scala.Net directory> should be the directory that you pulled the SVN repository into
  • <Scala entry class> is the Scala class that should serve as the entry point for your program. In my case it was “Test”
  • <Dependent Assemblies> are the .Net assemblies your Scala program relies on. In my case this was “Echidna.dll”
  • <Scala files> are your Scala source files. In my case it was “simple.scala”

Next, the MSIL needs to be turned into an executable using ilasm. I opened a Visual Studio command prompt so that ilasm was in my Path:

ilasm Test.msil

The last problem I ran into was that the executable couldn’t find scalalib.dll, so I copied it out of the directory where the compiler is (the same as <Scala.Net directory>.) After this, my executable ran perfectly.

More information on the compiler and it’s capabilities (for example, it does not currently support .Net generics) can be found at: http://lamp.epfl.ch/~magarcia/ScalaNET/2011Q2/PreviewScalaNET.pdf

Posted in .Net, Scala | Leave a comment

Kinect: Getting Started

Microsoft released its Kinect SDK yesterday. It can be found here. I spent yesterday afternoon building my own sample application to explore the API and Microsoft’s samples.

The API documentation is fairly comprehensive and the getting started guide is a quick read (on the Kinect SDK website, not included in the download). The two samples are large and cover all the major points of the API. I think I would have preferred smaller, more targeted samples, but they are still good for figuring out how to use pieces of the API.

The Kinect hardware does quite a bit of processing, so the API is fairly easy to work with. Pulling color image data off of the device is almost trivial:

var kinect = new Runtime();

kinect.Initialize(RuntimeOptions.UseColor);

 

kinect.VideoStream.Open(ImageStreamType.Video,

   2, ImageResolution.Resolution640x480, ImageType.Color);

 

kinect.VideoFrameReady += HandleNewColorVideoFrame;

This is all the code needed to start receiving video from the Kinect. Every time there is a new frame available the “HandleNewColorVideoFrame” method will get called. The EventArgs has a property that contains the image data of the new frame.

The really revolutionary part of the Kinect is the depth map it provides of the scene it can see. For each frame of the depth map video it can create detailed skeletal information about two people in the scene. This skeletal information gives the (X, Y, Z) absolute position of 20 points on the body and the confidence of that absolute position. The confidence is mostly for points that the Kinect cannot directly see. For example, the Kinect will still try to track my hand if I put it behind my back. It is just as easy to get the skeletal data as it is to get the color video image:

kinect = new Runtime();

kinect.Initialize(RuntimeOptions.UseDepthAndPlayerIndex | 

   RuntimeOptions.UseSkeletalTracking);

 

kinect.DepthStream.Open(ImageStreamType.Depth, 2,

  ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);

 

kinect.SkeletonFrameReady += ProcessSkeletonFrame;

ProcessSkeletonFrame will be called every time the Depth video has a new frame. The EventArgs contains a list of all of the skeletons the Kinect has information about.

This is all the code you need to get up and running with a Kinect. It’s pretty awesome that it takes so little to start working with it; however, working with it is by no means easy. There are a lot of considerations that need to be made to make the Kinect a good user experience. In my next post I will go over a couple gotchas that I encountered while building my sample.

Posted in .Net, Kinect | Leave a comment