[Zheng Qun original] I studied terrain collision detection method

xiaoxiao2021-03-05  23

First of all, the topographic vertices we have to generate should be N * M top points in X, Z plane into rectangular (recommended square) rules

Column, the X, Z coordinate is regular (for simple period x, z all in the positive half), and the Y value is different, Y value performance

It is the high and low terrain. (Y value can calculate it, such as random, or use a height map, after recommending

By. )

The Y value is then recorded with a 2-dimensional number of MAPDATA [N] [M] of N * M.

The key to calculating the human height is here. First, the three points Y value of the triangle where the character is saved according to the character coordinates.

Array index, this can be very simple, as shown. Of course, it is very simple to get the rectangle of the character, but I

What we have to get is the triangle of the characters. At this time, it has to be judged. This is a very simple way, that is just now.

X% Rect_Width Y% Rect_long <= (Rect_Width Rect_long) / 2 (provided as long-width is an integer to use%, otherwise, honest PX-P1.x, PY-P1.Y is done) In the triangle on the left, otherwise

In the triangle on the right.

The Y value of the three vertices was obtained, and the Y value of the character was obtained, which has a formula P.y = p1.y (p2.y-p1.y) * u (P3.Y-P1.

Y) * V, u, v is the center of gravity coordinates, because we are the right triangle, so you can get it very simple, u = z% Rect_long / Rect_long, v = x% Rect_width / Rect_Width. How? This method is very good to test the collision detection of the topography, and the high level is very accurate.

. Try it out :)

FLOAT CTERRAIN :: GetHeightwithXZ (FLOAT X, FLOAT Z) / / Depending on the map planar coordinates

{

INT IIndexx = X / MAP_RECT_WIDTH;

INT IIndexz = z / map_rect_long; // These two data are used to determine the position of the height information in the array, that is, the array subscript

Float U, V;

Float T0, T1, T2;

FLOAT FOFFSETZ = 0.0F;

FLOAT FOFFSETX = 0.0F;

FOFFSETX = X- (FLOAT) IINDEXX * MAP_RECT_WIDTH;

FOFFSETZ = z- (float) ingdexz * map_rect_long; // The offset coordinate of this coordinate in this triangle

IF (FOFFSETX FOFFSETZ> (MAP_RECT_LONG MAP_RECT_WIDTH / 2.0F) // Judging that the point in the square side

{

u = 1- (foffsetx) / map_rect_width;

v = 1- (foffsetz) / map_rect_long; // center of gravity coordinates

T0 = ​​mapdata [IIndexz 1] [IINDEXX 1],

T1 = MAPDATA [IINDEXZ] [IINDEXX 1],

T2 = MAPDATA [IINDEXZ 1] [IIndexx]; / / Determine the height of three vertices in the triangle of the coordinate

RETURN T0 U * (T2-T0) V * (T1-T0); // Using the center of gravity coordinates to obtain a highly interpolated

}

Else

{

u = (foffsetx) / map_rect_width;

v = (foffsetz) / map_rect_long;

T0 = ​​Mapdata [IIndexz] [IINDexx],

T1 = mapdata [IIndexz] [IIndexx 1], T2 = MapData [IINDEXZ 1] [IINDexx];

RETURN T0 V * (T2-T0) U * (T1-T0);

}

Original geology: http://bbs.gamedev.9cbs.net/showthread.php? T = 10294

转载请注明原文地址:https://www.9cbs.com/read-35017.html

New Post(0)