Cocos2d-x game instances (3)-Get Map Indexes

Source: Internet
Author: User

Small man (Bill man) Personal Original, welcome to reprint, reprint please indicate the address, small man (Bill man) column address http://blog.csdn.net/bill_man

We must not only display the map, but also perform some logical operations on the map, such as map collision. To obtain the map attributes, we must obtain the Map Index, that is, the row and column values of the map, so how can we obtain the Map Index Value Based on coordinates? This article describes the two types of maps supported by the tiles tool to obtain the Map Index Value Based on coordinates.

First, a common map is relatively simple, that is, dividing the coordinate value by the width and height of the graphic element. This is also a commonly used map representation method.

The following describes how to obtain the Map Index Value Based on coordinates of the 45 degree angle. First, let's take a look at the 45 degree angle map.

It is a diamond. We Convert coordinates from the geometric point of view. The pixel width and height are not the real pixel width and height, but the length of the diagonal line of the diamond of each small pixel block, in this way, the problem is transformed into solving the problem of the diamond edge length corresponding to the diamond midpoint.

Sorry, my picture is not good. A is our coordinate point, the whole map is the diamond. We can find the DC and CB respectively and divide them by the length of the togine and diamond to obtain the corresponding index coordinates, it should be noted that ABCD is a Parallelogram (although it looks like ......), therefore, for a triangle ADC, we only need to obtain the length of AD and DC (AD is equal to BC ), we can use the sine theorem to obtain (AD/sin angle ACD = ac/sin angle ADC = Dc/sin angle CAD) because we can obtain the AC (two-point distance formula ), we can obtain the angle of the AC on the horizontal line. The difference angle formula can be used to obtain the angle ACD and angle CAD. Then the angle CDA is obtained, and the triangle is unlocked, let's separate the case where a points are located at the left and right of the C point. The Code is as follows: mystatic5 is the root number 5 mystatic2 is the root number 2 mystatic is the length of the diamond edge (figure high * root number 5)

If there are any errors, I hope you can correct them more.

Next, continue with the game instance.

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.