How to use Vector2 in XNA


In this article I will be talking about Vector2

Vector is the one of the most used struct especially in 2D based games in XNA. First of all lets take a look at its structure:

Add Two Vectors:

1.gif
 
Add Two Reference Vectors:

2.gif
 
Using BaryCentric with 3 Vectors and 2 floats:

3.gif
 
Using BaryCentric with 3 Reference Vectors and 2 floats:

4.gif
 
What is BaryMetric?

In geometry, the barycentric coordinate system is a coordinate system in which the location of a point is specified as the center of mass, or barycenter, of masses placed at the vertices of a simplex (a triangle, tetrahedron, etc). Barycentric coordinates are a form of homogeneous coordinates.

Generalized barycentric coordinates have applications in computer graphics and more specifically in geometric modelling. Often, a three-dimensional model can be approximated by a polyhedron such that the generalized barycentric coordinates with respect to that polyhedron have a geometric meaning. In this way, the processing of the model can be simplified by using these meaningful coordinates.

Using CatmullRom with 4 Vectors and a float:

5.gif
 
Using CatmullRom with 4 Reference Vectors,a float and an out result:

6.gif
 
What is Catmull-Rom?

A Catmull-Rom spline is obtained, being a special case of a cardinal spline.

The curve is named after Edwin Catmull and Raphael (Raphie) Rom. In computer graphics, Catmull-Rom splines are frequently used to get smooth interpolated motion between key frames. For example, most camera path animations generated from discrete key-frames are handled using Catmull-Rom splines. They are popular mainly for being relatively easy to compute, guaranteeing that each key frame position will be hit exactly, and also guaranteeing that the tangents of the generated curve are continuous over multiple segments.

Using Clamp with 3 Vectors:

7.gif
 
Using Clamp with 3 Reference Vectors:

8.gif
 
Using Distance with 2 Vectors to calculate distances between them:

9.gif
 
Using Distance with 2 Reference Vectors to calculate distances between them:

10.gif
 
Using DistanceSquared to calculate distances between two vectors squared:

11.gif
 
Using DistanceSquared to calculate distances between two vectors squared:

12.gif
 
Using Divide to divide a Vector:

13.gif
 
Using Divide to divide 2 vectors:

14.gif
 
Using Divide to divide a reference Vector:

15.gif
 
Using Divide to divide 2 reference vectors:

16.gif 

Using Dot to calculate dot product with 2 vectors:

17.gif
 
Using Dot to calculate dot product with 2 reference vectors:

18.gif
 
Using Hermite with 4-Vectors and a float:

19.gif
 
Using Hermite with 4- Reference Vectors and a float:

20.gif
 
Using Lerp to perform a linear interpolation between 2 vectors using an amount:

21.gif
 
Using Lerp to perform a linear interpolation between 2 reference vectors using an amount:

22.gif
 
Using Max to get the highest value between 2 vectors:

23.gif
 
Using Max to get the highest value between 2 reference vectors:

24.gif
 
Using Min to get the lowest value between 2 vectors:

25.gif
 
Using Min to get the lowest value between 2 reference vectors:

26.gif
 
Using Multiply to multiply a vector with a float:

27.gif
 
Using Multiply to multiply 2 vectors:

28.gif
 
Using Multiply to multiply 1 vector with a float:

29.gif
 
Using Multiply to multiply 2 reference vectors:

30.gif
 
Using Negate to change the direction of the Vector:

31.gif
 
Using Negate to change the direction of the Vector reference:

32.gif
 
Using Normalize to set a unit vector from the original vector:

33.gif
 
Using Normalize to set a unit vector from the original vector reference:

34.gif
 
Set both vector components to 1:

35.gif
 
Reflect a vector using 2 vectors:

36.gif
 
Reflect a vector using 2 vectors reference:

37.gif
 
Using SmoothStep to interpolate cubicly between 2 vectors with an amount:

38.gif
 
Using SmoothStep to interpolate cubicly between 2 vectors reference with an amount:

39.gif

40.gif 
41.gif
42.gif
              
Return X axis of Unit Vector:

43.gif
 
Return Y axis of Unit Vector:

44.gif
 
Set all the components in a vector to 0:

45.gif 

Create a new Vector object(null):

46.gif
 
Create a new Vector with a given float:

47.gif
 
Create a new Vector with 2 float values:

48.gif

Now that we know the structure we wil be giving code samples how to use them:

//Variables
Vector2 val1 = new Vector2(10.0f, 20.0f);
Vector2 val2 = new Vector2(30.0f, 20.0f);
Vector2 val3=new Vector2(40.0f,20.0f);
Vector2 val4 = new Vector2(60.0f, 60.0f);
Vector2 onevector, unitxvector, unityvector, zerovector;
Matrix transform=new Matrix(5.0f,2.0f,3.0f,4.0f,4.0f,4.0f,4.0f,1.0f,2.0f,3.0f,1.0f,5.0f,3.0f,4.0f,2.0f,1.0f);                     

//Using Vector2
Vector2.Add(val1, val2);Vector2.Barycentric(val1, val2, val3, 10.0f, 20.0f);
Vector2.CatmullRom(val1, val2, val3, val4, 10.0f);
Vector2.Clamp(val1, val2, val4);
Vector2.Distance(val1, val4);
Vector2.DistanceSquared(val2, val3);
Vector2.Divide(val4, val1);
Vector2.Dot(val4, val1);
Vector2.Hermite(val4, val1, val2, val3, 1.0f);
Vector2.Lerp(val1, val4, 5.0f);
Vector2.Max(val1, val4);
Vector2.Min(val2, val3);
Vector2.Multiply(val1, val2);
Vector2.Negate(val4);
Vector2.Normalize(val3);
Vector2.Reflect(val4, val1);
Vector2.SmoothStep(val4, val3, 3.0f);
Vector2.Subtract(val4, val2);
Vector2.Transform(val4, transform);
Vector2.TransformNormal(val1, transform);
onevector = Vector2.One;
unitxvector = Vector2.UnitX;
unityvector = Vector2.UnitY;
zerovector = Vector2.Zero;

You can use Vector2 for transformations,positions and vector2 processing.


Similar Articles