Mesh pathfinding method and device, electronic equipment and storage medium
Patent Information
- Application Number
- CN202611335094.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-31
- Publication Date
- 2026-10-30
AI Technical Summary
现有方向场编辑工具普遍采用单元格逐方向点击式编辑或者采用下拉菜单或数值输入式编辑,即选中一个格子后通过下拉框或输入框设定方向编码,但需要编辑者记忆方向编码规则,认知负担重,另外还有基于A星(A-Star)寻路算法的自动路径生成方案,自动计算从起点到终点的最短路径,但无法表达多分叉、可选路径等复杂行为,使得相关技术中存在操作效率低,需大量重复点击;认知负担高,需记忆抽象编码;表达力受限,无法表达多方向并存;中央处理器事件处理开销大,每次点击都触发独立的事件处理与重绘流程等技术问题
[0009]本发明实施例提供的网格寻路方法,通过在界面渲染对应可通行方向的箭头图形,在轨迹绘制模式下,以鼠标右键按住拖动的方式,依据鼠标在箭头图形区域的实际移动方向与箭头方向满足一致性要求,且在满足一致性要求的情况下判断夹角是否落入预设角度范围来确认是否完成相应通行方向的设置。相比于相关技术中逐方向点击编辑、依靠下拉菜单或数值输入编辑的模式,本申请可借助鼠标拖动轨迹即可批量完成方向场编辑,减少大量重复点击操作,提升编辑效率;编辑过程依托可视化箭头图形与鼠标拖动轨迹完成方向匹配,无需编辑者记忆抽象的方向编码规则,降低认知负担;同时可在同一网格上设置多个允许通行的方向,能够实现多分叉、可选路径的表达,克服 A 星自动路径生成方案表达能力受限的缺陷;并且通过一次拖动操作即可完成多个网格的方向配置,减少频繁点击带来的多次独立事件处理与界面重绘,降低 CPU 的事件处理开销。
Smart Images

Figure CN122998741A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and more specifically to a grid pathfinding method, apparatus, electronic device, and storage medium. Background Technology
[0002] In grid-based game applications, the movement of mobile units is typically driven by a direction field, which pre-specifies one or more permitted directions (such as up, down, left, and right) on each grid. Existing direction field editing tools generally employ cell-by-cell direction-based editing or drop-down menus or numerical input, where selecting a cell and setting the direction code via a drop-down or input box requires the editor to memorize the direction code rules, resulting in a heavy cognitive burden. Additionally, there are automatic path generation schemes based on the A-Star pathfinding algorithm, which automatically calculate the shortest path from the starting point to the destination, but they cannot express complex behaviors such as multiple branches or optional paths. These technologies suffer from several technical problems: low operational efficiency (requiring numerous repetitive clicks), high cognitive burden (requiring memorization of abstract codes), limited expressive power (unable to represent multiple directions simultaneously), and high CPU event processing overhead (each click triggers an independent event processing and redrawing process). Summary of the Invention
[0003] In view of this, embodiments of the present invention provide a grid pathfinding method, apparatus, electronic device, and storage medium to solve at least one technical problem existing in the background art described above.
[0004] According to a first aspect, embodiments of the present invention provide a grid pathfinding method, comprising: displaying a grid array on a trajectory drawing interface, wherein each grid in the grid array renders an arrow graphic corresponding to the number of passable directions; determining, in response to detecting a right-click event, entering a trajectory drawing mode; and, in response to detecting that the mouse is pressed and held to enter any arrow graphic area of any target grid, detecting whether the movement direction of the mouse in the arrow graphic area and the arrow direction corresponding to the arrow graphic area meet a consistency requirement; if the movement direction of the mouse in the arrow graphic area and the arrow direction corresponding to the arrow graphic area meet the consistency requirement, and when the angle between the movement direction of the mouse in the arrow graphic area and the arrow direction in the current arrow graphic area is within a preset angle range, then setting the arrow direction in the current arrow graphic area to allow passage.
[0005] According to a second aspect, embodiments of the present invention provide a grid pathfinding device, comprising: a display module for displaying a grid array on a trajectory drawing interface, wherein each grid in the grid array renders an arrow graphic corresponding to the number of passable directions; a first determination module for determining, in response to detecting a right-click event, that a trajectory drawing mode has been entered; a first detection module for detecting, in response to detecting that the mouse movement direction in the arrow graphic area and the arrow direction corresponding to the arrow graphic area meet a consistency requirement; and a setting module for setting the arrow direction in the current arrow graphic area to allow passage if the mouse movement direction in the arrow graphic area and the arrow direction corresponding to the arrow graphic area meet the consistency requirement, and the angle between the mouse movement direction in the arrow graphic area and the arrow direction in the current arrow graphic area is within a preset angle range.
[0006] According to a third aspect, an embodiment of the present invention provides an electronic device, including: a memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the computer instructions to perform the grid pathfinding method described in the first aspect or any optional embodiment of the first aspect.
[0007] According to a fourth aspect, embodiments of the present invention provide a computer-readable storage medium storing computer instructions for causing a computer to perform the grid pathfinding method described in the first aspect or any optional embodiment of the first aspect.
[0008] According to a fifth aspect, embodiments of the present invention provide a computer program product, including computer instructions for causing a computer to execute the grid pathfinding method of the first aspect or any corresponding embodiment thereof.
[0009] The grid pathfinding method provided in this invention renders arrow graphics corresponding to the passable directions on the interface. In trajectory drawing mode, the method uses the right mouse button to drag and select the direction of the path. It determines whether the actual movement direction of the mouse within the arrow graphic area matches the arrow direction, and if the consistency requirement is met, it checks whether the included angle falls within a preset angle range to confirm whether the corresponding passable direction setting is complete. Compared to related technologies that require clicking and editing in each direction, or relying on drop-down menus or numerical input, this application allows batch editing of direction fields by dragging the mouse trajectory, reducing repetitive clicks and improving editing efficiency. The editing process relies on visual arrow graphics and mouse drag trajectories to match directions, eliminating the need for the editor to memorize abstract direction encoding rules and reducing cognitive burden. Furthermore, multiple passable directions can be set on the same grid, enabling the expression of multi-branching, optional paths, overcoming the limitations of the automatic path generation scheme in A*. Moreover, multiple grid direction configurations can be completed with a single drag operation, reducing multiple independent event processing and interface redrawing caused by frequent clicks, and lowering CPU event processing overhead. Attached Figure Description
[0010] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0011] Figure 1 This is a flowchart of a grid pathfinding method according to an embodiment of the present invention; Figure 2 This is a schematic diagram of a grid pathfinding method according to an embodiment of the present invention; Figure 3 This is a schematic diagram of a grid pathfinding method according to an embodiment of the present invention; Figure 4 This is a flowchart of a grid pathfinding method according to an embodiment of the present invention; Figure 5 This is an interactive schematic diagram of a grid pathfinding method according to an embodiment of the present invention; Figure 6 This is a schematic diagram of a grid pathfinding method according to an embodiment of the present invention; Figure 7 This is a schematic diagram of a grid pathfinding method according to an embodiment of the present invention; Figure 8 This is a schematic diagram of a grid pathfinding method according to an embodiment of the present invention; Figure 9This is a structural block diagram of a grid pathfinding device according to an embodiment of the present invention; Figure 10 This is a schematic diagram of the hardware structure of the electronic device provided in an embodiment of the present invention. Detailed Implementation
[0012] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0013] According to an embodiment of the present invention, an embodiment of a grid pathfinding method is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0014] In grid-based game applications, unit movement is typically driven by a direction field, where one or more permitted directions (up, down, left, right) are pre-specified on each grid cell. Existing direction field editing tools generally use a cell-by-cell click-based editing method, displaying four independent arrow buttons on each grid cell. The editor must click each button individually to toggle that direction on or off. Editing a 30x20 map requires up to 2400 clicks. Other technologies include dropdown menus or numerical input editing schemes, where selecting a cell and setting a direction code via a dropdown or input box requires the editor to memorize the direction coding rules, resulting in a heavy cognitive burden. Additionally, there are automatic path generation schemes based on the A-Star pathfinding algorithm, which automatically calculate the shortest path from the starting point to the destination, but cannot handle complex behaviors such as multi-branching or optional paths. Specifically, these automatic path generation schemes can only mark a single travel direction on each grid cell (i.e., once a unit reaches a cell, it can only continue moving in that single direction), and cannot mark multiple permitted directions on the same grid cell simultaneously. For example, when it's desired that a grid allows movement both upwards and to the left, so that a moving unit can randomly choose one direction after arriving at that grid to create a multi-path flow effect, the aforementioned automatic path generation scheme cannot achieve this. In other words, related technologies suffer from low operational efficiency (requiring numerous repetitive clicks), high cognitive burden (requiring memorization of abstract codes), limited expressive power (unable to represent multiple directions simultaneously), high CPU event processing overhead (each click triggers an independent event processing and redrawing process), and inefficient storage (direction field data is typically stored as an object array, with each grid occupying tens of bytes).
[0015] This embodiment provides a grid pathfinding method that can be used in a visual editor integrated on a computer. Figure 1 This is a flowchart of a mesh pathfinding method according to an embodiment of the present invention, such as... Figure 1 As shown, the process includes the following steps: Step 101: Display a grid array on the trajectory drawing interface, and render an arrow graphic corresponding to the number of passable directions on each grid in the grid array.
[0016] For example, the grid array displayed on the trajectory drawing interface can be an R-row, C-column grid array. Each grid cell in the array renders an arrow graphic corresponding to the number of passable directions. For instance, if the number of passable directions on a grid is up, down, left, and right, then arrow graphics for those four directions are rendered on the grid. Specifically, it can be like this: Figure 2 As shown, Figure 2 Taking a 1x3 grid array as an example, each grid has 4 passable directions. Then, the corresponding number of arrow graphics are rendered on the corresponding grid. Of course, more passable directions can be configured, such as upper left, lower left, upper right, and lower right. Similarly, the arrow graphics in the corresponding directions can be configured. The number of passable directions configured between different grids can be the same or different. Those skilled in the art can set it in advance according to actual needs. Figure 2 The arrow graphics corresponding to the four passable directions displayed can have their opacity set to 0.1 before the corresponding passable direction is activated, and to 1 when activated. The specific relative opacity is not limited, as long as there is a relative change in the arrow between the activation and deactivation of the corresponding passable direction. The reference angles for the direction of each arrow graphic are 0 degrees, 180 degrees, 270 degrees, and 90 degrees, corresponding to the up, down, left, and right directions, respectively. The opacity of the arrow graphic is determined by the value of the corresponding bit, that is, when the corresponding bit is 1, the opacity is 1.0, otherwise it is 0.1.
[0017] Step 102: In response to the detection of a right mouse button press event, determine that the trajectory drawing mode has been entered.
[0018] For example, the editor can integrate a mouse event listening function to listen for mouse events. When the listening unit captures the right mouse button being pressed, the editor's internal state machine undergoes a state transition, determines and switches to the trajectory drawing mode. In this embodiment, the trajectory drawing mode starts from the right mouse button press event and continues until the right mouse button release event is detected.
[0019] Step 103: In response to detecting that the mouse is pressed and held to enter any arrow graphic area of any target grid, check whether the movement direction of the mouse in the arrow graphic area meets the consistency requirement with the arrow direction corresponding to the arrow graphic area.
[0020] For example, when the editor is in trajectory drawing mode, the mouse event listening unit continuously captures changes in the mouse position. When it detects that the mouse is holding down the right mouse button and the mouse pointer enters any arrow graphic area of a target grid, a movement direction matching detection process is triggered to determine whether the actual movement direction of the mouse within the arrow graphic area meets the consistency requirement with the direction of travel represented by the arrow graphic. This consistency requirement can be that the angle between the mouse movement direction and the arrow direction in the corresponding area is greater than 90 degrees. Specifically, the angle can be detected by connecting any two points along the movement direction after the mouse enters the corresponding arrow graphic area and calculating the angle between the vector direction and the arrow in the area. For example... Figure 3 As shown, the dashed boxes within the grid represent arrow graphic areas in the corresponding directions, and the dashed arrows represent the mouse's movement trajectory within that grid. When the right mouse button is pressed and held, the mouse first enters arrow graphic area A. If any two detection points are selected along the mouse's movement trajectory in that area, and the angle between the vector direction formed by the line connecting the two detection points and the arrow direction within that area is greater than 90 degrees, then it is determined that the mouse's movement direction within the arrow graphic area does not meet the consistency requirement with the corresponding arrow direction. Therefore, the connection operation for the corresponding arrow trajectory direction in that arrow graphic area is ignored, and the mouse's movement trajectory within the grid area continues to be monitored. When the mouse moves to the next arrow graphic area B, the consistency requirement between the mouse's movement direction within that arrow graphic area and the corresponding arrow direction is checked again. Specifically, as shown... Figure 3 As shown, each arrow graphic has a rectangular detection area. The boundary of this rectangular detection area can be determined by extending a certain area outward from the position coordinates of the arrow graphic as a rectangular sensing area. The specific extension size can be determined based on a preset width and a preset height. In this embodiment, the four boundaries of the rectangle are calculated by superimposing the preset width and preset height on the position coordinates of the arrow graphic. That is, using the position coordinates of the arrow graphic as the reference point, the total preset width is extended to the left and right, and the total preset height is extended upward and downward to generate a complete rectangular detection boundary. When the current coordinates of the mouse pointer fall within the rectangular detection area of an arrow graphic, it is determined that the mouse has entered the area of that arrow graphic, and a mouse entry event is triggered; when the current coordinates of the mouse pointer leave the area of that rectangular detection area, it is determined that the mouse has left the area of that arrow graphic, and a mouse leave event is triggered.
[0021] Step 104: If the movement direction of the mouse in the arrow graphic area meets the consistency requirement with the arrow direction corresponding to the arrow graphic area, and the angle between the movement direction of the mouse in the arrow graphic area and the arrow direction in the current arrow graphic area is within a preset angle range, then the arrow direction in the current arrow graphic area is set to allow passage.
[0022] For example, such as Figure 3 As shown, when the mouse moves from left to right into the arrow graphic area B, if the angle between the mouse's movement direction and the arrow direction in the area is less than 90 degrees, the consistency requirement is met. To avoid accidental activation of the corresponding arrow direction path due to user error, it is further determined whether the angle between the mouse's movement direction in the arrow graphic area and the arrow direction in the current arrow graphic area is within a preset angle range. In this embodiment, the preset angle can be ±30 degrees to 60 degrees from the arrow direction in the current area as 0 degrees. If the angle between the mouse's movement direction in the arrow graphic area and the arrow direction in the current arrow graphic area is within the preset angle range, then the arrow direction in the current arrow graphic area is set to allow passage.
[0023] The grid pathfinding method provided in this embodiment renders arrow graphics corresponding to the passable directions on the interface. In trajectory drawing mode, the mouse is dragged while holding down the right mouse button. The method checks whether the actual movement direction of the mouse in the arrow graphic area meets the arrow direction. If the consistency requirement is met, it further determines whether the included angle falls within a preset angle range to confirm whether the corresponding passable direction setting has been completed. Compared with the mode of editing by clicking in each direction or relying on drop-down menus or numerical input in related technologies, this application can complete the batch editing of direction fields by dragging the trajectory with the mouse, reducing a large number of repetitive click operations and improving editing efficiency. The editing process relies on the visual arrow graphics and the mouse drag trajectory to complete the direction matching, eliminating the need for the editor to remember abstract direction encoding rules and reducing cognitive burden. At the same time, multiple passable directions can be set on the same grid, which can realize the expression of multi-branch and optional paths, overcoming the limitation of the automatic path generation scheme of A-star. Furthermore, the direction configuration of multiple grids can be completed by a single drag operation, reducing the multiple independent event processing and interface redrawing caused by frequent clicks and reducing CPU event processing overhead.
[0024] As an optional implementation of this invention, step 104 includes: recording the first coordinate point where the mouse enters the arrow graphic area and the second coordinate point where the mouse leaves the arrow graphic area; calculating the angle between the mouse's movement direction in the arrow graphic area and the arrow direction in the current arrow graphic area based on the first coordinate point and the second coordinate point; if the angle is within a preset angle range, then setting the arrow direction in the current arrow graphic area to allow passage.
[0025] For example, while meeting consistency requirements, the first coordinate point a (x1, y1) when the mouse enters the arrow graphic area and the second coordinate point b (x2, y2) when the mouse leaves the arrow graphic area are recorded. The direction angle of the vector pointing from the entering coordinate point to the leaving coordinate point is calculated using the arctangent function and the coordinates of the first and second coordinate points, and the direction angle can be normalized to the range of 0 degrees to 360 degrees.
[0026] As an optional implementation of this invention, the method further includes: determining the end of trajectory drawing in response to detecting a right mouse button release event. Exemplarily, the mouse event listening unit continuously listens to the mouse button state; when a right mouse button release event is detected, the editor determines the end of the current trajectory drawing operation, exits the trajectory drawing mode, and stops trajectory editing processing for the arrow graphic area movement in / out events.
[0027] As an optional implementation of this invention, each grid in the grid array is assigned a multi-bit encoding field. Each bit represents whether a direction of the corresponding grid is passable. The number of bits is determined according to the number of passable directions configured for the corresponding grid. The default initial value of each bit is 0, indicating that the corresponding arrow direction is not passable. For example, each grid is set with an independent N-bit encoding field. This encoding field is a logical storage field at the software level, used to record the status of all passable directions of this grid. The total number of bits contained in the encoding field is matched and set according to the number of passable directions supported by the grid. One passable direction corresponds to one bit in the encoding field. In this embodiment, N can be set to an integer greater than or equal to 4. The N-bit encoding field uses the same storage structure in the editor and at runtime, without any data format conversion. Each bit serves as a binary status marker, with two values: 0 and 1. Bits are mapped one-to-one with passable directions. During editor initialization, all bits are initially set to 0, meaning the arrow direction corresponding to that bit is blocked. When subsequent editing processes determine that access in a particular direction needs to be enabled, the corresponding bit is set to 1, indicating that access in that arrow direction is permitted. By using bit-encoded storage, multi-directional access permissions can be recorded based on the binary state of the bits, eliminating the need for large data structures like arrays or objects, thus reducing the storage overhead of single-grid direction field data. If different grids have different configured numbers of accessible directions, the number of bits in the encoding field can be adjusted adaptively to improve the scheme's adaptability. Initializing all bits to 0 ensures that all directions are initially blocked when the grid has not undergone any editing operations, preventing the anomaly of defaulting to open directions without editing.
[0028] If the included angle is within a preset angle range, the arrow direction in the current arrow graphic area is set to allow passage, including: determining whether the bit corresponding to the arrow direction in the current arrow graphic area is 0; if the bit corresponding to the arrow direction in the current arrow graphic area is 0, setting the bit corresponding to the arrow in the current arrow graphic area to 1, and setting the opacity of the arrow graphic corresponding to the current arrow graphic area to an opaque state. If the bit corresponding to the arrow direction in the current arrow graphic area is already 1, the repeated setting operation is stopped.
[0029] For example, when the angle between the calculated mouse trajectory vector and the arrow reference direction falls within a preset angle threshold range, the editor does not directly execute the set-to-1 operation. Instead, it first reads the current state of the bit corresponding to the arrow direction through bitwise operations, and determines whether the bit is 0, i.e., whether the direction was originally in a prohibited state. If the bit is already 1, it means that the direction of the grid is already in a permitted state, so no bit rewriting or interface redrawing operation is performed, thereby avoiding redundant bitwise operations and unnecessary graphics rendering refreshes, and reducing CPU processing overhead. Only when the bit is determined to be 0, i.e., the direction is currently prohibited, is the bit set to 1, setting the arrow direction to permitted.
[0030] Specifically, before setting the target bit, it is first determined whether the target bit is already 1. This can be done by right-shifting the direction encoding field by (N - 1 - k) bits and performing a bitwise AND operation with 1. If the result is 0, it indicates that the bit has not yet been set. If it has not been set, the direction encoding field is added to 2 raised to the power of (N - 1 - k) to set the target bit to 1. Specifically, setting the bit to 1 can be done by adding 2 raised to the power of (N - 1 - k) to set the target bit to 1, where N is the total number of bits in the encoding field, and k is the bit index corresponding to the current target direction. The k values for the four bits corresponding to the up, down, left, and right directions are 0, 1, 2, and 3, respectively. Figure 3 Taking the bit position of the arrow pointing to the right as 1 as an example, according to the formula, the mask value is... The rightward direction corresponds to k=3. Substituting this into the equation, we get the exponent: N-1-k = 4-1-3 = 0. The mask is 2^0 = 1, which is 0001 in binary, corresponding to the least significant bit 0 of the encoding field. First, a bit state check is performed, reading the bit corresponding to the rightward direction. Assuming the initial value of the current direction encoding field is 0 (0000 in binary), the rightward bit is determined to be 0, satisfying the condition for setting the bit by addition. The mask is added to the direction encoding field, i.e., 0+1=1. After processing, the direction encoding field value is 1 (0001 in binary). This result only sets the rightward bit 0 to 1, while the remaining bits corresponding to the up, down, and left directions remain 0, indicating that only the rightward direction is allowed. Editing results are stored in the map configuration file via JSON (Lightweight Data Interchange Format). Single-grid direction field data can be expressed as a single decimal integer. The N-bit encoded field uses the exact same storage structure in the editor and at runtime, requiring no data format conversion and allowing for synchronized UI updates. For example, changing the opacity of the arrow graphic to opaque provides visual feedback to the editor indicating that access to that direction has been successfully granted. Bits handle the underlying data storage, while the arrow graphic's opacity handles the upper-level visualization, ensuring that stored data and interface display remain synchronized.
[0031] As an optional implementation of this application, the method further includes: in response to detecting a mouse click event triggered by any arrow graphic area, inverting the bit corresponding to the arrow in the current area and adjusting the opacity of the corresponding arrow graphic.
[0032] For example, the mouse event listening unit continuously listens for mouse click events in the arrow graphic area. This click editing logic is independent of the right-click drag trajectory drawing mode and is used to achieve fine-grained correction of single-area grid passage directions. When a user clicks on an arrow graphic area, the editor inverts the bit mapped to that arrow: if the bit is currently 0, it is changed to 1, corresponding to enabling passage in that direction; if the bit is currently 1, it is changed to 0, corresponding to disabling passage in that direction. After completing the bit state inversion, the opacity of the arrow graphic is adjusted synchronously: when the bit is 1, the arrow graphic switches to an opaque state; when the bit is 0, the arrow graphic switches to a low-opaque state, synchronizing the underlying bit storage data with the editor's visual interface state. This solution can be used to undo and correct misactivated directions generated in trajectory drawing mode, or it can directly complete the enabling and disabling settings for a single direction, achieving a combination of batch drag editing and single-point fine-grained modification editing methods. Specifically, a click event is registered for each arrow graphic. When the click event is triggered, the bit corresponding to the arrow is inverted: if the bit is currently 0, the direction encoding field is added to 2 raised to the power of (N minus 1 minus k) to complete the setting; if the bit is currently 1, the direction encoding field is subtracted from 2 raised to the power of (N minus 1 minus k) to complete the clearing.
[0033] As an optional implementation of this application, the method further includes: if the arrow direction currently set as allowed to pass is a misactivated direction, then in the trajectory drawing mode, re-detect the right mouse button press and move event to activate the correct allowed to pass direction.
[0034] For example, during batch editing in trajectory drawing mode, factors such as mouse drag trajectory offset and hand tremors may cause an arrow direction to be incorrectly set as allowed, resulting in a misactivated direction. When it is necessary to add the correct passage direction, and the misactivated direction can be retained and does not need to be closed, there is no need to exit trajectory drawing mode or clear the misactivated bit by clicking. The editor continues to listen for the right mouse button press and move event. When the user presses the right mouse button again and drags it across the same grid in the correct direction, the system re-collects the coordinates of the mouse entering and leaving the arrow graphic area and calculates the angle between the trajectory vectors. If the new trajectory angle falls within the preset angle range, the bit corresponding to the correct direction is set to 1, completing the activation of the correct passage direction. After this processing, the grid retains both the original misactivated direction and the newly activated correct direction, enabling multiple passage directions to coexist in the same grid, which is suitable for scenarios where multiple selectable passage paths are allowed in the grid. This processing method only adds the correct passage direction and does not clear the bit of the already set misactivated direction.
[0035] As one specific implementation method of this application, such as Figure 4 The diagram shows the overall flowchart of the grid-based pathfinding direction field visualization editing method based on mouse movement trajectory vectors. It includes: binding mouse operations into various events, including right-click event, holding down the right mouse button to enter the arrow graphic area event, mouse leaving the arrow graphic area event, right-click release event, and click event; dividing the map into an R-row, C-column grid array, with each grid assigned an N-bit encoding field. In this embodiment, N is 4, where the 3rd bit (most significant bit) indicates whether upward movement is allowed, the 2nd bit indicates downward movement, the 1st bit indicates leftward movement, and the 0th bit (least significant bit) indicates rightward movement. A bit value of 1 allows movement, and 0 disallows it. For each grid, four arrow graphics are overlaid and rendered, corresponding to the up, down, left, and right directions. The position of the arrow graphics is determined by the grid coordinates plus the offset in each direction, and the rotation angles of the arrow graphics are 0 degrees, 180 degrees, 270 degrees, and 90 degrees respectively. The opacity of the arrow graphic is determined by the value of the corresponding bit; when the corresponding bit is 1, the opacity is 1.0, otherwise it is 0.1. In response to a right-click event, the editor's internal state is set to trajectory drawing mode, and all arrow graphics are displayed. In response to a mouse entering an arrow graphic area, the mouse's entry coordinates within that grid are recorded. Specifically, the horizontal coordinate component of the entry point is obtained by subtracting the horizontal coordinate of the map panel from the horizontal coordinate of the mouse in the screen coordinate system, adding the absolute value of the horizontal coordinate of the map container, and then subtracting the product of the column number and the grid side length; the vertical coordinate component is calculated in the same way.
[0036] Specifically, such as Figure 5The image shows the route editor interface for each area in a grid game. The editor interface is divided into a plot list area on the left and a map editing area on the right. Selecting a plot and clicking "Enable Arrow Mode" will enter the editing interface corresponding to the trajectory drawing mode for that area. This editing interface displays an R-row, C-column grid array. To determine the mouse's entry coordinates within a grid, subtract the coordinates of the top-left corner of the map panel from the mouse's x and y coordinates in the screen coordinate system, add the absolute value of the map container's offset, and finally subtract the grid's offset relative to the map origin. Specifically, the x-coordinate component of the entry coordinate is calculated as: x1 = xs - xp + |xm| - c × B; the y-coordinate component is calculated as: y1 = ys - yp + |ym| - r × B. Where xs and ys are the x and y coordinates of the mouse in the screen coordinate system, respectively; xp and yp are the x and y coordinates of the top-left corner of the map panel in the screen coordinate system, respectively; xm and ym are the horizontal and vertical offsets (absolute values) of the map container relative to the map panel, respectively; c is the column number of the grid; r is the row number of the grid; B is the side length of each grid; and x1 and y1 are the x and y coordinate components of the entry point, respectively. The formula for calculating the exit point is the same as that for the entry point, only the mouse coordinates need to be replaced with the coordinates at the time of exit, that is, the x coordinate component of the exit point x2 = xs2 - xp + |xm| - c × B, and the y coordinate component of the exit point y2 = ys2 - yp + |ym| - r × B, where xs2 and ys2 are the x and y coordinates of the mouse in the screen coordinate system at the time of exit. Specifically, taking the calculation of the horizontal coordinate x1 as an example, xs is the horizontal coordinate of the mouse in the screen coordinate system, xp is the horizontal coordinate of the top left corner of the map panel, and (xs-xp) gives the horizontal position of the mouse relative to the map panel; the absolute value of the map container offset |xm| is added to obtain the horizontal coordinate of the mouse in the map container coordinate system; then the product of the target grid column number c and the grid side length B, c × B, is subtracted, that is, the horizontal offset of the top left corner of the target grid in the map container is deducted, and finally x1 is obtained, which represents the horizontal distance of the mouse point relative to the left edge of the target grid itself.
[0037] like Figure 6The following is a specific example of coordinate calculation. Assume a grid is located in the 2nd row and 3rd column (row number r=2, column number c=3), and the grid side length B is 80 pixels. The top-left corner of the map panel on the screen has coordinates (xp=100, yp=50). The map container's offset relative to the map panel is (xm=0, ym=0), meaning no offset. When the mouse's coordinates in the screen coordinate system are (xs=400, ys=250), the calculation process for the x-coordinate component of the input point is as follows: x1 = xs - xp + |xm| - c × B = 400 - 100 + 0 - 3 × 80 = 60 The calculation process for the ordinate component of the coordinate point is as follows: y1 = ys - yp + |ym| - r × B = 250 - 50 + 0 - 2 × 80 = 40 Therefore, the entry coordinate point is (x1=60, y1=40), representing the local coordinate position of the mouse within this grid relative to the top-left corner of the grid. Here, xs and ys are the horizontal and vertical coordinates of the mouse in the screen coordinate system, respectively; xp and yp are the horizontal and vertical coordinates of the top-left corner of the map panel in the screen coordinate system, respectively; xm and ym are the horizontal and vertical offsets of the map container relative to the map panel, respectively, using absolute values because the map container may offset in the negative direction; c is the column number of the grid; r is the row number of the grid; B is the side length of each grid (in pixels); and x1 and y1 are the horizontal and vertical coordinate components of the entry coordinate point, respectively.
[0038] When the mouse leaves the area indicated by the arrow graphic, record the departure coordinates. Let the mouse coordinates in the screen coordinate system at the time of departure be (xs²=440, ys²=180). The calculation process for the departure coordinates is as follows: x2 = xs2 - xp + |xm| - c × B = 440 - 100 + 0 - 3 × 80 = 100 y2 = ys2 - yp + |ym| - r × B = 180 - 50 + 0 - 2 × 80 = -30 Therefore, the departure coordinate point is (x2=100, y2=-30). The trajectory vector is the vector pointing from the entry coordinate point (60, 40) to the departure coordinate point (100, -30), with a horizontal component of 100-60=40 and a vertical component of -30-40=-70. Since the vertical axis is positive downwards in the screen coordinate system, the vertical component needs to be negative to convert it to a mathematical coordinate system before calculating the angle. The arctangent function is used to calculate the angle between this vector and the direction directly upwards: θ = arctan(40 / 70) ≈ 30 degrees. The absolute value of the difference between this trajectory angle of 30 degrees and the reference angle of 0 degrees in the "upward" direction is approximately 30 degrees, which is less than the preset threshold (e.g., 45 degrees). Therefore, it is determined that the "upward" direction is activated, and the 3rd bit of the grid direction encoding field is set to 1.
[0039] In response to the mouse leaving the same arrow graphic area, the departure coordinate point is recorded and the trajectory angle is calculated. The calculation method for the departure coordinate point is the same as that for the entry coordinate point; simply substitute the mouse coordinates at the time of departure into the formula mentioned above. Then, the arctangent function is used to calculate the direction angle of the vector from the entry coordinate point to the departure coordinate point, and normalized to the interval of 0 to 360 degrees to obtain the trajectory angle. The direction reference angle corresponding to the arrow is taken, and the absolute value of the difference between the trajectory angle and the direction reference angle is calculated. If the absolute value is less than 45 degrees, it is determined that the mouse dragging intention is to activate that direction. When setting bit positions, it is first determined whether the target bit is already 1. Specifically, the direction encoding field is shifted right by (3 minus k) bits and then bitwise ANDed with 1. If the result is 0, the direction encoding field is added to 2 raised to the power of (3 minus k) to set the target bit to 1, and the opacity of the arrow graphic is set to 1.0. In response to the right mouse button release event, the current trajectory drawing operation ends. The editing method provided in the above embodiments allows batch editing to be achieved when the editor holds down the right mouse button and continuously slides across several grids in a certain direction. The arrow in each grid that is closest to the sliding direction will be automatically lit.
[0040] like Figure 7As shown, in this embodiment, the single-grid direction encoding field is a 4-bit integer. Taking the direction encoding field value as binary 1010 (i.e., decimal 10) as an example, the 3rd bit being 1 indicates that upward movement is allowed, the 1st bit being 1 indicates that leftward movement is allowed, and the 2nd and 0th bits being 0 indicate that downward and rightward movement is not allowed. The specific process for reading whether a certain direction is allowed is as follows: shift the direction encoding field to the right by the corresponding number of bits and perform a bitwise AND operation with the value 1. A result of 1 indicates that movement is allowed, and a result of 0 indicates that movement is not allowed. The specific process for setting the bit is as follows: shift the direction encoding field to the right by (3 minus k) bits and perform a bitwise AND operation with 1. Determine if the target bit is 0. If it is 0, add 2 to the (3 minus k) power of the direction encoding field to set the target bit to 1. The specific process for inverting the bit is as follows: first determine the current value of the target bit using the above method. If it is 0, perform the setting operation; if it is 1, subtract 2 to the (3 minus k) power of the direction encoding field to clear the bit.
[0041] This embodiment demonstrates the use of orientation field data during runtime, proving that the editor and runtime share the same data structure. When a moving unit enters a new grid, the orientation encoding field of that grid is read. Four orientation vectors are preset: upward (ordinate minus 1), downward (ordinate increment 1), leftward (horizontal coordinate minus 1), and rightward (horizontal coordinate increment 1). The four directions are traversed, and the bit index i = N-1-k corresponding to each direction is obtained. For each bit, a judgment is performed: the orientation encoding field is shifted right by i bits and then bitwise ANDed with 1. If the result is 1, the corresponding orientation vector is added to the candidate orientation set. After the traversal is complete, if the candidate orientation set is not empty, one is randomly selected as the current orientation for that grid. When multiple allowed orientations exist for a grid, the random selection mechanism naturally creates a multi-path flow effect.
[0042] like Figure 8 As shown, when the editor holds down the right mouse button and slides horizontally to the right, the arrow closest to the rightward direction (reference angle 90 degrees) in each grid cell the mouse path passes through will be automatically highlighted. Traditional methods require six clicks per cell, while the method described in this application requires only one drag.
[0043] The grid-based pathfinding method provided in this application divides the map into an R-row, C-column grid array, assigning an N-bit encoding field to each grid, where each bit indicates whether passage in a direction is permitted. Arrow graphics corresponding to the direction number are rendered on each grid. The method enters trajectory drawing mode in response to a right-click event. It records the entry of a coordinate point in response to a mouse entering the arrow graphic area, and records the exit from the coordinate point in response to a mouse leaving event, calculating the trajectory vector angle. The trajectory angle is subtracted from the direction reference angle; if the absolute value of the difference is less than a preset threshold, the corresponding bit is set. Drawing ends in response to a right-click release event. This method enables batch editing of direction fields through single, continuous mouse dragging, significantly reducing the number of operations and CPU event processing overhead. Furthermore, the use of bit-encoded storage compresses the storage overhead of a single grid to 0.5 bytes. The method of batch editing direction fields through single, continuous mouse dragging allows for the editing of 30 rows... The number of full editing operations for a 20-column map was reduced from approximately 2,400 to approximately 20 to 40, a reduction of over 98%; the storage bytes per cell were compressed from 4 to 16 bytes to 0.5 bytes (4 bits), a compression of over 8 times; the number of CPU event processing operations was reduced from being proportional to 4 times the number of grids to being proportional to the number of grids, a reduction of 75%; the editor and runtime data formats are completely identical, with zero conversion overhead.
[0044] This embodiment also provides a grid pathfinding device for implementing the above embodiments and preferred embodiments; details already described will not be repeated. The term "module" as used below refers to a combination of software and / or hardware that performs a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, hardware implementations, or combinations of software and hardware, are also possible and contemplated.
[0045] This embodiment provides a grid pathfinding device, such as Figure 9 As shown, it includes: Display module 201 is used to display a grid array on the trajectory drawing interface, wherein each grid in the grid array renders an arrow graphic corresponding to the number of passable directions; The first determination module 202 is used to determine whether to enter the trajectory drawing mode in response to the detection of a right mouse button press event; The first detection module 203 is used to detect whether the movement direction of the mouse in the arrow graphic area and the arrow direction corresponding to the arrow graphic area meet the consistency requirement in response to the detection that the right mouse button is pressed and the mouse enters any arrow graphic area of any target grid. The setting module 204 is configured to set the arrow direction in the current arrow graphic area to allow passage if the movement direction of the mouse in the arrow graphic area meets the consistency requirement with the arrow direction corresponding to the arrow graphic area, and the angle between the movement direction of the mouse in the arrow graphic area and the arrow direction in the current arrow graphic area is within a preset angle range. For detailed explanation, please refer to the relevant descriptions in the above method embodiments, which will not be repeated here.
[0046] In this embodiment, the grid pathfinding device is presented in the form of a functional unit. Here, a unit refers to an ASIC circuit, a processor and memory that execute one or more software or fixed programs, and / or other devices that can provide the above functions.
[0047] The grid pathfinding device provided in this embodiment renders arrow graphics corresponding to the passable directions on the interface. In trajectory drawing mode, by holding down and dragging the right mouse button, it determines whether the actual movement direction of the mouse in the arrow graphic area meets the arrow direction requirement. If the consistency requirement is met, it further determines whether the included angle falls within a preset angle range to confirm whether the corresponding passable direction setting is completed. Compared with the mode of editing by clicking in each direction or relying on drop-down menus or numerical input in related technologies, this application can complete the direction field editing in batches by dragging the trajectory with the mouse, reducing a large number of repetitive click operations and improving editing efficiency. The editing process relies on the visual arrow graphics and the mouse drag trajectory to complete the direction matching, eliminating the need for the editor to remember abstract direction encoding rules and reducing cognitive burden. At the same time, multiple passable directions can be set on the same grid, which can realize the expression of multi-branch and optional paths, overcoming the deficiency of the limited expression capability of the automatic path generation scheme of A-star. Moreover, the direction configuration of multiple grids can be completed by a single drag operation, reducing the multiple independent event processing and interface redrawing caused by frequent clicks and reducing CPU event processing overhead.
[0048] Further functional descriptions of the above modules are the same as those in the corresponding embodiments described above, and will not be repeated here.
[0049] This invention also provides an electronic device having the above-described features. Figure 9 The grid-based pathfinding device shown.
[0050] Please see Figure 10 , Figure 10 This is a schematic diagram of the structure of a device provided in an optional embodiment of the present invention, such as... Figure 10As shown, the device may include: at least one processor 601, such as a central processing unit (CPU), at least one communication interface 603, memory 604, and at least one communication bus 602. The communication bus 602 is used to enable communication between these components. The communication interface 603 may include a display screen or a keyboard; optionally, the communication interface 603 may also include a standard wired interface or a wireless interface. The memory 604 may be high-speed volatile random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Optionally, the memory 604 may also be at least one storage device located remotely from the aforementioned processor 601. The processor 601 may be combined with... Figure 9 The described apparatus has an application program stored in memory 604, and a processor 601 calls the program code stored in memory 604 to perform any of the above method steps.
[0051] The communication bus 602 can be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, etc. The communication bus 602 can be divided into an address bus, a data bus, and a control bus, etc. For ease of representation, Figure 10 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.
[0052] The memory 604 may include volatile memory, such as random-access memory (RAM); the memory may also include non-volatile memory, such as flash memory, hard disk drive (HDD) or solid-state drive (SSD); the memory 604 may also include a combination of the above types of memory.
[0053] The processor 601 can be a central processing unit (CPU), a network processor (NP), or a combination of a CPU and an NP.
[0054] The processor 601 may further include a hardware chip. This hardware chip may be an application-specific integrated circuit (ASIC), a programmable logic device (PLD), or a combination thereof. The PLD may be a complex programmable logic device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof.
[0055] Optionally, memory 604 is also used to store program instructions. Processor 601 can call the program instructions to implement the functions described in this application. Figures 1 to 8 The grid pathfinding method shown in the embodiment.
[0056] This disclosure also provides a computer-readable storage medium in which the methods described in this disclosure can be implemented in hardware or firmware, or implemented as recordable on a storage medium, or implemented as computer code originally stored on a remote storage medium or a non-transitory machine-readable storage medium and subsequently stored on a local storage medium after being downloaded over a network. Thus, the methods described herein can be processed by software stored on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. The storage medium may be a magnetic disk, optical disk, read-only memory, random access memory, flash memory, hard disk, or solid-state drive, etc.; further, the storage medium may also include combinations of the above types of memory. It is understood that computers, processors, microprocessor controllers, or programmable hardware include storage components capable of storing or receiving software or computer code that, when accessed and executed by the computer, processor, or hardware, implements the methods shown in the above embodiments.
[0057] A portion of this disclosure can be applied to computer program products, such as computer program instructions, which, when executed by a computer, can invoke or provide methods and / or technical solutions according to this disclosure through the operation of the computer. Those skilled in the art will understand that the forms in which computer program instructions exist in a computer-readable medium include, but are not limited to, source files, executable files, and installation package files. Accordingly, the ways in which computer program instructions are executed by a computer include, but are not limited to: the computer directly executing the instructions; the computer compiling the instructions and then executing the corresponding compiled program; the computer reading and executing the instructions; or the computer reading and installing the instructions and then executing the corresponding installed program. Here, the computer-readable medium can be any available computer-readable storage medium or communication medium accessible to a computer.
[0058] Although embodiments of the present disclosure have been described in conjunction with the accompanying drawings, those skilled in the art can make various modifications and variations without departing from the spirit and scope of the present disclosure, and such modifications and variations all fall within the scope defined by the appended claims.
Claims
1. A grid pathfinding method, characterized in that, include: The trajectory drawing interface displays a grid array, and each grid in the grid array renders an arrow graphic corresponding to the number of passable directions. In response to the detection of a right mouse button press event, it is determined that the trajectory drawing mode has been entered; In response to detecting that the mouse is pressed and held to enter any arrow graphic area of any target grid, the system detects whether the movement direction of the mouse in the arrow graphic area meets the consistency requirement with the arrow direction corresponding to the arrow graphic area. If the direction of mouse movement in the arrow graphic area meets the consistency requirement with the arrow direction corresponding to the arrow graphic area, and the angle between the direction of mouse movement in the arrow graphic area and the arrow direction in the current arrow graphic area is within a preset angle range, then the arrow direction in the current arrow graphic area is set to allow passage.
2. The method according to claim 1, characterized in that, When the angle between the mouse's movement direction in the arrow graphic area and the arrow direction in the current arrow graphic area is within a preset angle range, the arrow direction in the current arrow graphic area is set to allow passage, including: Record the first coordinate point when the mouse enters the arrow graphic area and the second coordinate point when the mouse leaves the arrow graphic area; Based on the first coordinate point and the second coordinate point, calculate the angle between the mouse's movement direction in the arrow graphic area and the arrow direction in the current arrow graphic area; If the included angle is within the preset angle range, then the arrow direction in the current arrow graphic area is set to allow passage.
3. The method according to claim 1, characterized in that, The method further includes: The trajectory drawing is considered complete upon detecting a right mouse button release event.
4. The method according to claim 1, characterized in that, Each grid in the grid array is assigned a coded field of multiple bits. Each bit represents whether a direction of the corresponding grid is allowed to pass. The number of bits is determined according to the number of passable directions configured for the corresponding grid. The default initial value of each bit is set to 0, which means that the corresponding arrow direction is not passable. If the included angle is within a preset angle range, then setting the arrow direction in the current arrow graphic area to allow passage includes: Determine if the bit corresponding to the arrow direction in the current arrow graphic area is 0; If the bit corresponding to the arrow direction in the current arrow graphic area is 0, set the bit corresponding to the arrow in the current arrow graphic area to 1, and set the opacity of the arrow graphic in the current arrow graphic area to an opaque state.
5. The method according to claim 4, characterized in that, The method further includes: If the bit corresponding to the arrow direction in the current arrow graphic area is already 1, then stop repeating the bit setting operation.
6. The method according to claim 4, characterized in that, The method further includes: In response to a mouse click event triggered by detecting any arrow graphic area, the bits corresponding to the arrow in the current area are inverted and the opacity of the corresponding arrow graphic is adjusted.
7. The method according to claim 1, characterized in that, The method further includes: If the arrow direction currently set as allowed to pass is incorrectly activated, the right mouse button press and move event will be re-detected in the trajectory drawing mode to activate the correct allowed to pass direction.
8. A grid-based pathfinding device, characterized in that, include: The display module is used to display a grid array on the trajectory drawing interface, wherein each grid in the grid array renders an arrow graphic corresponding to the number of passable directions; The first determination module is used to determine whether to enter the trajectory drawing mode in response to the detection of a right mouse button press event; The first detection module is used to detect whether the movement direction of the mouse in the arrow graphic area and the arrow direction corresponding to the arrow graphic area meet the consistency requirement in response to the detection that the right mouse button is pressed and the mouse enters any arrow graphic area of any target grid. The setting module is used to set the arrow direction in the current arrow graphic area to allow passage if the movement direction of the mouse in the arrow graphic area meets the consistency requirement with the arrow direction corresponding to the arrow graphic area, and the angle between the movement direction of the mouse in the arrow graphic area and the arrow direction in the current arrow graphic area is within a preset angle range.
9. An electronic device, characterized in that, include: A memory and a processor are communicatively connected, the memory stores computer instructions, and the processor executes the computer instructions to perform the grid pathfinding method according to any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions for causing the computer to perform the grid pathfinding method according to any one of claims 1-7.
