Book Image

3D Game Development with Microsoft Silverlight 3: Beginner's Guide

By : Gaston C. Hillar
Book Image

3D Game Development with Microsoft Silverlight 3: Beginner's Guide

By: Gaston C. Hillar

Overview of this book

Microsoft Silverlight is a programmable web browser plug-in that enables the animation, vector graphics, and audio-video playback features that characterize Rich Internet Applications. Silverlight is a great (and growing) RIA platform and games are the next level to exploit in it. But it doesn't offer 3D capabilities out of the box and integrating a 3D engine can involve lot of complex mathematics and matrix algebra. This book will help C# developers to get their fingers on the pulse of 3D in Silverlight. This book uses Balder, an open source 3D engine offering 3D capabilities for Silverlight 3. It leaves out boring matrix algebra and complex 3D mathematics. By the end of the book you will have explored the entire engine, and will be able to design and program your own 3D games with ease! The book begins by introducing you to the fundamental concepts of 2D games and then drives you into the 3D world, using easy-to-follow, step-by-step examples. The book employs amazing graphics and impressive performance, and increasingly adds more features to a 3D game giving you a rich interactive experience. By following the practical examples in this book, you will learn the important concepts, from the creation of the initial models, up to the addition of physics and artificial intelligence. The book helps you to provide realistic behaviors for 3D characters by enveloping models with different textures, using lights to create effects, animating multiple 3D characters using a physics engine (Farseer Physics Engine), and simulating real-life physics. Videos, music, and sounds associated with specific events offer the final touches to the 3D game development learning experience.
Table of Contents (21 chapters)
3D Game Development with Microsoft Silverlight 3
Credits
About the Author
Acknowledgement
About the Reviewer
Preface
Pop quiz—Answers

Time for action—building and running Silverlight applications


Now, you want to see the ghost moving on the screen while the mouse pointer changes its position. In order to do this, we must add some code to show the ghost and to move it. We will add both XAML and C# code as follows:

  1. 1. Stay in the SilverlightMonster project.

  2. 2. Open the XAML code for MainPage.xaml (double-click on it in the Solution Explorer) and replace the existing code with the following:

    <UserControl x:Class="SilverlightMonster.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml
    /presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Cursor="None"
    Width="1366" Height="768" MouseMove="Ghost_MouseMove"
    xmlns:SilverlightMonster="clr-namespace:SilverlightMonster">
    <Canvas x:Name="cnvMovementTest" Width="1366" Height="768"
    Background="Bisque">
    <SilverlightMonster:Ghost Canvas.Left="10" Canvas.Top="10"
    x:Name="ghost"/>
    </Canvas>
    </UserControl>
    
  3. 3. You will see the ghost appearing in the upper-left corner of the page in the designer.

  4. 4. Now, expand MainPage.xaml in the Solution Explorer and open MainPage.xaml.cs (double-click on it). We need to add an event handler to move the ghost on the screen to the position where the mouse has moved.

  5. 5. Add the following lines of code in the public partial class MainPage : UserControl to program the event handler for the MouseMove event:

    private void Ghost_MouseMove(object sender, MouseEventArgs e)
    {
    // Get the mouse current position
    Point point = e.GetPosition(cnvMovementTest);
    // Set the canvas Left property to the mouse X position
    ghost.SetValue(Canvas.LeftProperty, point.X);
    // Set the canvas Top property to the mouse Y position
    ghost.SetValue(Canvas.TopProperty, point.Y);
    }
    
  6. 6. Build and run the solution. The IDE will ask whether you want to turn on debugging or not. It is always convenient to click on Yes because you will need to debug many times. The default web browser will appear showing a bisque background and the ghost will move following the mouse pointer, as shown in the following picture:

What just happened?

The ghost appeared in the web browser and it moved through the bisque rectangle as if it were a mouse pointer. You created and ran your first Silverlight application in the web browser.

First, we changed the XAML code for MainPage.xaml. We made some changes to do the following:

  • Hide the mouse pointer (the cursor):

    <UserControl x:Class="SilverlightMonster.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml
    /presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Cursor="None"
    
  • Specify width and height of 1366X768 pixels:

    Width="1366" Height="768"
    
  • Indicate a MouseMove event handler:

    MouseMove="Ghost_MouseMove"
    
  • Reference the SilverlightMonster namespace to allow access to the definitions specified in it:

    xmlns:SilverlightMonster="clr-namespace:SilverlightMonster">
    

Then, we added a Canvas named cnvMovementTest (with a 1366X768 pixels width and height), a bisque background, and an instance of Ghost (SilverlightMonster:Ghost) named ghost located in the point (10, 10) taking into account both the Canvas and its upper-left corner:

<Canvas x:Name="cnvMovementTest" Width="1366" Height="768"
Background="Bisque">
<SilverlightMonster:Ghost Canvas.Left="10" Canvas.Top="10"
x:Name="ghost"/>
</Canvas>

Programming event handlers in the main page

Once the ghost was shown using XAML code, we had to program code for the event handler, which is triggered when the mouse moves on the main page's UserControl. We had indicated the MouseMove event handler as Ghost_MouseMove. For this reason, we defined a private void method with that name in the C# class MainPage, associated with the previously explained XAML.

As mentioned earlier, we defined the cnvMovementTest Canvas in the XAML section. The code in the event is very simple. However, it interacts with some elements defined in the XAML code.

The following line retrieves the mouse's current position as an instance of Point—a 2D vector, with two coordinates (X and Y):

Point point = e.GetPosition(cnvMovementTest);

Now we call the SetValue method for ghost (the instance of Ghost is already defined in the XAML section). We assign the values of the X and Y coordinates of the 2D vector to the Left and Top properties of the canvas containing the ghost illustration:

ghost.SetValue(Canvas.LeftProperty, point.X);
ghost.SetValue(Canvas.TopProperty, point.Y);

Note

We can create visual objects, such as the ghost of this example, in XAML or using C# code. Sometimes, it is more convenient to use XAML, whereas at other times it is more convenient to use C# code. It depends on the kind of application we are developing.