Methods of selecting objects in games

Source: Internet
Author: User
Tags count transparent color valid

For PC games, the mouse in the big line of the day, how the position of the mouse to determine what is under the object, is almost all games must face the problem.

The following methods are available for informational purposes only.

1, bounding box method. In general, create an accompanying bounding box for each object in the game, by traversing all visible objects to determine whether the mouse coordinate is falling inside a bounding box to get its selected object.

The advantage of this method is simple, the algorithm is easy to understand, when the use of rectangular bounding box, and the number of objects is relatively limited, the efficiency is also very good. The disadvantage is that the selection is not accurate enough to select the details of the object.

In 2D games, the bounding box is usually a rectangle, or a combination of several rectangles, and the 3D game uses the bounding box, or surrounds the ball or its combination. Regardless of the specific way, its algorithm is essentially the same.

2, enumeration method. One of the least efficient methods.

and 1, surrounded by the frame method similar to it also needs to traverse all visible objects, but because of the lack of bounding box mechanism, can only detect the object is located under the mouse in that position is a valid pixel, or a valid alpha value, for 3D objects, is to check the mouse point to form the selection of rays across the object of a piece of the surface.

This method can achieve very accurate selection, but because the efficiency is too low, so very little direct use, generally first use Method 1 to reduce the number of traversal objects, then use this method to achieve accurate selection.

3, feedback method. This is a very effective, but also a quick way, especially in 3D games, there are incomparable advantages.

The realization of the feedback method is very simple, first of all, to maintain a background buffer, when the target object is drawn, while the object's visible information (usually the object picture of the alpha value, or Z value) to the background buffer, and then detect the mouse corresponding buffer location of the value of the change, if changed, Indicates that the object you just drawn can be selected by the mouse. When the buffer uses a complex z operation, we can get a list of objects that can be selected by the mouse after the drawing is complete, and then simply extract the desired object from the list according to a certain principle. This mechanism, under 2D, generally does not maintain additional buffers and uses the drawing buffer directly. 3D, like OpenGL provides a built-in feedback method, more convenient for users to use. A similar mechanism can also be implemented using Z buffer, template buffering, and so on.

This method can achieve accurate to pixel level selection, and almost no impact on operational efficiency. The disadvantage is that you need to have a high control over the drawing part of the code.

4, direct mapping method. This is also an efficient algorithm that can achieve the time complexity of O (1). It is common in the 2D game of chess.

In this kind of game, the scene is stored with a two-dimensional table, each item of the table, holding the object information above it, we can through a simple algorithm, the current mouse position to get the table index, and then directly read the index of the corresponding items to complete the selection.

This approach can also be used in a fixed-angle 3D game or even a 3D game with a fixed view. The disadvantage of this method is that in the scene, the object can only be arranged in a two-dimensional table or a multi-layer two-dimensional table. This method has a greater demand for memory space. Chess game is more suitable to use this method.

Because each method has its inherent advantages and disadvantages, but for the game, the scene has ever-changing, complex and numerous. In order to be able to adapt to the actual needs, the above methods can be combined to use, so as to avoid weaknesses, better achieve demand.

Other complex selections, such as Range selection (box selection), can also be evolved from several basic methods.

The following is a 2D basic implementation of the supplemental-feedback method for the front feedback method:

This directly uses the background draw buffer as the selection buffer.

First, specify a transparent color that does not allow any non-transparent part of the object or background, such as a specified

trans = RGB (0,0,0);

is a transparent color.

Suppose the drawing buffer is a two-dimensional array:

Color Buf[h][w];

The corresponding buffer coordinates for the current mouse are

(x,y)

The list of objects that needs to be drawn is a one-dimensional array

Object Array[count];

Finally gets the stack of the selected object

Objstack;

Pseudo code is as follows: color old = buf[y][x];
buf[y][x] = trans;
 
for( int i = 0; i < count; i++ )
{
 draw(array[i]);
 if( buf[y][x] != trans )
 {
   old = buf[y][x];
   buf[y][x] = trans;  
   objstack.push(array[i]);
 }
}
buf[y][x] = old;

After you complete this step, the Objstack is a collection of all the objects below the mouse, sorted in the order in which they were drawn. All you have to do is extract the objects you need from them.

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.