Create a tracking camera (1)

Source: Internet
Author: User

A very common type of camera in computer games is a tracking camera. A camera follows (usually a player) after a key object to provide a first-person or third-person perspective, allows objects to be viewed in the game world.

We already have a lot of pieces to implement this kind of camera in our own game, but a little bit more camera type of the game framework, we can implement this absolutely simple implementation in the game.

This part of work uses this kind of change to build a scenario (there is a paper airplane that we created in the final project ). Once we have this plane moving (it will have an interesting task in itself), you will see how to append the camera so that it will automatically chase the plane and configure some different tracking cameras.

Add paper airplane

Paper airplane is a simple 3D model created in sketchup. It contains only four triangles arranged to the basic shape of the paper airplane. The model can be in sketchup,

It can be located so that its center is similar to the original point (0, 0, 0 ). This positioning will ensure that when we rotate it, it will rotate around its center point.

This object only contains a flat triangle, so in this case we must ensure that it can be exported so that the back surface of each triangle is included. Without this, the plane will obviously disappear from the top of every triangle when observing at the bottom .. The X file export has an option to export the back surface, so this option is used to create a model file.

The plane is then added to the so-called standard matrixmodelobject derived class in the game project and named paperplaneobject. Our goal for an airplane is to make it fly smoothly in the House. However, we must add some additional features for the class to implement it.

Give aircraft vitality

Some examples of the previous object creation can be smoothly moved around the screen in multiple ways. However, they all depend on the speed at which the application locates the object. In this case, we will get a better flight path on the plane, allowing it to follow a series of moving points distributed in 3D scenarios.

This is a very useful example, but it may not be the type of control organization you will use in the game, it will be more likely to rely on user input to control the movement of players. Following a mobile path is just a useful technique to know, and there are a variety of applications in the game, from the rails of the shooter (Players control accuracy, weapons open fire, but not directly control its movement) to the computer features controlled in the game.

The first thing we need to do is to define a series of points along the moving path that the plane will follow. At the beginning of the paperplaneobject class, they are declared as a static vector3 struct array. We also store the array size to avoid further query later. BelowCodeDescription.

 // Points on the spline movement path  Static Vector3 [] _ movementpath = { New Vector3 (-1, 1.5f,-2 ), New Vector3 (-1.5f, 2.5f, 2 ), New Vector3 (0, 1, 6 ), New Vector3 (3, 0.5f, 6 ), New Vector3 (4, 1, 2 ), New Vector3 (0, 0.4f, 2 ),New Vector3 (-4, 0.8f, 1 ), New Vector3 (-5, 1.5f, 1 ), New Vector3 (-4, 2.5f,-2 ), New Vector3 (2, 2.0f,-4 ), New Vector3 (4, 1.5f,-7 ), New Vector3 (2, 1.0f,-7.2f ), New Vector3 (0, 0.5f,-6 ),}; Static    Int _ Movementpathlength = _ movementpath. length;

Prompt: These points will be determined through a simple, iterative process. In the game, many of these points must define a reasonable precision, which will be worth creating a simple design and practicalProgramTo allow them to be positioned on the screen, rather than input manually.

Now we need to move planes at these points, but the path between them is uncertain. An airplane cannot simply jump from one position to another. Even if we calculate a straight line between a point and a moving plane, its movement will look angular and unnatural.

XNa provides another very useful tool that we can use to solve this problem: a function to calculate the curve. A Spline is a curve through a series of points, such as those we define. It also directly requires the position of the point on the moving path. We can also require that the point in it calculate a smooth bending transition from one point to the next point. The calculated path is ideal, and it is the moving path of our aircraft.

The mobile path is generated from the curve of the point set for the mobile path. You can use the camera to directly look down in the scene. Aircraft images are displayed at the location of the defined moving path points. These lines show similar curve paths.

There are different curve calculation functions, but one of them will be called in xNa, called Catmull-Rom curve (after it is created, it is named edwin Catmull and Raphael ROM ). This is very useful, simple to use, and ensures that the curve path is accurate through all defined locations (not all curves can be done ).

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.