Method for converting screen coordinate to logical coordinate and Cartesian coordinate of a 45-degree diamond Map

Source: Internet
Author: User
Method for converting screen coordinate to logical coordinate and Cartesian coordinate of a 45-degree diamond Map

The formula is used to determine the coordinates of the screen:
X coordinate of the screen = position in the map lattice logical array x * grid width
Y coordinate of the screen = position in the map lattice logical array y * grid height/2

The figure is as follows:

How can this formula be obtained?

There are five lines on this map. You will think about how to make a map out of a puzzle. Draw a picture to demonstrate:

Br/>
From this figure, we can see that, from left to right, from top to bottom, like regular rectangular puzzles, the only difference is that there are overlapping map elements and elements, look at the line between the first and second, the map elements in the second line will be pressed on the elements in the first line, and the map elements in the third line will be pressed on the elements in the second line. So, as long as you find the correct formula, you can design the program correctly, and then take a picture:

The green points (High Brightness green, not dark green) on the graph are the starting point of each map element. The line coordinate of the first line is 0, and the line coordinate of the second line is 1, the coordinate of the row in the third row is 2 ,...... these line coordinates determine the starting point of your map element. From this rule, we can see that the travel coordinate 0 is the same as the start point of the horizontal coordinate X of the line coordinate 2 is 0, the starting point of the line coordinate 1 is to move the half map element to the right.

Looking for a law from the vertical coordinates, we can see that the distance between the two map elements is just the height of a map element. Let's look at the line coordinate 0 and line coordinate 1. The distance between the two map elements is just the height of the half map element. Therefore, to calculate the position of each map element, the coordinates and logic formulas of your conversion screen are exactly as follows:

Logic. Y = (2 * stage. Y)/tileheigth;
Logic. x = (stage. X/tilewidth)-(logic. Y & 1) * (tilewidth/2 );
Stage. x = logic. x * tilewidth + (logic. Y & 1) * (tilewidth/2 );
Stage. Y = logic. y * tileheigth/2;
Here, tileheigth and tilewidth are the height and width of the diamond, so that you can try to define (), and other points. They are exactly the same as the result ···

Important:

First, the above formula is only applicable to the staggered 45-degree oblique map, while slide, and diamond maps must be slightly modified before they can be used.

Staggered:

 

Does the obtained logical coordinate mean the actual coordinate obtained at an angle of 45 degrees? Of course not ···

If we want to use a Cartesian coordinate to represent this map, we may be more intuitive to develop it, it is really uncomfortable to see that the Y axis indicated by this coordinate is distorted, so we will convert it to the Cartesian coordinate, then we will add pathfinding and so on.AlgorithmNo difficulty

First, we will convert the height of the map. This conversion is mainly to restore the diamond to a square. For example, if the aspect ratio of the diamond is, we need to double the height of the map on the map, then rotate-45 degrees, and the corresponding coordinates are as follows:

 

We carefully mark the coordinates of this graph:

 

 

 

// Map Click Event
Public Function onclick (Event: Event): void {
// VaR _ nodepointoint = gamemaputils. getcellpoint (_ cellwidth, _ cellheight, event. currenttarget. mousex, event. currenttarget. Mousey );
VaR _ noderpointoint = gamemaputils. getdirectpointbypixel (_ cellwidth, _ cellheight, event. currenttarget. mousex, event. currenttarget. Mousey, _ row );
VaR _ nodex: Int = _ noderpoint. X;
VaR _ nodey: Int = _ noderpoint. Y;

// VaR _ rolepointoint = gamemaputils. getcellpoint (_ cellwidth, _ cellheight, _ player. X, _ player. y );
VaR _ rolerpointoint = gamemaputils. getdirectpointbypixel (_ cellwidth, _ cellheight, _ player. X, _ player. Y, _ row );
VaR _ Rolex: Int = _ rolerpoint. X;
VaR _ Roley: Int = _ rolerpoint. Y;
VaR _ length: int;
_ Player. movetox = event. currenttarget. mousex;
_ Player. movetoy = event. currenttarget. Mousey;
// Trace ("_ nodex:" + _ nodex );
// Trace ("_ nodey:" + _ nodey );
// Trace ("_ Rolex:" + _ Rolex );
// Trace ("_ Roley:" + _ Roley );
VaR maxpoint = gamemaputils. getmaxdirectpoint (_ row, _ col );
If (_ nodex> = 0 & _ nodex <maxp. X & _ nodey> = 0 & _ nodey <maxp. Y & _ roadseeker. value (_ nodex, _ nodey) = 0 ){
_ Roadarr = _ roadseeker. path8 (new point (_ Rolex, _ Roley), new point (_ nodex, _ nodey ));
_ Length = _ roadseeker. Path. length;
// Trace (_ length );
If (_ length> 0 ){
_ Pathindex = 1;
// Trace ("_ roadseeker. path [_ pathindex]. X:" + _ roadseeker. path [_ pathindex]. X );
// Trace ("_ roadseeker. path [_ pathindex]. Y:" + _ roadseeker. path [_ pathindex]. y );
// Trace ("PX:" + _ roadmap. getvalue (_ roadseeker. path [_ pathindex]. Y + "" + _ roadseeker. path [_ pathindex]. x ). px );
// Trace ("py:" + _ roadmap. getvalue (_ roadseeker. path [_ pathindex]. Y + "" + _ roadseeker. path [_ pathindex]. x ). PY );

VaR Px: Int = _ roadmap. getvalue (_ roadseeker. path [_ pathindex]. Y + "-" + _ roadseeker. path [_ pathindex]. X). PX;
VaR py: Int = _ roadmap. getvalue (_ roadseeker. path [_ pathindex]. Y + "-" + _ roadseeker. path [_ pathindex]. X). py;

// Trace ("PX:" + px );
// Trace ("py:" + Py );
_ Player. addeventlistener (walkevent. uplo_end, onmoveover );
_ Player. addeventlistener (walkevent. on_walk, checkdep );
VaR tpoint = gamemaputils. getpixelpoint (_ cellwidth, _ cellheight, PX, Py );
_ Player. moveTo (Tp. X, TP. Y + _ cellheight );
}
}
}

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.