Silverlight C # Game Development: L4 model groups and simple animations

Source: Internet
Author: User

We have already imported the basic model and custom model. It is really troublesome to add a bunch of models through code each time. You can use Geometry as a model group in the Balder, add all kinds of models to it. This time, let's take a look at the model group and add a small animation. If you understand this article, you can use this engine to develop a simple game and look forward to your achievements.

The first step is to create a new control. Create a control to open the. cs file and add the code as shown below,

Public Lesson04 ()
{
InitializeComponent ();
// L1
Game game = new Game () {Width = 600, Height = 400 };
Game. Camera = new Camera ();
Game. Camera. Position = new Coordinate (100,120,150 );
Game. Camera. Target = new Coordinate (0, 0, 0 );
Game. Children. Add (new OmniLight () {Position = new Coordinate (0, 0, 0 )});
// L3
Game_Axis axis_x = new Game_Axis (new Vertex (-300, 0, 0), new Vertex (300, 0, 0), Colors. Red );
Game_Axis axis_y = new Game_Axis (new Vertex (0,-300, 0), new Vertex (0,300, 0), Colors. Blue );
Game_Axis axis_z = new Game_Axis (new Vertex (0, 0,-300), new Vertex (0, 0,300), Colors. Green );
// L4
Balder. Objects. Geometries. Geometry group = new Balder. Objects. Geometries. Geometry () {Name = "MeshGroup "};
Group. InteractionEnabled = true;
Group. Children. Add (axis_x );
Group. Children. Add (axis_y );
Group. Children. Add (axis_z );
// L2
Mesh Teapot = new Mesh ();
Teapot. Position = new Coordinate (0, 0, 0 );

Teapot. AssetName = new Uri ("/Balder_Studio; component/Res/teapot. ase", UriKind. Relative );
// L4
Group. Children. Add (Teapot );
Group. children. add (new Box () {Dimension = new Coordinate (10, 20, 30), Position = new Coordinate (100, 10, 0), Name = "MyBox "});
Group. Children. Add (new Box () {Dimension = new Coordinate (10, 30, 10), Position = new Coordinate (10, 10,-100 )});
Game. Children. Add (group );

LayoutRoot. Children. Add (game );
}

 

Okay, let's take a look. You will see a teapot, a simple coordinate (for details about the coordinate axis class in Lesson03), and two bricks (Box) just as in the beginning of the article, the code for adding a model to a group can be completely transformed into the following code. For simple understanding, there will be no changes.

Public class MyGeometry: Balder. Objects. Geometries. Geometry
{
// You code
}

The above content mainly introduces how to group data. In fact, it is no different from a Children set. If you understand the Silverlight container mechanism, it is easy to understand.

To write an animation, we need a timer to simulate a small loop. The Code is as follows:

 

DispatcherTimer _ dispatchertimer = new DispatcherTimer ();
_ Dispatchertimer. Tick + = new EventHandler (_ dispatchertimer_Tick );
_ Dispatchertimer. Interval = TimeSpan. FromMilliseconds (30 );

 

If you find some minor errors, you may need using System. Windows. Threading, and write the following code in the Lesson04 class:

 

// Model group
Balder. Objects. Geometries. Geometry _ meshgroup = null;
Balder. Objects. Geometries. Box _ box = null;
// Rotation angle count
Float angle = 0;
Float speed = 2;
Void _ dispatchertimer_Tick (object sender, EventArgs e)
{
If (_ meshgroup = null)
_ Meshgroup = this. FindName ("MeshGroup") as Balder. Objects. Geometries. Geometry;
If (_ box = null)
_ Box = this. FindName ("MyBox") as Balder. Objects. Geometries. Box;
_ Meshgroup. World = Balder. Math. Matrix. CreateRotationY (angle ++ );
_ Box. Position. Y + = speed;
If (_ box. Position. Y> = 50 | _ box. Position. Y <= 0)
Speed =-speed;

}

 

In this Code, the angle is constantly increasing, and the matrix of the world coordinate system after the Y axis is created is assigned to the model group, so that the entire model group is rotated, while Box follows the logic of moving up and down the Y axis. This is the simplest way. I believe it is not difficult to understand it :). The FindName method is generally not used frequently, the main purpose is to avoid the misunderstanding of the previous and subsequent Code and make it coherent.

Finally, let's run it to see the effect:


If you still do not understand it, you can download the code research, 3D computing is just starting, and next, let's talk about the camera and the light :)

Source code: Click here to download the project

If Balder. dll is missing in the project, download it quickly here: SL4_Balder.rar

We recommend the Silverlight game development blog:Dark blue right hand

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.