Use of vector ry in game programming 1

Source: Internet
Author: User

<1> simple 2-D Tracing
-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

Andre Lamothe said: "vector ry is the best friend of game programmers ". It is not false. Vector RY's position in game programming cannot be ignored, because in the eyes of game programmers, the display screen is a coordinate system, and the trajectory of the moving object is the result of the curve motion of the object in this coordinate system, what describes the motion of these curves is a vector. Vector can be used to simulate physical phenomena and basic AI.

Now, let's take a look at the middle school knowledge..

Vector v(Using a bold letter to represent a vector) is also calledVectorIt is a volume with the size and direction. A vector with a length of 1 is calledUnit Vector, Also calledYaoya, Which is recordedE. A vector with a length of 0 is calledZero vector,0The zero vector has no definite direction. In other words, its direction is arbitrary.

I. Basic operations on Vectors


1. Vector Addition:+BEqualBStarting point andAWhen the end pointAStart PointBThe end point is the end point vector.

2. Vector Subtraction: A-BEqualBStarting point andAWhen the start pointBThe end pointAThe end point is the end point vector.

3. number multiplication vector: K *A, K> 0, equalAThe length is K times larger. When k is 0, it is equal to 0 vector. When k is <0, it is equalALength of | K | times and then returns the reverse.

4. Inner Product of a vector (quantity product and dot product ):A. B= |A| * |B| * Cosa equals VectorAMultiplied by the lengthBLengthAAndBThe cosine of the angle.
Its geometric meaning isALength andBInAThe product of the projection length on, orBLength andAInBThe product of the projection length. It is a scalar, and
And can be positive or negative. Therefore, the inner product of the vector perpendicular to each other is 0.

5. Vector Product (Cross Product ):AXB= |A| * |B| * Sina *V=C, |A| YesALength, |B| YesBLength, A isAAndBThe angle between them is not greater than 180,VYes andA,BThe vertical vector of the plane, that isAXBAndA,BAll vertical. In the right-hand coordinate system,A,B,CConstitute the right hand system, that is, the right thumb straighten, the other four fingersAToBThe angle curl is not greater than 180 degrees, and the direction indicated by the thumb isC. ThereforeAXB! =BXA. If it is left-handAXB=-C, That isA,BAnd-CConstitute the left-hand system.AXBThe formula for calculating the determinant of is shown on the right. The vector product of two vectors is a vector.

6. Inner Product of orthogonal vectors: The two vertical vectors are orthogonal, and the inner product of orthogonal vectors is zero.A. B= |A|. |B| * Cos (PI/2) = |A|. |B| * 0 = 0.

Ii. Vector Properties

Without the following properties, we cannot deduce the vector technique.

1)A+B=B+A
2 )(A+B) +C=A+ (B+C)
3)A+0=0+A=A
4)A+ (-) = 0
5) K * (L *A) = (K * l )*A=A* (K * l)
6) K *(A+B) = K *A+ K *B
7) (K + l )*A= K *A+ L *A
8) 1 *A=A

9)A. B=B.
10)A.(B+C) =A. B+A. c
11) K *(A. B) = (K *A). B=A.(K *B)
12)0.=0
13)A.= |A| ^ 2


3. Algebra (Component) Representation of free Vectors

1. Algebraic representation of vectors in Cartesian coordinates:

A= (X, y)

X and Y are the components of the vector on the X and Y axes respectively. Any vector with the component (x, y) on the Cartesian axis is equal. For example, each vector in is expressed as (-2, 1 ).

Or write itA= X *I+ Y *J,That isIAndJLinear Combination, hereIIs the unit vector (1, 0) in the X axis direction ),JIs the unit vector (0, 1) in the Y axis, soIOrthogonalJ. Any 2-D vector can be expressedIAndJLinear Combination.

|I| = |J| = 1

2. Calculation of vector algebra (Component) representation:

Vector Addition component representation:A+B= (XA, ya) + (XB, Yb) = (xa + XB, ya + Yb)

Vector subtraction component representation:A-B= (XA, ya)-(XB, Yb) = (xA-xB, ya-Yb)

Vector Inner Product (quantity product, dot product) component representation:

A. B
= (Xa *I+ YA *J). (XB *I+ Yb *J)
= XA *I* XB *I+ XA *I* Yb *J+ YA *J* XB *I+ YA *J* Yb *J
= (Xa * xb )*(I*I) + (Xa * Yb )*(I*J) + (XB * ya )*(I*J) + (Ya * Yb )*(J*J)
= XA * XB + YA * Yb


3. Calculation of vector length (modulus) and unitization (normalization ):

SetA= (X, y), then
|A| = | (X, y) | = | x *I+ Y *J| = SQRT (x ^ 2 *I^ 2 + y ^ 2 *J^ 2) = SQRT (x ^ 2 + y ^ 2). Here SQRT is the square symbol.

AThe unit vector of isA/|A|, That is, (x, y)/SQRT (x ^ 2 + y ^ 2 ).

Iv. simple 2-D tracking

Now, with the basic vector knowledge, we can analyze a common problem-tracking from one point on the screen to another. In fact, this problem can also be understood as a line-drawing problem, there are many online draw algorithms: DDA, midpoint, and bresenham. However, these algorithms are generally used to draw fixed line segments at both ends, and it is not flexible to track dynamic points and points. The vector method can effectively solve such problems.

Now, if you are writing a flying shooting game, your enemy needs a very powerful weapon-tracking missiles, this type of weapon constantly fixes the positional relationship between itself and the target while traveling, so that it always points to the player, regardless of where the player is located, this is a low-level player (Me ?) It may be a disaster tolerance, and players may be surprised that the enemy will have such an advanced secret weapon. But for you, you only need to add several lines of code to the program loop.
They work in units and basic vector operations.

First, we need to know the player location (x_player, y_player). Then, our missile can obtain an initial speed through calculation, the speed direction is constantly corrected based on the player's position. Its essence is the calculation process of vector subtraction. We can set the speed by ourselves. It can be fast and slow, depending on the difficulty of the game. Its essence is the process of vector unitization and multiplication vector. The specific algorithm is: Missile update speed (vx_missile, vy_missile) = player location (x_player, y_player)-missile location (x_missile, y_missile), and then (vx_missile, vy_missile) scale down, move missiles, determine whether to catch players, reupdate the speed, and narrow down...

Let's take a look at the code of this simple algorithm:

// Assume x_player, and y_player is the player position component.
// X_missile, y_missile is the missile position component
// Xv_missile, yv_missile is the missile speed component
// Let's get started!

Float n_missile; // The length of the vector between the player position and the missile position
Float v_rate; // This is the missile's rate scaling ratio.

// Calculate the position vector between the player and the missile
Xv_missile = x_player-x_missile; // vector subtraction, directed by missiles to players, X components
Yv_missile = y_player-y_missile; // y component

// Calculate its length
N_missile = SQRT (xv_missile * xv_missile + yv_missile * yv_missile );

// Normalized missile velocity vector:
Xv_missile/= n_missile;
Yv_missile/= n_missile;

// At this time, the missile speed is 1. Note that the speed is used here.
// The missile's velocity component meets xv_missile ^ 2 + yv_missile ^ 2 = 1
// Good! Now the missile's speed direction has been corrected, and it points to players.
// Because the current missile speed is too fast, I want to slow down the missile to ease the tension.
V_rate = 0.2f; // deceleration rate
Xv_missile * = v_rate; // The rate scaling ratio here. You can adjust the size at will.
Yv_missile * = v_rate; // acceleration: v_rate is greater than 1; deceleration v_rate is greater than 0 and less than 1. This is what we do here!

// Missile progress! The missile bravely rushed to the players!
X_missile + = xv_missile;
Y_missile + = yv_missile;

// Then determine whether the attack is successful

Now, your enemies can use tracking missiles to Attack Players. You can also change it to a straight line attack weapon. This is common.
The Basic tracking effect can be simulated using vectors.

At this point, we only use a small part of the vector knowledge. Other knowledge will be used in the game. This is the first introduction here.
Next time I will talk about how to use a vector to simulate a 2-D object at any angle to return to the bullet. :)! Don't forget to review basic vector knowledge. We will use them.

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

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.