As3 RPG Game Engine Development log 3: map coordinate conversion

Source: Internet
Author: User

The map remains unchanged because of the 45-degree coordinate conversion problem mentioned above. However, the relationship between the map coordinate and the game coordinate is changed. The corresponding relationship is as follows:

The dotted line indicates the edge of the map, and the number of items in the block is the index of the array, that is, the map coordinate. The height of the map is in uppercase h, and the height of the block is in lowercase h. The width of the block is twice the height.

    • Convert coordinates to map coordinates

Assume that the display coordinates of a point (A in) are (x, y), and the map coordinates are (I, j ), to convert the display coordinate to map coordinate, we need to find the I and J when we know the X and Y.

The value of I is equal to the side length of AB except the side length of the Diamond Block. AB = AF + BF, △adf is a right triangle with a ratio of to the right, therefore, AF =√ 5-X2. △Fbk is BF = BK isosceles triangle, and the height on the bottom side is equal to the bottom side FK, FK = Ge, GE = AE-AG, Ag = X/2, so BF =√ 5-2(Y-X/2 ). Last

I = (AF + BF )/(√ 5-H2) = [√ 5-X2+
√ 5-2(Y-X/2)]/√ 5-H2= (Y + X/2)/h.

To solve the J value, there are two conditions: A and O. The conditions are X/2

at point A, J is equal to the side length of the AC except the side length of the Diamond Block, AC = Ah + HC, hc = Hi = AJ, AJ can be obtained through the I value. Ah = √ 5 -x 2 , AJ = side length of the Diamond Block-(side length of the AB-Diamond Block × I) =

√ 5-H2-[√ 5-X2+√ 5-2(Y-X/2)] +√ 5-Hi2,

This is the result of J = (x/2-y)/H + I + 1.

At O point, the same as above, the difference is that PQ, PQ = OS, and the final calculation result is J = (2 H + x/2-y)/h-I.

Conclusion:
I = (Y + X/2)/h
J = (x/2-y)/H + I + 1 when X/2 J = (2 H + x/2-y)/h-I when X/2> = H-H

    • Map coordinate conversion to display coordinates

At this time, you need to convert (6, 7) to the pixel value from the Z point to the left and top edges of the map. Here, you can directly use the display coordinate to convert the X and Y to the binary equations in the map coordinates, the same score conditions:

When I

X = HJ-H
Y = H (2i-j + 1)/2
When I> = H/h:
X = H (2I + J)-2 h
Y = H-HJ/2

The above formula is directly obtained. When we put it in the calculation, the X value is correct, but the Y value is always greater than the real value H/2. In this case, we need to consider the corresponding relationship. The point in the map coordinate system, such as (6, 7), actually corresponds to the point in the display coordinate system instead of the Z point, it is the point at H/2 below Z. Turn your head to the left and you can see that in the map coordinate, the upper left corner of the point (6, 7) (the upper left corner is the coordinate point) is to display the Z point below the h/2 position in the coordinate, so after correction, get the conversion formula:

When I

X = HJ-H
Y = H (2i-j)/2
When I> = H/h:
X = H (2I + J)-2 h
Y = h-h (J + 1)/2

    • Summary

Mathematics is very interesting. I thought these formulas would be related to the angle of map skew, but after calculation, the value of root number 5 does not exist. However, this is not entirely true, because the aspect ratio of the Diamond Block is exactly 1 to 2, the formula is much simpler at last.

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.