Sunday, October 02, 2011

Artismo on Windows7

Artismo is working on Windows 7. It turned out that I had optimized some of the core code and forgot to update the installer to include the new dll's. So, we're back up and running.

I started another project working on a Content Management System. I should be using this system on the new Artismo Site to start testing the beta. The CMS is written in C# using MVC3.

Happy Coding!
Jason

Saturday, June 18, 2011

.NET Workflow WF4 State Machine Activities

You may have noticed that workflow has changed drastically from WF 3.5 to WF 4.0. You may have also noticed that the state machine activities were not shipped with WF 4.0.

Fortunately, they have been released on Codeplex. So, here is a link to download the update.
Workflow 4.0 from Codeplex as part of a service pack

I've been playing with the new Workflow and let me say I really like it! Good job WF guys at Microsoft!

Happy Coding!
Jason

Saturday, June 11, 2011

WCF, Entity Framework, and LINQ in 15 Minutes

In this tutorial we'll use Entity Framework in VB.NET to create a Data Model from our SQL database. Then, we'll create a WCF Service to fetch a customer using LINQ. Finally, we'll create a Windows Form as a client application to call the WCFService and get back some data.







Prerequisites
-Visual Studio 2010
-Sql Server Express
-Sql Management Studio (Installed with Sql Express)

1. Creating the Database

To create a simple database for this tutorial:
  1. Open Microsoft Sql Server Management Studio.

  2. Right-Click on the databases folder and choose New Database. Name the database "Testeco".

  3. Right-Click on Tables and choose New Table. Create a table with the following columns:





    IDPrimary key, datatype as numeric(18,0) and allow nulls false. Set Identity to true in properties of column.
    LastVarchar(50)
    FirstVarchar(50)


  4. Name the table "Customer" and hit Save. I named mine "tblCustomer" out of habit, but if you usually prefix your database objects (e.g. tbl, vw, sp) you may want to stop doing this because these names now bubble up into the object names in the solution.

  5. Right-click your table and choose "edit top 200 rows" which will bring up an interface to add rows. Insert five test rows, hit Save and your database should be ready.
2. Creating the Solution

Open Visual Studio 2010. Create a new project. Under Project Templates Choose Other Project Types->Visual Studio Solutions: Blank Solution. Name your project "Testeco" and hit OK.

3. Creating the Data Model Project
  1. Right-Click your Testeco Solution and choose Add->New Project.
    Choose the Visual Basic->Class Library and name it "Testeco Data Model" and hit ok.

  2. Delete the Class1.vb file.

  3. Right-Click the new project and choose Add->New Item. From the list select ADO.Net Entity Data Model, and name it "TestecoDataModel" and hit ok.

  4. A dialog box will appear. Select "Generate from Database" and hit next.

  5. The next dialog requires that you specify the database connection. Select new connection. If you're using sqlexpress you will type the name of the database as localhost\SQLExpress. Select the testeco database and hit ok. Save the connection string as "TestecoEntities" and hit next.

  6. To select the database objects, drill down to tables and check the Customer table. Uncheck the option for Pluralize or Singularize generated object names. This option is not working correctly in the current version but is supposed to be fixed on the next service pack. Name the namespace as "TestecoModelContext" and hit finish.



At this point, you have an Entity Framework Data Model of your table. You should see a new file called TestecoDataModel.edmx and a diagram of the data model in the designer. It has generated the classes to handle updating the database with your object changes for you. Now we are ready to use that data in a service.



4. Creating the WCF Service

In this step we will create a web project that will host our WCF Service.
  1. Right-Click the new project and choose Add->New Item.
    Select WCF->WCF Service Application from the options, and name it "WCFTestecoService". Hit OK.


  2. Delete the IService1 and Service1 files as we will be creating new ones.

  3. Select this project, right-click and select Add->New Item and select WCF Service. Name the file "WCFTestecoService" and hit Add. It creates the Data Contract(Interface) and Service which implements the contract.

  4. Open the Data Contract called "IWCFTestecoService". It created a sample function called DoWork which we will replace with a function to fetch a customer from our Data Model. Replace the Sub DoWork function with:


    Function GetCustomer(ByVal id As Integer) As Customer

    The compiler will flag the return type Customer as unknown but we will fix that on the next step.

  5. Right-click on the project and add->Class... and name it Customer. At this point our compiler will be happy about returning a Customer type. But, we need to add two public properties to the class file. Add two properies to the Customer file for Last and First making your class look like this:

    Public Class Customer
    Public PropertyFirst As String
    Public PropertyLast As String

    End Class

    Something to keep in mind here is that this is not the same class as the Customer coming out of the Entity Framework. We are creating a simple wrapper to return from the service.

  6. Open the WCFTestecoService file. There should be a function in there still that says dowork. We need to rewrite this function so that it implements the new constract we created. Just delete the dowork function completly, place your cursor at the end of the line that says: Implements IWCFTestecoService and hit enter. It will reimplement the interface in your class and write the new function GetCustomer.

  7. Now we need to write the function to get the data out of our datamodel. But, we need to add a reference to the data model project. Right-Click on your service project and select Add Reference. A dialog will appear. Make sure the Project tab is active and select TestecoDataModel and hit OK. We have now added the reference to our data model project.

  8. Back to the GetCustomer function, we need to instantiate the datamodel to query against. Add the following to your function:

    Public Function GetCustomer(ByVal id As Integer) As Customer Implements IWCFTestecoService.GetCustomer
    Dim context = New TestecoDataModel.TestecoEntities()

    Dim result = (span class="blue">From cust In context.Customers
    Where cust.id = id
    Select cust).First

    Return New Customer() With {.First = result.First, .Last = result.Last}

    End Function

    When you update your function two things need to be checked. First, it may complain about the context.Customers. If so, remove the .Customers and use intellisense to add the proper table name (yours may be a little different than mine). Next, it will pick up a missing reference. if you click on the error it will tell you you're missing a reference to System.Data.Entity. And if you click it, it will fix it for you. Great tool!(Wishing C# did this..) The code should now compile correctly. But, before we move on let's review this code.

    The context is our datamodel. We are using Linq to query the context for a customer with a matching ID. We are returning a new customer and setting the properties of the object before we send it back.

  9. One last thing to do is to copy the connection string from our app.config file in the EntityFrameWork project into the web.config for our WCFService. Copy the connectionstring tag out and paste into your web.config. Now, our code can properly reach into the configuration for the connection string it will need.

  10. Build the entire solution and make sure it compiles.


5. Creating the Client Application

We will use a windows application to consume the WCF Service and provide an interface to get our data.


  1. Add a new project to your solution and choose type Visual Basic->Windows->Windows Forms Application and name it ClientApp.

  2. Create a simple form that looks like the image above.
    Name the textboxes as txtID,txtFirst, and txtLast.

  3. We need to add a web reference to our WCF Service. We do this by right clicking the project and selecting "Add Service Reference" which will bring up a dialog. Click the Discover button which will find your web project. Click on the little arrow next to TestecoService.svc which will show our service contract under it. Selectit in the left (nothing will appear in the right which is okay) Name it TestecoService.


  4. Double-Click the Go button on your form to go to the gobehind code. We need to write code that will take the data form the txtID.Text, get a result from our service, and load it into our textboxes. The function should look like this:

    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
    Dim svs = New TestecoService.TestecoServiceClient

    Dim cust = svs.GetCustomer(CInt(txtID.Text))

    txtFirst.Text = cust.First

    txtLast.Text = cust.Last

    End Sub

    We first get a reference to a proxy class that was generated when we added the reference. we then call the function and get our data back. That's it.

  5. Set the startup project as your ClientApp project and run the program.
So, in a few minutes we've used the Entity Framework to create a data model, which we accessed using LINQ in our WCFService, that was used in a simple windows application.

Happy coding!
Jason

Wednesday, April 20, 2011

3D Boolean Operations


A Boolean operation is a process of creating a result set from combining two 3D objects. In this case, I'm creating the union of the two objects. See the picture above of the latest output from the algorithm i'm writing.

It has been challenging to say the least. You have to calculate intersection between the objects for both coplanar (flat surface) and nonplanar scenarios. Once this is done you must triangulate the triangles and then sort them and return the solution. I'll post more pics after a few more nights of testing. Fun stuff! Reply if you want me to blog more on the actual algorithms I'm using. I came up with a few interesting techniques for doing this.

Happy Coding!
Jason

Wednesday, December 08, 2010

vs2010 Resx Error Not Valid Win32 Application Workaround

I had a problem today compiling an app on Windows 7 64-Bit that compiled fine on XP 32 bit. It turns out that it is a bug in vs2010. This is the workaround that I used just in case you run into it.

Description
Two things were happening. Both of them were related to the resx file of my main form.
1. Images that were used in ImageList or Toolbars were causing a problem.
2. A Matrix from a class property was being cached in the resources now and causing an error.

Workaround
I reloaded the GUI images for the toolbar buttons to use images imported into the Properties Resources instead of the Form Resource. This seemed to clear up that issue.

Next, I deleted the offending Matrix data out of resx. I also removed where it was setting the class property in the InitializeComponents function. Next, I fixed the class so that the Matrix property was not serializable by the designer. So, I wouldn't have to do this again. This took a piece of metadata above the property to tell it not to set a default value:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
Everything seems to be working after that. I'll try to post the exact error next time I see it.

Happy Coding!
Jason

Tuesday, December 07, 2010

BSP Trees in Modern Game Engines



I first came across BSP when working with the Unreal Engine. We were working in UnrealEd and a level designer was demonstrating how they were using BSP Brushes to make rooms. I thought they looked like plain boxes. And, that’s really all they were until the compile button was pressed and the magic began. Then, those boxes were sliced, diced, lit, packaged, and ready to run.

Now, as I start planning out my level editor, I find myself thinking about those BSP trees. It has been a few years now, and I begin to wonder if they are the right structure to use in my level editor.

A Binary Space Partitioning (BSP) Tree is a structure that subdivides space into smaller sections or sets. It has been used in First Person Shooter (FPS) games to solve a problem with rendering polygons in the correct order. It would provide a method to traverse the structure and draw the polygons from back to front quickly.

But, the need to manually order triangles became less of an issue with the advent of the hardware accelerated Z-buffer. The polygons get pushed onto the GPU to be quickly sorted and processed for rendering.

That doesn't mean that BSP is obsolete. Spatial partitions, such as BSP, quad-tree, and octrees, are essential tools in modern rendering engines. They are used in visibility testing, network optimization, collision, and lighting. They can also be computed during compile time.

There are many discussions on which spatial partition is best. The truth is that they've all got their strengths and weaknesses. Once you've identified the characteristics of your engine, you'll be ready to identify which structure is best for you. But don't rule out the BSP Tree. They are well documented and still used today in modern engines. Especially for First Person Shooters.

Happy Coding!
Jason

Related Links:
http://www.gamedev.net/reference/programming/features/bsptree/bsp.pdf


http://en.wikipedia.org/wiki/Binary_space_partitioning

Saturday, December 04, 2010

XNA Multiplayer Programming



In a previous post I put together a simple multiplayer tank game with GDI+. But, I decided to change to XNA when I hit a limitation with the game sound. What a perfect opportunity to make it 3D!

There are many advantages to using XNA 4.0, especially if you want to play your game on the xbox 360 or Zune. But, keep in mind that if you want to play it on the XBox 360 you will need to purchase a Developers Membership at App Hub

Content Creation
The first task is to create the tank model and turret using Artismo 3D. If you want a copy just let me know by commenting on this page. Or send me your e-mail by using my contact page here.

The tank in Artismo:


In Artismo I can create the models and the artwork in one application. It even does animation for characters. And, supports plug-in architecture to incorporate project-specific processes. For example, a level editor plug-in could be incorporated into the tool to produce the levels for your game.

Tank Movement
Moving around a 3D object is a lot easier than in GDI+. In GDI+ you're only drawing a square bitmap and everything is centered on the upper left corner so you have to move the image up there, rotate it, and put it back. In 3D you have a matrix and XNA provides really simple functions to rotate or translate the object around. If you ever used Managed DirectX for Windows it is very similar code.

User Interface
In the first version I had regular windows textbox controls and buttons. Not in XNA! You'll have to code them or get a GUI library on the internet. They're not that hard to code though. But, the textbox control does take a little work. If you do a few searches on the internet you can find some examples of doing it. The font support was really easy as well.

Client/Server Protocol
On the server side, there were a few changes because I had to send more data now. Instead of just a position point in 2D and a rotation amount, I changed it to send the tank matrix or transform. It is possible to do it with the original data but it was much easier to pass back and forth the exact data I needed instead of trying to derive the matrix from pieces of information.

Sound
Sound was a lot easier! I can play multiple sounds at the same time and XNA even supports stereo functionality so if a tank blasts off a round to your right you can hear it in the right speaker. I didn't take it that far but atleast it is good to know that it supports it.

Collision
I used a small 3D ball for the bullet. To do the collision I created a Bounding Box roughly the same size of the tank. Then, I move the position of the bullet into the model's space and create a Bounding Sphere for it and do collision against the two. All of the collision code is built into XNA. It looks like this:
BoundingSphere sphere = new BoundingSphere(bulletpoint, 0.2f);
// create a bounding box for each object.
BoundingBox bbox = new BoundingBox(new Vector3(-3, 0, -3), new Vector3(3, 3, 3));
bbox.Intersects(ref sphere, out result);
if (result)
{
od.Health -= 1;
return true;
}


Conclusion
The end result is a 3D version of my original tank game that can be played on multiple computers. Some extra features that could be tossed in there is chatting, explosions, etc. That's as far as I'm going with this game.

Tank Wars Single Player with 20 NPC Tanks:



Next time we'll discuss level design and make some plug-ins for Artismo to produce the levels.

Happy Coding!

Jason