CN114949858A - Collision detection method for character movement in game - Google Patents
Collision detection method for character movement in game Download PDFInfo
- Publication number
- CN114949858A CN114949858A CN202210633831.7A CN202210633831A CN114949858A CN 114949858 A CN114949858 A CN 114949858A CN 202210633831 A CN202210633831 A CN 202210633831A CN 114949858 A CN114949858 A CN 114949858A
- Authority
- CN
- China
- Prior art keywords
- point
- obstacle
- obs
- vector
- static
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Images
Classifications
-
- A—HUMAN NECESSITIES
- A63—SPORTS; GAMES; AMUSEMENTS
- A63F—CARD, BOARD, OR ROULETTE GAMES; INDOOR GAMES USING SMALL MOVING PLAYING BODIES; VIDEO GAMES; GAMES NOT OTHERWISE PROVIDED FOR
- A63F13/00—Video games, i.e. games using an electronically generated display having two or more dimensions
- A63F13/55—Controlling game characters or game objects based on the game progress
- A63F13/57—Simulating properties, behaviour or motion of objects in the game world, e.g. computing tyre load in a car race game
- A63F13/577—Simulating properties, behaviour or motion of objects in the game world, e.g. computing tyre load in a car race game using determination of contact between game characters or objects, e.g. to avoid collision between virtual racing cars
Abstract
The invention provides a collision detection method for character movement in a game, which comprises the following steps: according to the radius of the game role, adopting an obstacle expansion algorithm to expand the static obstacle to obtain a static obstacle expansion body; writing each grid covered by the grid into a grid index; expanding the dynamic barrier to obtain a dynamic barrier expansion body, and refreshing the grid index of each grid covered by the dynamic barrier expansion body in the game process; when the game character needs to move, the obstacle with collision is detected according to the position and the velocity vector of the game character, and the collision point and the moving direction after collision are calculated. The invention is a method for detecting collision between a role and a barrier and determining the movement direction of the color after collision, which is suitable for games, has a simple scheme and simultaneously considers efficiency.
Description
Technical Field
The invention relates to a collision detection method, in particular to a collision detection method for character movement in a game.
Background
In many games, a character moves and there are many obstacles, and therefore, it is necessary to detect a collision between the character and an obstacle and to determine a movement direction of the character after the collision.
In conventional approaches, the angular movement orientation is confirmed after a collision using a game engine to provide the result of the physical collision. But has the following problems: the calculated amount is large, the game engine is deeply bound, the engineering is huge and difficult to modify, and the customized requirements are not met.
Disclosure of Invention
Aiming at the defects in the prior art, the invention provides a collision detection method for character movement in a game, which can effectively solve the problems.
The technical scheme adopted by the invention is as follows:
the invention provides a collision detection method for character movement in a game, which comprises the following steps:
step 1, a map in a game scene is in a Grid form and is provided with a plurality of Grid grids;
step 2.1, the radius of the game role is R 0 (ii) a According to the radius R of the game role 0 Expanding a Static obstacle Static _ obs (i) by adopting an obstacle expansion algorithm to obtain a Static obstacle expansion body E Static _ obs (i);
step 2.2, writing a Grid index into each Grid covered by the Static obstacle expansion body E Static _ obs (i), wherein the Grid index is as follows: ID of static obstacle S _ obs (i);
and 3, in the game process, regarding the dynamic obstacle, namely the dynamic obstacle Move _ obs (j), performing the following operations in real time:
step 3.1, expanding the dynamic obstacle Move _ obs (j) by adopting an obstacle expansion algorithm in advance to obtain a dynamic obstacle expansion body E Move _ obs (j), and storing;
step 3.2, assuming that the current position of the dynamic obstacle Move _ obs (j) is u1, reading a dynamic obstacle expansion body E Move _ obs (j), and displaying the dynamic obstacle expansion body E Move _ obs (j) to the current position u1 according to the current posture of the dynamic obstacle Move _ obs (j);
for each Grid covered by the dynamic obstacle expander E Move _ obs (j), writing a Grid index, where the Grid index is: ID of dynamic obstacle Move _ obs (j);
step 3.3, when the dynamic obstacle Move _ obs (j) moves, if the dynamic obstacle moves from the position u1 to the position u2, the grid index written in the step 3.2 is cleared first;
reading the dynamic obstacle expansion body E Move _ obs (j), and displaying the dynamic obstacle expansion body E Move _ obs (j) to the current position u2 according to the current posture of the dynamic obstacle Move _ obs (j);
for each Grid covered by the dynamic obstacle expander E Move _ obs (j), writing a Grid index, where the Grid index is: ID of dynamic obstacle Move _ obs (j);
the above is circulated continuously, so as to realize the dynamic display of the dynamic barrier Move _ obs (j);
step 4.1, presetting a search range radius r;
step 4.2, simplifying the role of the game into points, wherein the positions of the points are the positions of the centroids of the role of the game;
starting from the current position A point according to the velocity vectorSearching for a motion path of length r; judging whether a Grid written with a Grid index exists in the Grid grids passed by the motion path; if not, it indicates that the role of the game is according to the velocity vectorWhen the mobile terminal moves to the next position, the mobile terminal does not collide with a Static obstacle Static _ obs (i) and a dynamic obstacle Move _ obs (j);
if yes, acquiring all Grid indexes in Grid grids passed by the motion path, and further acquiring IDs of all static obstacles S _ obs (i) and/or IDs of all dynamic obstacles Move _ obs (j), wherein the obtained IDs of the static obstacles S _ obs (i) and/or IDs of the dynamic obstacles Move _ obs (j) are collectively called as obstacle IDs at this time;
then step 4.3 is executed;
step 4.3, displaying an obstacle expansion body in a map for the obstacle corresponding to each obstacle ID obtained in the step 4.2;
for each obstacle expander, the distance L is calculated using the following method: calculating the distance between the current centroid position of the role of the game and the possible collision point as a distance L, wherein the intersection point of the motion path and the obstacle expansion body is the possible collision point;
then, the distance L corresponding to each obstacle expansion body is compared to obtain the obstacle expansion body corresponding to the minimum value of the distance L, and the distance L is expressed as: an obstacle expander E _ obs (min);
therefore, the intersection point of the movement path and the obstacle expanding body E _ obs (min), which is the finally determined collision point, is represented as: collision point dot (last);
the barrier expansion body E _ obs (min) is used as a normal line at the position of a collision point dot (last), and a connecting line between the current centroid position of the game role and the collision point dot (last) is a motion path of the game role and is positioned on one side of the normal line; when the role moves to the collision point dot (last), the outer boundary of the obstacle expander E _ obs (min) on the other side of the normal moves, namely the detected moving direction after the collision;
the collision point and the direction of movement after the collision are determined so far.
Preferably, the Static obstacle-extension body E Static _ obs (i) and the dynamic obstacle-extension body EMove _ obs (j), both of which are in the shape of an obstacle polygon.
Preferably, in step 2.1, the radius R of the game character is determined according to 0 Expanding the Static obstacle Static _ obs (i) by adopting an obstacle expansion algorithm to obtain a Static obstacle expansion body E Static _ obs (i), which specifically comprises the following steps:
step 2.1.1, simplifying a Static obstacle Static _ obs (i) into an obstacle polygon;
step 2.1.2, judging whether the barrier polygon is a convex polygon, if so, directly expanding the barrier to obtain a Static barrier expansion body E Static _ obs (i); if not, the obstacle polygon is a concave polygon, the concave polygon is convexly solved into a plurality of convex polygons, obstacle expansion is independently performed on each convex polygon, so that a plurality of Static obstacle expansion bodies are obtained, and then the plurality of Static obstacle expansion bodies are combined to obtain a final Static obstacle expansion body E Static _ obs (i).
Preferably, for convex polygons, the obstacle expansion is performed in the following manner:
step 2.1.2.1, assuming that the convex polygon is an n-polygon with n vertices, the n vertices are arranged clockwise, and are sequentially represented as: b is 1 ,B 2 ,...,B n (ii) a Therefore, in clockwise arrangement, the resulting n-edge vectors are sequentially represented as:
the game role is radius R 0 The circle of (a); obtaining a circle inscribed hexagon of the game role; the 6 vertexes of the circle inscribed hexagon are arranged clockwise, and are sequentially expressed as follows: k is 1 ,K 2 ,...,K 6 (ii) a Therefore, in clockwise arrangement, the resulting 6-edge vectors are sequentially represented as:
vector of 6 edgesThe direction and length of the edge vector are kept unchanged, and the 6 edge vectors are moved to converge the starting points of the 6 edge vectors intoOne point K 0 This results in a hexagonal expansion body EK which has a center point K 0 The vectors with 6 edges are arranged in the clockwise direction and respectively are:
step 2.1.2.2, first align the edge vectorsAnd (3) expanding, wherein the expanding mode is as follows:
s1.1) edge vectorIs point B 1 (ii) a The hexagonal expansion body EK is moved in parallel to make the center point K of the hexagonal expansion body EK 0 And point B 1 Overlapping;
point B 1 Is an edge vectorEnd point, opposite side vectorElongation is carried out, the elongation point is represented as B " 1 Thereby obtaining a first reference line vector
s1.2) 6-sided vector of hexagonal expansion body EKWherein the extreme value is identified as being greater than the first reference line vectorAnd is less than or equal to the second reference line vectorThe set of edge vectors of (1) is set to have f edge vectors, and the f edge vectors are sorted according to a clockwise order and are sequentially represented as follows:wherein g1, g2, gf e [1,2, 6 ]];
Translation edge vectorMake the edge vectorStarting point K of 0 And point B 1 Overlapping; then, the edge vector is translatedMake itStarting point K of 0 And the edge vectorEnd point K of g1 Coincidence, and so on, until the opposite side vector is completedBy shifting the edge vectorThe heads and the tails are connected;
will point K gf Renamed as point C 1 ;
S1.3) by point C 1 As a starting point, an edge vector is drawnMake the edge vectorAnd the edge vectorParallel and equal length, point C 2 Is a vector of opposite edgePerforming an expanded end point;
this completes the opposite side vectorEnd point is point C 2 (ii) a Point B 1 To point C 2 The passed edge vector is the expanded edge vector;
s2.1) moving the hexagonal expansion body EK in parallel to enable the center point K of the hexagonal expansion body EK 0 And point C 2 Overlapping;
s2.2) Point C 2 Is an edge vectorEnd point, opposite side vectorElongation was carried out with the elongation point denoted C " 2 Thereby obtaining a first reference line vector
At point C 2 As a starting point, make an edge vectorParallel edge vectors as second reference line vectors;
s2.3) 6-edge vector of hexagonal expansion body EKWherein the extreme value is identified as being greater than the first reference line vectorAnd the edge vectors are less than or equal to the edge vector set between the second reference line vectors, so that all the edge vectors of the obtained edge vector set are sorted according to a clockwise direction, and after the edge vectors are connected end to end, the starting point and the point C are connected end to end 2 The end points after the superposition and the head-to-tail connection are named as points C 3 ;
S2.4) by point C 3 As a starting point, the edge vector is drawn so that the edge vectorAnd the edge vectorParallel and equal length, point C 4 Is a vector of opposite edgePerforming an expanded end point;
this completes the opposite side vectorEnd point is point C 4 (ii) a Point C 2 To point C 4 The passed edge vector is the expanded edge vector;
and 2.1.2.4, finally forming a Static obstacle expander E Static _ obs (i).
The collision detection method for character movement in the game provided by the invention has the following advantages:
the method is suitable for games, the roles and the barriers are subjected to collision detection, and the color movement direction after collision is determined, the scheme is simple, the customization requirements are met, and meanwhile, the efficiency is considered.
Drawings
FIG. 1 is a schematic flow chart of a collision detection method for character movement in a game according to the present invention;
FIG. 2 is a schematic diagram of a collision detection method for character movement in a game according to the present invention;
FIG. 3 is a schematic diagram of a concave polygon convexly divided into a plurality of convex polygons according to the present invention;
FIG. 4 is a schematic view of the convex polygon for obstacle expansion according to the present invention;
FIG. 5 is a schematic diagram of a circular inscribed hexagon provided by the present invention;
FIG. 6 is a schematic diagram of a circle inscribed hexagon provided by the present invention after 6 edges of the circle inscribed hexagon are arranged in a deformed manner;
FIG. 7 is a schematic representation of the barrier provided by the present invention prior to expansion;
fig. 8 is a schematic view of the barrier of the present invention after expansion.
Detailed Description
In order to make the technical problems, technical solutions and advantageous effects solved by the present invention more clearly apparent, the present invention is further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
The invention provides a method for detecting collision between a character and an obstacle and determining the movement direction of the character after collision, which is light and suitable for games.
Referring to fig. 1, the collision detection method for character movement in a game provided by the invention comprises the following steps:
step 1, a map in a game scene is in a Grid form and is provided with a plurality of Grid grids;
step 2.1, the radius of the game role is R 0 (ii) a According to the radius R of the game role 0 Expanding a Static obstacle Static _ obs (i) by adopting an obstacle expansion algorithm to obtain a Static obstacle expansion body E Static _ obs (i);
step 2.2, writing a Grid index into each Grid covered by the Static obstacle expansion body E Static _ obs (i), wherein the Grid index is as follows: ID of static obstacle S _ obs (i);
and 3, in the game process, regarding the dynamic obstacle, which is expressed as a dynamic obstacle Move _ obs (j), performing the following operations in real time:
step 3.1, expanding the dynamic obstacle Move _ obs (j) by adopting an obstacle expansion algorithm in advance to obtain a dynamic obstacle expansion body E Move _ obs (j), and storing;
step 3.2, assuming that the current position of the dynamic obstacle Move _ obs (j) is u1, reading a dynamic obstacle expansion body E Move _ obs (j), and displaying the dynamic obstacle expansion body E Move _ obs (j) to the current position u1 according to the current posture of the dynamic obstacle Move _ obs (j);
for each Grid covered by the dynamic obstacle expander E Move _ obs (j), writing a Grid index, where the Grid index is: ID of dynamic obstacle Move _ obs (j);
step 3.3, when the dynamic obstacle Move _ obs (j) moves, if the dynamic obstacle moves from the position u1 to the position u2, the grid index written in the step 3.2 is cleared first;
reading the dynamic obstacle expansion body E Move _ obs (j), and displaying the dynamic obstacle expansion body E Move _ obs (j) to the current position u2 according to the current posture of the dynamic obstacle Move _ obs (j);
for each Grid covered by the dynamic obstacle expander E Move _ obs (j), writing a Grid index, where the Grid index is: ID of dynamic obstacle Move _ obs (j);
the dynamic display of the dynamic barrier Move _ obs (j) is realized by continuously circulating the steps;
step 4.1, presetting a search range radius r;
step 4.2, simplifying the role of the game into points, wherein the positions of the points are the positions of the centroids of the role of the game;
starting from the current position A point according to the velocity vectorSearching for a motion path of length r; judging whether a Grid written with a Grid index exists in the Grid grids passed by the motion path; if not, it indicates that the role of the game is according to the velocity vectorWhen the mobile terminal moves to the next position, the mobile terminal does not collide with a Static obstacle Static _ obs (i) and a dynamic obstacle Move _ obs (j);
if yes, acquiring all Grid indexes in Grid grids passed by the motion path, and further acquiring IDs of all static obstacles S _ obs (i) and/or IDs of all dynamic obstacles Move _ obs (j), wherein the obtained IDs of the static obstacles S _ obs (i) and/or IDs of the dynamic obstacles Move _ obs (j) are collectively called as obstacle IDs at this time;
then step 4.3 is executed;
step 4.3, displaying an obstacle expansion body in a map for the obstacle corresponding to each obstacle ID obtained in the step 4.2;
for each obstacle expander, the distance L is calculated using the following method: calculating the distance between the current centroid position of the role of the game and the possible collision point as a distance L, wherein the intersection point of the motion path and the obstacle expansion body is the possible collision point;
then, the distance L corresponding to each obstacle expansion body is compared to obtain the obstacle expansion body corresponding to the minimum value of the distance L, and the distance L is expressed as: an obstacle expander E _ obs (min);
therefore, the intersection point of the motion path and the obstacle expander E _ obs (min) is the finally determined collision point, and is represented as: collision point dot (last);
referring to fig. 2, the obstacle expander E _ obs (min) makes a normal line at the collision point dot (last), and a connection line between the current centroid position of the game role and the collision point dot (last) is a motion path of the game role and is located at one side of the normal line; when the role moves to the collision point dot (last), the outer boundary of the obstacle expander E _ obs (min) on the other side of the normal moves, namely the detected moving direction after the collision; the principle here is: the game rules require that the character cannot coincide with the barrier and can only walk along the edge of the barrier, namely, walk close to the edge, so as to determine the moving direction after collision.
The collision point and the direction of movement after the collision are determined so far.
In the present invention, the Static obstacle expander E Static _ obs (i) and the dynamic obstacle expander EMove _ obs (j) are both in the shape of an obstacle polygon.
In the present invention, the Static obstacle Static _ obs (i) is expanded, which is the same as the expansion algorithm for expanding the dynamic obstacle Move _ obs (j), and the following only takes the process of expanding the Static obstacle Static _ obs (i) as an example to describe in detail:
in step 2.1, according to the radius R of the game role 0 Expanding the Static obstacle Static _ obs (i) by adopting an obstacle expansion algorithm to obtain a Static obstacle expansion body E Static _ obs (i), which specifically comprises the following steps:
step 2.1.1, simplifying a Static obstacle Static _ obs (i) into an obstacle polygon;
step 2.1.2, judging whether the barrier polygon is a convex polygon, if so, directly expanding the barrier to obtain a Static barrier expansion body E Static _ obs (i); if not, the obstacle polygon is a concave polygon, the concave polygon is convexly solved into a plurality of convex polygons, obstacle expansion is independently performed on each convex polygon, so that a plurality of Static obstacle expansion bodies are obtained, and then the plurality of Static obstacle expansion bodies are combined to obtain a final Static obstacle expansion body E Static _ obs (i).
As shown in fig. 4, 1 represents a game character and is a circle; 2 represents the circle inscribed hexagon of the game role; 3 represents a point of game character simplification; 4 represents an unexpanded obstruction; and 5 represents an obstacle expander.
Wherein, for the convex polygon, adopt following mode, carry out the barrier expansion:
step 2.1.2.1, assuming that the convex polygon is an n-polygon with n vertices, the n vertices are arranged clockwise, and are sequentially represented as: b is 1 ,B 2 ,...,B n (ii) a Therefore, in clockwise arrangement, the resulting n-edge vectors are sequentially represented as:as shown in fig. 7, when the convex polygon is a rectangle, it has four vertices: b is 1 ,B 2 ,B 3 ,B 4 。
The game role is radius R 0 The circle of (a); obtaining a circle inscribed hexagon of the game role; the 6 vertexes of the circle inscribed hexagon are arranged clockwise, and are sequentially represented as: k 1 ,K 2 ,...,K 6 (ii) a Therefore, in clockwise arrangement, the resulting 6-edge vectors are sequentially represented as:referring to fig. 5, the vector is a 6-edge vector of the inscribed hexagon.
Vector of 6 edgesThe direction and length of the vector are kept unchanged, and the 6 edge vectors are moved to converge the starting points of the 6 edge vectors into a point K 0 This results in a hexagonal expansion body EK having a center point K 0 The vectors with 6 edges are arranged in the clockwise direction and respectively are: referring to fig. 6, a hexagonal expansion body EK is obtained.
Step 2.1.2.2, referring to FIG. 8, first align the edge vectorsAnd (3) expanding, wherein the expanding mode is as follows:
s1.1) edge vectorIs point B 1 (ii) a The hexagonal expansion body EK is moved in parallel to make the center point K of the hexagonal expansion body EK 0 And point B 1 Overlapping;
point B 1 Is an edge vectorEnd point, opposite side vectorElongation is carried out, the elongation point is represented as B " 1 Thereby obtaining a first reference line vector
s1.2) 6-sided vector of hexagonal expansion body EKWherein the extreme value is identified as being greater than the first reference line vectorAnd is less than or equal to the second reference line vectorThe set of edge vectors of (1) is set to have f edge vectors, and the f edge vectors are sorted according to a clockwise order and are sequentially represented as follows:wherein g1, g2, gf e [1,2, 6 ]];
Translation edge vectorMake the edge vectorStarting point K of 0 And point B 1 Overlapping; then, the edge vector is translatedMake itStarting point K of 0 And the edge vectorEnd point K of g1 Coincidence, and so on, until the opposite side vector is completedBy shifting the edge vectorThe heads and the tails are connected;
will point K gf Renamed as point C 1 ;
In this step, for FIG. 8, the extreme value is greater than the first reference line vectorAnd is less than or equal to the second reference line vectorThe edge vector set of (2) has two edge vectors in total, which are respectively: edge vectorSum edge vectorAfter the opposite side vectors are ordered clockwise, the following are: edge vectorSum edge vector
Edge vectorSum edge vectorEnd to end, starting point and point B 1 Coincidence, the end point being point C 1 。
S1.3) by point C 1 As a starting point, an edge vector is drawnMake the edge vectorAnd the edge vectorParallel and equal length, point C 2 Is a vector of opposite edgePerforming an expanded end point;
this completes the opposite side vectorEnd point is point C 2 (ii) a Point B 1 To point C 2 The passed edge vector is the expanded edge vector;
s2.1) moving the hexagonal expansion body EK in parallel to enable the center point K of the hexagonal expansion body EK 0 And point C 2 Overlapping;
s2.2) Point C 2 Is an edge vectorEnd point, opposite side vectorElongation was carried out with the elongation point denoted C " 2 Thereby obtaining a first reference line vector
At point C 2 As a starting point, make an edge vectorParallel edge vectors as second reference line vectors;
s2.3) 6-edge vector of hexagonal expansion body EKWherein the extreme value is identified as being greater than the first reference line vectorAnd the edge vectors are less than or equal to the edge vector set between the second reference line vectors, so that all the edge vectors of the obtained edge vector set are sorted according to a clockwise direction, and after the edge vectors are connected end to end, the starting point and the point C are connected end to end 2 The end points after the superposition and the head-to-tail connection are named as points C 3 ;
In this step, for FIG. 8, the extreme value is greater than the first reference line vectorAnd only one edge vector, namely the edge vector, in the edge vector set between the vectors of the second reference lines is less than or equal to
S2.4) by point C 3 As a starting point, an edge vector is drawnMake the edge vectorAnd the edge vectorParallel and equal length, point C 4 Is a vector of opposite edgePerforming an expanded end point;
this completes the opposite side vectorEnd point is point C 4 (ii) a Point C 2 To point C 4 The passed edge vector is the expanded edge vector;
and 2.1.2.4, finally forming a Static obstacle expander E Static _ obs (i).
The principle of the invention is described below:
the invention uses the circle inscribed hexagon with the same radius as the role as the expansion body to expand the polygon of the obstacle, thereby obtaining the obstacle expansion body. Then, the relationship between the character and the obstacle is simplified to a relationship between the point and an obstacle extension body (which is a polygon).
For static obstacles, the polygon data of the obstacle expansion body is calculated in advance, and a grid index is written in a grid covered by the area of the polygon data. The polygon data of the obstacle expansion body of the static obstacle is calculated in advance and written in, and the performance of subsequent collision detection can be improved.
For the dynamic barrier, in the game process, calculating the grid covered by the barrier expansion body in real time, and refreshing the grid index, namely: and deleting the grid index of the grid covered by the old position, and writing the grid index of the grid covered by the new position. In this step, the shape of the obstacle expander of the dynamic obstacle is pre-calculated and stored; then, during the game progress, the position thereof is calculated only in real time, thereby improving the performance.
Therefore, in the process of game, acquiring a character position and a grid index covered by a path formed by the speed direction of the character, searching out a corresponding obstacle ID, calculating by using a position vector formed by combining the position of the center of a circle of the character and the speed and each searched obstacle expander, and obtaining a collision point with each obstacle expander; and the collision point with the nearest distance is the detected collision point. And the moving direction of the color after collision can be determined by combining the normal line of the collision point position.
When calculating the obstacle expansion body, if the obstacle is a concave polygon, the obstacle is convexly solved into a plurality of convex polygons and then expanded. The specific method comprises the following steps: the points of the concave polygon can be arranged by a writing tool, and the convex polygon is artificially divided into a plurality of convex polygons. Or automatic calculation can be carried out, and convex solution is completed according to the flow of triangularization of the concave polygon and synthesis of the convex polygon by the triangle. As shown in FIG. 3, the 6-sided polygon defined by the vertices W1-W2-W3-W4-W5-W6-W1 is a concave polygon (the concave polygon is defined as having an interior angle greater than or equal to 90 degrees, i.e. a concave polygon), and is convexly decomposed into two convex polygons, which are: a first convex polygon W1-W2-W3-W6 and a second convex polygon W6-W3-W4-W5. Then, respectively expanding the first convex polygon and the second convex polygon to obtain an expansion body enclosed by W18-W19-W20-W21-W22-W23-W24-W25-W26-W27 after the first convex polygon is expanded; and expanding the second convex polygon to obtain an expansion body surrounded by W8-W9-W10-W21-W22-W13-W14-W15-W16-W7.
Therefore, the invention converts the relationship between the character and the obstacle into the relationship between the point and the polygon. And the static obstacles and the dynamic obstacles are treated separately. By pre-calculating the expansion body of the static barrier and the expansion body of the dynamic barrier and refreshing the grids covered by the expansion body of the dynamic barrier in the game running process, the collision detection efficiency is improved.
The collision detection method for character movement in the game provided by the invention has the following advantages:
the method is suitable for games, the role and the barrier are subjected to collision detection, and the color movement direction after collision is determined, the scheme is simple, and meanwhile, the efficiency is considered.
The foregoing is only a preferred embodiment of the present invention, and it should be noted that, for those skilled in the art, various modifications and improvements can be made without departing from the principle of the present invention, and such modifications and improvements should also be considered within the scope of the present invention.
Claims (4)
1. A collision detection method for character movement in a game is characterized by comprising the following steps:
step 1, a map in a game scene is in a Grid form and is provided with a plurality of Grid grids;
step 2, displaying a plurality of static obstacles in a map; for any static obstacle, it is expressed as: static obstacles Static _ obs (i), the following operations are performed:
step 2.1, the radius of the game role is R 0 (ii) a According to the radius R of the game role 0 Expanding a Static obstacle Static _ obs (i) by adopting an obstacle expansion algorithm to obtain a Static obstacle expansion body E Static _ obs (i);
step 2.2, writing a Grid index into each Grid covered by the Static obstacle expander E Static _ obs (i), wherein the Grid index is as follows: ID of static obstacle S _ obs (i);
and 3, in the game process, regarding the dynamic obstacle, namely the dynamic obstacle Move _ obs (j), performing the following operations in real time:
step 3.1, expanding the dynamic obstacle Move _ obs (j) by adopting an obstacle expansion algorithm in advance to obtain a dynamic obstacle expansion body E Move _ obs (j), and storing;
step 3.2, assuming that the current position of the dynamic obstacle Move _ obs (j) is u1, reading a dynamic obstacle expansion body E Move _ obs (j), and displaying the dynamic obstacle expansion body E Move _ obs (j) to the current position u1 according to the current posture of the dynamic obstacle Move _ obs (j);
for each Grid covered by the dynamic obstacle expander E Move _ obs (j), writing a Grid index, where the Grid index is: ID of dynamic obstacle Move _ obs (j);
step 3.3, when the dynamic obstacle Move _ obs (j) moves, if the dynamic obstacle moves from the position u1 to the position u2, the grid index written in the step 3.2 is cleared first;
reading the dynamic obstacle expansion body E Move _ obs (j), and displaying the dynamic obstacle expansion body E Move _ obs (j) to the current position u2 according to the current posture of the dynamic obstacle Move _ obs (j);
for each Grid covered by the dynamic obstacle expander E Move _ obs (j), writing a Grid index, where the Grid index is: ID of dynamic obstacle Move _ obs (j);
the dynamic display of the dynamic barrier Move _ obs (j) is realized by continuously circulating the steps;
step 4, in the process of game, when the role needs to move, the centroid of the role is assumed to be located at the point A, and the coordinate is (x) A ,y A ) The radius of the game character is R 0 The velocity vector of which isDetermining a role velocity vector in accordance with a game character using the following methodMoving collision point and post-collision moving direction:
step 4.1, presetting a search range radius r;
step 4.2, simplifying the role of the game into points, wherein the positions of the points are the positions of the centroids of the role of the game;
starting from the current position A point according to the velocity vectorThe direction of (d) searches for a motion path of length r; judging whether a Grid written with a Grid index exists in the Grid grids passed by the motion path; if not, it indicates that the role of the game is according to the velocity vectorWhen the mobile terminal moves to the next position, the mobile terminal does not collide with a Static obstacle Static _ obs (i) and a dynamic obstacle Move _ obs (j);
if yes, acquiring all Grid indexes in Grid grids passed by the motion path, and further acquiring IDs of all static obstacles S _ obs (i) and/or IDs of all dynamic obstacles Move _ obs (j), wherein the obtained IDs of the static obstacles S _ obs (i) and/or IDs of the dynamic obstacles Move _ obs (j) are collectively called as obstacle IDs at this time;
then step 4.3 is executed;
step 4.3, displaying an obstacle expansion body in a map for the obstacle corresponding to each obstacle ID obtained in the step 4.2;
for each obstacle expander, the distance L is calculated using the following method: calculating the distance between the current centroid position of the role of the game and the possible collision point as a distance L, wherein the intersection point of the motion path and the obstacle expansion body is the possible collision point;
then, the distance L corresponding to each obstacle expansion body is compared to obtain the obstacle expansion body corresponding to the minimum value of the distance L, and the distance L is expressed as: an obstacle expander E _ obs (min);
therefore, the intersection point of the motion path and the obstacle expander E _ obs (min) is the finally determined collision point, and is represented as: collision point dot (last);
the barrier expansion body E _ obs (min) is used as a normal line at the position of a collision point dot (last), and a connecting line between the current centroid position of the game role and the collision point dot (last) is a motion path of the game role and is positioned on one side of the normal line; when the role moves to the collision point dot (last), the outer boundary of the obstacle expander E _ obs (min) on the other side of the normal moves, that is, the detected moving direction after the collision;
the collision point and the direction of movement after the collision are determined so far.
2. The collision detection method of character movement in a game according to claim 1, wherein the Static obstacle-extension body E Static _ obs (i) and the dynamic obstacle-extension body E Move _ obs (j), both of which are obstacle polygon shapes.
3. The method of claim 1, wherein in step 2.1, the collision detection method is based on the radius R of the game character 0 Expanding the Static obstacle Static _ obs (i) by adopting an obstacle expansion algorithm to obtain a Static obstacle expansion body E Static _ obs (i), which specifically comprises the following steps:
step 2.1.1, simplifying a Static obstacle Static _ obs (i) into an obstacle polygon;
step 2.1.2, judging whether the barrier polygon is a convex polygon, if so, directly expanding the barrier to obtain a Static barrier expansion body E Static _ obs (i); if not, the obstacle polygon is a concave polygon, the concave polygon is convexly solved into a plurality of convex polygons, obstacle expansion is independently performed on each convex polygon, so that a plurality of Static obstacle expansion bodies are obtained, and then the plurality of Static obstacle expansion bodies are combined to obtain a final Static obstacle expansion body E Static _ obs (i).
4. The method of claim 3, wherein the convex polygon is expanded by the following method:
step 2.1.2.1, assuming that the convex polygon is an n-polygon with n vertices, the n vertices are arranged clockwise, and are sequentially represented as: b is 1 ,B 2 ,...,B n (ii) a Therefore, in clockwise arrangement, the resulting n-edge vectors are sequentially represented as:
the game role is radius R 0 The circle of (a); obtaining a circle inscribed hexagon of the game role; the 6 vertexes of the circle inscribed hexagon are arranged clockwise, and are sequentially represented as: k 1 ,K 2 ,...,K 6 (ii) a Therefore, in clockwise arrangement, the resulting 6 edge vectors are sequentially represented as:
vector of 6 edgesThe direction and length of the vector are kept unchanged, and the 6 edge vectors are moved to converge the starting points of the 6 edge vectors into a point K 0 This results in a hexagonal expansion body EK which has a center point K 0 The vectors with 6 edges are arranged in the clockwise direction and respectively are:
step 2.1.2.2, first align the edge vectorsAnd (3) expanding, wherein the expanding mode is as follows:
s1.1) edge vectorIs point B 1 (ii) a The hexagonal expansion body EK is moved in parallel to make the center point K of the hexagonal expansion body EK 0 And point B 1 Overlapping;
point B 1 Is an edge vectorEnd point, opposite side vectorElongation is carried out, the elongation point is represented as B " 1 Thereby obtaining a first reference line vector
Point B 1 Is an edge vectorStarting point of (2), making the edge vectorAs a second reference line vector;
s1.2) 6-sided vector of hexagonal expansion body EKWherein the extreme value is identified as being greater than the first reference line vectorAnd is less than or equal to the second reference line vectorThe set of edge vectors of (1) is set to have f edge vectors in total, the f edge vectors are sorted according to a clockwise order and are sequentially represented as follows:wherein g1, g2, gf e [1,2, 6 ]];
Translation edge vectorMake the edge vectorStarting point K of 0 And point B 1 Overlapping; then, the edge vector is translatedMake it possible toStarting point K of 0 And the edge vectorEnd point K of g1 Coincidence, and so on, until the opposite side vector is completedBy shifting the edge vectorThe heads and the tails are connected;
will point K gf Renamed as point C 1 ;
S1.3) by point C 1 As a starting point, an edge vector is drawnMake the edge vectorAnd the edge vectorParallel and equal length, point C 2 Is a vector of opposite edgePerforming an expanded end point;
this completes the opposite side vectorEnd point is point C 2 (ii) a Point B 1 To point C 2 The passed edge vector is the expanded edge vector;
s2.1) moving the hexagonal expansion body EK in parallel to enable the center point K of the hexagonal expansion body EK 0 And point C 2 Overlapping;
s2.2) Point C 2 Is an edge vectorEnd point, opposite side vectorElongation was carried out with the elongation point denoted C " 2 Thereby obtaining a first reference line vector
At point C 2 As a starting point, make an edge vectorParallel edge vectors as second reference line vectors;
s2.3) 6-edge vector of hexagonal expansion body EKWherein the extreme value is identified as being greater than the first reference line vectorAnd the edge vectors are less than or equal to the edge vector set between the second reference line vectors, so that all the edge vectors of the obtained edge vector set are sorted according to a clockwise direction, and after the edge vectors are connected end to end, the starting point and the point C are connected end to end 2 The end point after the superposition and the head and the tail are connected is named as a point C 3 ;
S2.4) by point C 3 As a starting point, an edge vector is drawnMake the edge vectorAnd the edge vectorParallel and equal length, point C 4 Is a vector of opposite sidePerforming an expanded end point;
this completes the opposite side vectorEnd point is point C 4 (ii) a Point C 2 To point C 4 The passed edge vector is the expanded edge vector;
and 2.1.2.4, finally forming a Static obstacle expander E Static _ obs (i).
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202210633831.7A CN114949858A (en) | 2022-06-06 | 2022-06-06 | Collision detection method for character movement in game |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202210633831.7A CN114949858A (en) | 2022-06-06 | 2022-06-06 | Collision detection method for character movement in game |
Publications (1)
Publication Number | Publication Date |
---|---|
CN114949858A true CN114949858A (en) | 2022-08-30 |
Family
ID=82960386
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202210633831.7A Pending CN114949858A (en) | 2022-06-06 | 2022-06-06 | Collision detection method for character movement in game |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN114949858A (en) |
Cited By (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115222806A (en) * | 2022-09-20 | 2022-10-21 | 腾讯科技(深圳)有限公司 | Polygon processing method, device, equipment and computer readable storage medium |
CN116036601A (en) * | 2023-01-28 | 2023-05-02 | 腾讯科技(深圳)有限公司 | Game processing method and device, computer equipment and storage medium |
CN116562027A (en) * | 2023-05-12 | 2023-08-08 | 江苏大学 | Two-dimensional continuous collision detection method for circle and line segment set |
-
2022
- 2022-06-06 CN CN202210633831.7A patent/CN114949858A/en active Pending
Cited By (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115222806A (en) * | 2022-09-20 | 2022-10-21 | 腾讯科技(深圳)有限公司 | Polygon processing method, device, equipment and computer readable storage medium |
CN115222806B (en) * | 2022-09-20 | 2022-12-09 | 腾讯科技(深圳)有限公司 | Polygon processing method, device, equipment and computer readable storage medium |
CN116036601A (en) * | 2023-01-28 | 2023-05-02 | 腾讯科技(深圳)有限公司 | Game processing method and device, computer equipment and storage medium |
CN116562027A (en) * | 2023-05-12 | 2023-08-08 | 江苏大学 | Two-dimensional continuous collision detection method for circle and line segment set |
CN116562027B (en) * | 2023-05-12 | 2023-11-28 | 江苏大学 | Two-dimensional continuous collision detection method for circle and line segment set |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN114949858A (en) | Collision detection method for character movement in game | |
CN110487279B (en) | Path planning method based on improved A-Algorithm | |
CN111504325B (en) | Global path planning method based on weighted A-algorithm of enlarged search neighborhood | |
CN113219998B (en) | Improved bidirectional-RRT-based vehicle path planning method | |
CN107607120B (en) | Unmanned aerial vehicle dynamic track planning method based on improved restoration type Anytime sparse A algorithm | |
Fu et al. | Siamese anchor proposal network for high-speed aerial tracking | |
CN110231824B (en) | Intelligent agent path planning method based on straight line deviation method | |
CN110006429A (en) | A kind of unmanned boat path planning method based on depth optimization | |
Ho et al. | Collision-free curvature-bounded smooth path planning using composite Bezier curve based on Voronoi diagram | |
CN115079705A (en) | Routing planning method for inspection robot based on improved A star fusion DWA optimization algorithm | |
CN114161416B (en) | Robot path planning method based on potential function | |
CN112669434A (en) | Collision detection method based on grid and bounding box | |
CN112306067A (en) | Global path planning method and system | |
CN112286202A (en) | Mobile robot path planning method for non-uniform sampling FMT | |
CN114281084B (en) | Intelligent vehicle global path planning method based on improved A-algorithm | |
CN113172631A (en) | Mechanical arm autonomous obstacle avoidance method based on improved RRT algorithm | |
CN111561933A (en) | Double-improved A-star shortest route planning method | |
CN114200931A (en) | Mobile robot path smoothing method based on B-spline curve optimization | |
CN111721296A (en) | Data driving path planning method for underwater unmanned vehicle | |
CN115167398A (en) | Unmanned ship path planning method based on improved A star algorithm | |
CN115060281B (en) | Global path guide point generation planning method based on voronoi diagram | |
CN113239072A (en) | Terminal equipment positioning method and related equipment thereof | |
CN110975288B (en) | Geometric container data compression method and system based on jump point path search | |
CN112987740B (en) | Mobile robot path planning control method | |
CN114442629B (en) | Mobile robot path planning method based on image processing |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |