Skip to content Skip to sidebar Skip to footer

Vector Math For 3d Computer Graphics Download UPDATED

Vector Math For 3d Computer Graphics Download

Vector math¶

Introduction¶

This tutorial is a short and applied introduction to linear algebra as it applies to game development. Linear algebra is the report of vectors and their uses. Vectors have many applications in both 2D and 3D development and Godot uses them extensively. Developing a good agreement of vector math is essential to becoming a stiff game developer.

Coordinate systems (2nd)¶

In second space, coordinates are divers using a horizontal axis ( ten ) and a vertical axis ( y ). A particular position in 2d infinite is written every bit a pair of values such as (4, iii) .

../../_images/vector_axis1.png

Notation

If yous're new to computer graphics, it might seem odd that the positive y axis points downwards instead of upwards, equally you lot probably learned in math grade. Withal, this is common in most computer graphics applications.

Any position in the 2D plane tin can be identified by a pair of numbers in this way. However, we tin can also recollect of the position (4, 3) as an first from the (0, 0) point, or origin. Draw an arrow pointing from the origin to the point:

../../_images/vector_xy1.png

This is a vector. A vector represents a lot of useful data. As well every bit telling the states that the point is at (4, three) , nosotros tin can also think of it as an angle θ and a length (or magnitude) chiliad . In this case, the arrow is a position vector - it denotes a position in space, relative to the origin.

A very important point to consider nearly vectors is that they only represent relative direction and magnitude. There is no concept of a vector'south position. The following 2 vectors are identical:

../../_images/vector_xy2.png

Both vectors represent a indicate 4 units to the right and three units below some starting betoken. It does not matter where on the plane yous draw the vector, it always represents a relative management and magnitude.

Vector operations¶

Y'all can use either method (x and y coordinates or angle and magnitude) to refer to a vector, but for convenience, programmers typically use the coordinate notation. For case, in Godot, the origin is the superlative-left corner of the screen, so to place a 2D node named Node2D 400 pixels to the correct and 300 pixels down, use the post-obit code:

                                    $                  Node2D                  .                  position                  =                  Vector2                  (                  400                  ,                  300                  )                

Godot supports both Vector2 and Vector3 for 2nd and 3D usage, respectively. The same mathematical rules discussed in this article apply to both types.

Fellow member admission¶

The individual components of the vector can be accessed straight by name.

                                        # create a vector with coordinates (2, 5)                    var                    a                    =                    Vector2                    (                    ii                    ,                    5                    )                    # create a vector and assign x and y manually                    var                    b                    =                    Vector2                    ()                    b                    .                    x                    =                    three                    b                    .                    y                    =                    1                  

Adding vectors¶

When calculation or subtracting two vectors, the corresponding components are added:

                                        var                    c                    =                    a                    +                    b                    # (2, five) + (3, 1) = (5, 6)                  

We can also see this visually by adding the second vector at the end of the first:

../../_images/vector_add1.png

Note that adding a + b gives the aforementioned upshot every bit b + a .

Scalar multiplication¶

Note

Vectors correspond both direction and magnitude. A value representing simply magnitude is called a scalar.

A vector can be multiplied by a scalar:

                                        var                    c                    =                    a                    *                    2                    # (2, v) * 2 = (4, x)                    var                    d                    =                    b                    /                    3                    # (three, 6) / 3 = (one, two)                  

../../_images/vector_mult1.png

Note

Multiplying a vector by a scalar does non change its direction, simply its magnitude. This is how you scale a vector.

Practical applications¶

Let's await at two common uses for vector addition and subtraction.

Motility¶

A vector can represent any quantity with a magnitude and direction. Typical examples are: position, velocity, acceleration, and forcefulness. In this image, the spaceship at step i has a position vector of (one,3) and a velocity vector of (2,1) . The velocity vector represents how far the transport moves each pace. We can discover the position for step 2 past calculation the velocity to the electric current position.

../../_images/vector_movement1.png

Tip

Velocity measures the change in position per unit of fourth dimension. The new position is establish by adding velocity to the previous position.

Pointing toward a target¶

In this scenario, you have a tank that wishes to point its turret at a robot. Subtracting the tank'southward position from the robot'southward position gives the vector pointing from the tank to the robot.

../../_images/vector_subtract2.png

Tip

To notice a vector pointing from A to B use B - A .

Unit vectors¶

A vector with magnitude of 1 is called a unit vector. They are as well sometimes referred to as direction vectors or normals. Unit vectors are helpful when you demand to keep track of a direction.

Normalization¶

Normalizing a vector means reducing its length to one while preserving its direction. This is washed by dividing each of its components by its magnitude. Because this is such a common performance, Vector2 and Vector3 provide a method for normalizing:

Warning

Because normalization involves dividing past the vector's length, you cannot normalize a vector of length 0 . Attempting to do so would normally result in an fault. In GDScript though, trying to telephone call the normalized() method on a Vector2 or Vector3 of length 0 leaves the value untouched and avoids the error for you.

Reflection¶

A common use of unit of measurement vectors is to indicate normals. Normal vectors are unit vectors aligned perpendicularly to a surface, defining its direction. They are commonly used for lighting, collisions, and other operations involving surfaces.

For example, imagine we take a moving ball that we desire to bounciness off a wall or other object:

../../_images/vector_reflect1.png

The surface normal has a value of (0, -1) because this is a horizontal surface. When the ball collides, we take its remaining motility (the corporeality left over when it hits the surface) and reflect it using the normal. In Godot, the Vector2 grade has a bounce() method to handle this. Here is a GDScript example of the diagram in a higher place using a KinematicBody2D:

                                        # object "collision" contains information about the collision                    var                    collision                    =                    move_and_collide                    (                    velocity                    *                    delta                    )                    if                    standoff                    :                    var                    reflect                    =                    standoff                    .                    remainder                    .                    bounce                    (                    collision                    .                    normal                    )                    velocity                    =                    velocity                    .                    bounce                    (                    collision                    .                    normal                    )                    move_and_collide                    (                    reflect                    )                  

Dot product¶

The dot production is 1 of the about important concepts in vector math, but is often misunderstood. Dot production is an operation on two vectors that returns a scalar. Unlike a vector, which contains both magnitude and direction, a scalar value has only magnitude.

The formula for dot product takes two common forms:

../../_images/vector_dot1.png

and

../../_images/vector_dot2.png

All the same, in most cases it is easiest to employ the built-in method. Notation that the lodge of the two vectors does not matter:

                                    var                  c                  =                  a                  .                  dot                  (                  b                  )                  var                  d                  =                  b                  .                  dot                  (                  a                  )                  # These are equivalent.                

The dot product is nigh useful when used with unit of measurement vectors, making the showtime formula reduce to only cosθ . This ways we can utilize the dot product to tell united states of america something most the angle betwixt ii vectors:

../../_images/vector_dot3.png

When using unit of measurement vectors, the outcome will always be between -1 (180°) and i (0°).

Facing¶

We tin utilize this fact to detect whether an object is facing toward another object. In the diagram below, the player P is trying to avoid the zombies A and B . Assuming a zombie's field of view is 180°, tin can they see the player?

../../_images/vector_facing2.png

The green arrows fA and fB are unit of measurement vectors representing the zombies' facing directions and the blue semicircle represents its field of view. For zombie A , we find the direction vector AP pointing to the actor using P - A and normalize it, however, Godot has a helper method to do this called direction_to . If the bending between this vector and the facing vector is less than 90°, and then the zombie can see the role player.

In code it would look like this:

                                        var                    AP                    =                    A                    .                    direction_to                    (                    P                    )                    if                    AP                    .                    dot                    (                    fA                    )                    >                    0                    :                    impress                    (                    "A sees P!"                    )                  

Cross product¶

Similar the dot production, the cantankerous production is an performance on two vectors. Notwithstanding, the issue of the cross production is a vector with a management that is perpendicular to both. Its magnitude depends on their relative bending. If two vectors are parallel, the result of their cross production volition exist a nada vector.

../../_images/vector_cross1.png ../../_images/vector_cross2.png

The cantankerous production is calculated similar this:

                                    var                  c                  =                  Vector3                  ()                  c                  .                  x                  =                  (                  a                  .                  y                  *                  b                  .                  z                  )                  -                  (                  a                  .                  z                  *                  b                  .                  y                  )                  c                  .                  y                  =                  (                  a                  .                  z                  *                  b                  .                  ten                  )                  -                  (                  a                  .                  x                  *                  b                  .                  z                  )                  c                  .                  z                  =                  (                  a                  .                  x                  *                  b                  .                  y                  )                  -                  (                  a                  .                  y                  *                  b                  .                  10                  )                

With Godot, you tin use the built-in method:

Note

In the cross product, lodge matters. a.cross(b) does not requite the same result as b.cantankerous(a) . The resulting vectors point in reverse directions.

Calculating normals¶

One common employ of cross products is to notice the surface normal of a plane or surface in 3D space. If we have the triangle ABC we can use vector subtraction to discover two edges AB and AC . Using the cross product, AB x Air conditioning produces a vector perpendicular to both: the surface normal.

Hither is a function to calculate a triangle's normal:

                                        func                    get_triangle_normal                    (                    a                    ,                    b                    ,                    c                    ):                    # find the surface normal given three vertices                    var                    side1                    =                    b                    -                    a                    var                    side2                    =                    c                    -                    a                    var                    normal                    =                    side1                    .                    cross                    (                    side2                    )                    return                    normal                  

Pointing to a target¶

In the dot product section above, we saw how it could be used to detect the angle between two vectors. Even so, in 3D, this is not enough data. Nosotros also demand to know what centrality to rotate around. We tin can discover that by computing the cantankerous product of the current facing direction and the target direction. The resulting perpendicular vector is the axis of rotation.

DOWNLOAD HERE

Posted by: monroetatem1998.blogspot.com

Post a Comment for "Vector Math For 3d Computer Graphics Download UPDATED"