Use of vector ry in game programming 3

Source: Internet
Author: User

<3> 2-D border collision detection
-Twinsen compiling

-My skills are limited, and mistakes are inevitable. Please give me some advice from mathematical experts and programming experts.
-My email-Address: popyy@netease.com

I. Principle of Using vectors for Obstacle Detection

The last time we talked about Using vectors to simulate a rebound from any angle, this time we will talk about its premise-obstacle collision.

The basic idea for obstacle Collision Detection in a game is to give an obstacle range and determine whether an object will enter this range after this movement. If so, a collision will occur, otherwise, no collision occurs. In actual operations, it depends entirely on the programmer to determine whether to use the boundary of an object or other parts. At this time, a velocity vector line can be drawn from this part along the direction of velocity to determine whether there is any intersection between this line segment (from detection location to the end point of the velocity vector) and the obstacle boundary line. If yes, this intersection is the collision point.

The above object A will reach the position B after moving through the velocity vector. However, this move will not proceed smoothly, because we found that a collision occurred. The collision point is in the red area, that is, the intersection of the velocity vector and the boundary line. Our next job is to calculate the intersection. This is a process for Solving Linear Equations, so we will use the same tool...

2. A powerful tool for solving linear equations --- the Cramer Law

The first thing to note is that this rule has limitations. It must be used only when the coefficient determinant of a linear equations is non-zero. Don't be nervous. I will talk about them. First, let me introduce this rule (I will try to make you feel that this is not a math class ):

If the linear equations are:

A11 * X1 + A12 * X2 +... + A1N * xn = b1
A21 * X1 + A22 * X2 +... + a2n * xn = b2
...................................
An1 * X1 + an2 * X2 +... + Ann * xn = Bn

Coefficient matrix A =
____
| A11 A12... A1N |
| A21 A22... a2n |
| ...... |
| An1 an2... Ann |
----

| A |! = 0
Linear equations have solutions andThe solution is unique.And the solution can be expressed:

X1 = D1/d, X2 = d2/d,..., xn = DN/d (this is why/A/= D cannot be zero)

Here D is the value of the determinant/A/, DN (n = 1, 2, 3 ...) is to use the constant item b1, b2,... of the linear equations ,..., BN replaces the value of the determinant of the N column of the coefficient matrix, that is:

| B1 A12... A1N |
D1 = | B2 A22... a2n |
| ...... |
| Bn an2... Ann |

| A11 B1... A1N |
D2 = | A21 B2... a2n |
| ...... |
| An1 bn... Ann |

...

| A11 A12... b1 |
DN = | A21 A22... B2 |
| ...... |
| An1 an2... Bn |

Don't click the close window button! Let me give you an example now. Since we only talk about 2-D games for the moment (we will talk about them step by step later in 3-D), we will come to a 2-D linear equations:

(1) 4.0 * X1 + 2.0 * X2 = 5.0
(2) 3.0 * X1 + 3.0 * X2 = 6.0

Here there are two equations, two unknown numbers, according to the Cramer law above:

| 4.0 2.0 |
D = | 3.0 3.0 4.0 | = 3.0*2.0-3.0*6.0 = (for the solution of the Level 2 determinant, multiply the '/' diagonal line and then the '/' diagonal line)

| 5.0 2.0 |
D1 = | 6.0 3.0 5.0 | = 3.0*2.0-6.0*3.0 =

| 4.0 5.0 |
D2 = | 3.0 6.0 | = 4.0*6.0-5.0*3.0 = 9.0

Then

X1 = D1/D = 3.0/6.0 = 0.5
X2 = d2/D = 9.0/6.0 = 1.5

Now we get the only group of solutions for the equations.
Have you mastered the use of the Cramer law to solve the 2-D linear equations? If so, let's continue.

Iii. In-depth research

The essence of the 2-D obstacle collision detection here is to determine whether there is an intersection between two line segments. Note that it is not a straight line or a line segment. If there is an intersection between the two lines, there is also a intersection between the lines. Now we can write the equations of two line segments from the vector perspective.

Now you haveV1AndV2Two line segments are added based on the vector:

V1e=V1b+ S *V1
V2e=V2b+ T *V2

V1bAndV2bIt is the end of two line segments. S and T are two parameters in the range of [0.0, 1.0]. When S, T = 0.0,V1e=V1b,V2e=V2b; When S, T = 1.0,V1eAndV2eIt is the other end of two line segments. S, T is retrieved [0.0, 1.0]V1eAndV2eObtain each point of the two line segments.

Then we need to judgeV1AndV2If there is any intersection, letV1e=V2eCheck whether the resolved S and T are in the scope:

V1e=V2e
=>V1b+ S *V1=V2b+ T *V2
=> S *V1-T *V2=V2b-V1b
Component format:

S * x_v1-T * x_v2 = x_v2b-x_v1b
S * y_v1-T * y_v2 = y_v2b-y_v1b

Now there are two equations, two unknowns, according to the Cramer law:

| X_v1-x_v2 | 4.0-2.0 |
D = | y_v1-y_v2 | = | 1.0-3.0 | =-10.0

| X_v2b-x_v1b-x_v2 | 5.0-2.0 |
D1 = | y_v2b-y_v1b-y_v2 | = | 2.0-3.0 | =-11.0

S = D1/D =-11.0/-10.0 = 1.1> 1.0

Now S has been calculated and is not in [0.0, 1.0]. Therefore, the two line segments do not have intersections. It is intuitive to see from the graph. T there is no need to calculate it. Therefore, there is no collision between objects and obstacles. If the calculated S and T are all within [0.0, 1.0], they will be brought into the original equations and calculatedV1eOrV2eIts component is the component of the collision point.

4. Theoretically there are enough things to start writing.Program

I want to write a function to handle obstacle collision detection. to test it, I am also prepared to arrange some obstacles:

This is a convex polygon. I put a particle at the initial position (10, 8) and then gave it a random speed. The two random speeds of this random speed are within the range [1.0, 4.0, it also checks whether a collision with a boundary occurs. When a collision occurs, let it return to the initial position and return a random speed.

// First, write down the boundary coordinates of a convex polygon.
Float Poly [2] [8] = {
{6.0f, 2.0f, 4.0f, 8.0f, 14.0f, 18.0f, 14.0f, 6.0f}, // X components of all vertices. The last vertex and the first vertex coincide.
{2.0f, 6.0f, 10.0f, 14.0f, 12.0f, 8.0f, 4.0f, 2.0f} // y component of all vertices
};
// Define some variables
Float X, Y; // This is the position variable of the particle.
Float VX, Vy; // The velocity vector component of the particle.

// Well, start writing the Collision Detection Function
Bool collisiontest () {// return true when a collision occurs; otherwise, return false

Float S, T; // two parameters of the Line Segment Equation
// Parameters
Float x_v1, x_v2, y_v1, y_v2;
Float x_v2b, x_v1b, y_v2b, y_v1b;

For (INT I = 0; I <8-1; ++ I) {// loop to the second to last point

// Obstacle Line Segment
X_v1 = Poly [0] [I + 1]-Poly [0] [I];
Y_v1 = Poly [1] [I + 1]-Poly [1] [I];
// Object velocity vector
X_v2 = VX;
Y_v2 = Vy;
// The initial point of the obstacle Vector
X_v1b = Poly [0] [I];
Y_v1b = Poly [1] [I];
// Object Location
X_v2b = X;
Y_v2b = y;
// Calculate d, D1, and D2
// | X_v1-x_v2 |
// D = | y_v1-y_v2 |
// X_v2b-x_v1b-x_v2 |
// D1 = | y_v2b-y_v1b-y_v2 |
// | X_v1 the x_v2b-x_v1b |
// D2 = | y_v1 y_v2b-y_v1b |

D = (x_v1 * (-y_v2)-(-x_v2) * y_v1 );
D1 = (x_v2b-x_v1b) * (-y_v2)-(-x_v2) * (y_v2b-y_v1b ));
D2 = (x_v1 * (y_v2b-y_v1b)-(x_v2b-x_v1b) * y_v1 );

// Determine whether D is zero
If (ABS (d) <0.001f) // if it is equal to zero, the ABS () is used to obtain the absolute value.
D = 0.001f;

// Calculated parameter S, T
S = D1/d;
T = d2/d;
// Determine whether a collision has occurred
// Returns true if any occurrence occurs.
If (0.0f <= S & 1.0f> = S & 0.0f <= T & 1.0f> = T)
Return true;

} // For (INT I = 0; I <8-1; ++ I)

// If no collision occurs, false is returned.
Return false;

} // End of Function

// Test the function now
// Initialize the particle
X = 10.0f, y = 8.0f;
VX = Vy = (float) (RAND () % 4 + 1 );

// Enter the main loop
// Suppose it is already in the Main Loop
If (collisiontest () {// if an object collides with a particle
X = 10.0f, y = 8.0f;
VX = Vy = (float) (RAND () % 4 + 1 );
}
// Particle movement
X + = VX;
Y + = Vy;

Now you can combine the previous discussion to simulate a complete Ideal Physical scenario: an object moving and rebounding in an irregular obstacle will never stop...

So far, we have discussed the barrier collision detection and programming implementation of the 2-D game. In this process, we have learned about linear algebra, in the future, we will continue to add more mathematical and physical knowledge as we go deeper. Next time we will continue to discuss, bye!

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 376941

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.