Hot answers tagged

51

Generally speaking, a Normal vector represents the direction pointing directly "out" from a surface, meaning it is orthogonal (at 90 degree angles to) any vector which is coplanar with (in the case of a flat surface) or tangent to (in the case of a non-flat surface) the surface at a given point. A Tangent vector is typically regarded as one vector that ...


49

To get the 2D vector perpendicular to another 2D vector simply swap the X and Y components, negating the new Y component. So { x, y } becomes { y, -x }.


39

First of all, it's way less clutter. If you have a position, a velocity and an acceleration, that's already 6 variables you have to deal with, 9 in 3d. Secondly, and this is the most important part, it grants you access to many ways to use or change them. For instance, getting the length of the vector, normalizing it, adding them together, dot product, ...


36

Does that mean that the magnitude is simply it is distance from the origin point (0, 0, 0)? That's exactly that. Among other things, a vector can represent a point (a position), a direction and/or a velocity, depending on the context. If you have this variable: Vector3 mPosition; It generally represents only the position, i.e. where it is located in ...


32

The answer is actually pretty easy if you do the math. You have a fixed distance of Y and a variable distance of X (See Picture 1). You need to find out the angle between Z and X and turn your turret that much more. Step 1 - Get distance between the turret line (V) and the gun line (W) which is Y (this is constant but doesn't hurt to calculate). Get ...


29

Don't let a math major hear you calling Vectors points or coordinates! A 2D vector has an x and y component, not coordinate. Vectors do not define a position, they define a direction and a magnitude. I can't tell you why people are intimidated by them, likely the same reason people are intimidated by math in general, because everyone says it's hard before ...


28

You could easily find the normal by calculating two vectors, V1 = P2-P1, and V2 = P3-P1, and then find the cross product N = V1 x V2. Then you normalize N. Depending on the ordering of the vertices (clockwise or counterclockwise) you will get a normal facing front or back. You also need to make sure that three three points aren't aligned, if they are you ...


27

Defining >= for a Vector3 type makes no sense. What determines if one vector is greater than another? Their magnitude or their individual x, y, z components? A vector is a magnitude & a direction. So what determines what direction is greater? If you need to compare the magnitudes you can use sqrMagnitude. In this case Vector3 overrides == to ...


26

The simplest way is probably to get the angle of the vector using atan2(), as Tetrad suggests in the comments, and then scale and round it, e.g. (pseudocode): // enumerated counterclockwise, starting from east = 0: enum compassDir { E = 0, NE = 1, N = 2, NW = 3, W = 4, SW = 5, S = 6, SE = 7 }; // for string conversion, if you can't just do ...


25

dot(A,B) = |A| * |B| * cos(angle) which can be rearranged to angle = arccos(dot(A,B) / (|A|* |B|)). With this formula, you can find the smallest angle between the two vectors, which will be between 0 and 180 degrees. If you need it between 0 and 360 degrees this question may help you. By the way, the angle between two parallel vectors pointing in the same ...


24

Yes, you can simplify this. First, stop calling them vectors. They are points. Let’s call them A, B and C. So, you want this: dist(A, B) < dist(A, C) Replace distances with distances squared, then with dot products (from the definition of the Euclidean length. Replace AC with AB + BC (now these are real vectors). Expand, simplify, factor: dist(A, B)² &...


20

Shoot a ray from the camera through the center/reticle into the world. Find out where in the world it hits. Fire the bullet from the gun's muzzle at that point instead of straight out of the gun. Bonus points for animating the hands and gun to point in that direction while aiming around so the bullet still looks like it's firing straight out of the muzzle ...


20

I always forget how to do this when I need it so I wrote a couple of extension methods. public static Vector2 PerpendicularClockwise(this Vector2 vector2) { return new Vector2(vector2.Y, -vector2.X); } public static Vector2 PerpendicularCounterClockwise(this Vector2 vector2) { return new Vector2(-vector2.Y, vector2.X); ...


20

Does that mean that the magnitude is simply it is distance from the origin point (0, 0, 0)? The tl;dr answer may be: Yes, you can imagine it like that. But I'm not sure whether this might not lead to a wrong understanding. A vector is not a point, and there is a crucial difference between the two! The fact that a vector is usually represented as an "...


17

Normal vectors are typically used for lighting calculations. It is a vector that is supposed to be perpendicular to the surface that is approximated by the vertices of a mesh. Normals are defined at each vertice position but can be calculated differently depending on how you want light to refect at that vertice or what you want to do with your light ...


17

The reason for this is Pythagorean Theorem, and it's probably the bit of math I use most often in games. Even when working with a full-featured engine, there are times when knowing this math has helped me get the gameplay and look I wanted. Don't worry though, it's very simple once you've used it a few times. :) What this bit of code does is ensure that ...


17

The dot product of two vectors can tell you if they face each other or not. First vector can probably be the enemies view direction the second one should be a vector pointing from player's position to the enemies position. https://www.youtube.com/watch?v=Q9FZllr6-wY


16

Yes. Assuming your distance function uses a square root, you can simplify this by removing the square root. When trying to find the larger (or smaller) of a distance, x^2 > y^2 still holds true for x > y. However, further attempts to simplify the equation mathematically are likely pointless. The distance between vector1 and vector2 is not the same ...


16

To simplify the answer, Vector3 is a custom struct provided by the UnityEngine namespace. When we create custom class or struct types, we must also define its operators. As such, there is no default logic for the >= operator. As pointed out by Evgeny Vasilyev, _rect_tfm.position == _positionB makes sense, as we can directly check the Vector3.x, Vector3.y ...


15

Compare the function signatures of both RotatePoints versions. Lone variables: void RotatePoints( float *out_x, int x_interleave_out, const float *in_x, int x_interleave_in, float *out_y, int y_interleave_out, const float *in_y, int y_interleave_in, float angle, int count ) { float s = sinf(angle); float c = cosf(...


14

A texture mapping is the mapping between points on the 3D surface and their corresponding points on a texture image. If you have a 1:1 texture mapping, then every point on the 3D surface maps to a specific and unique point in the texture image (though the reverse would not need to be true. Some locations in the texture would not necessarily map to locations ...


12

A Unit Vector is of length 1. A given vector can be converted to a unit vector by dividing it by it's magnitude. (With the exception of course that a zero length vector can not be converted). Note that magnitude can be calculated using the Pythagorean theorem For example if a vector has components: (x, y, z) magnitude = sqrt( x2+ y2+ z2) unit vector =...


12

Others have pointed out how you can use the sign of the dot product to broadly determine the angle between two arbitrary vectors (positive: < 90, zero: = 90, negative: > 90), but there's another useful geometric interpretation if at least one of the vectors is of length 1. If you have one unit vector \$\hat U\$ and one arbitrary vector \$V\$, you can ...


12

Here's a vector-based solution. I haven't tried it, but it seems fine conceptually. Theory I gather you've stored the shape as line segments. Here's the letter A represented with three line segments. I've assumed that paths in the user's drawing are stored as lists of points. We can "inflate" those line segments to allow an error margin when checking for ...


11

First we get a vector from B to A, as in the following picture: Now we have a vector that tells us how to get exactly from B to A. In code it looks something along these lines: var BtoA = A - B; In the above image's case, the resulting vector is X:15 Y:-20. At this point we actually already know the direction, as it is included in the information of how ...


11

Readability > Writeability I feel like it's just slow me down and barely has any benefit other than organizing your code. You are correct in that it (slightly) slows you down writing that code. However, you write it once in the beginning and from then on everytime you come back you are going to read it. So optimizing the reading speed will do much more ...


10

Taking this picture: AB is the red vector from A to B. Say P is 25% of the way from A to B. The basic way to get to P from the origin is A + ( B - A ) / 4 = 3/4 A + B / 4 So 3/4 A and 1/4 B. Another way to find that is saying you want a vector 75% "close" to A, and 25% "close" to B. (A vector that is 100% "close" to A is just the A vector.) So you ...


10

You have 8 options (or 16 or more if you want even finer precision). Use atan2(y,x) to get the angle for your vector. atan2() works in the following way: So x=1, y=0 will result in 0, and it's discontinuous at x=-1, y=0, containing both π and -π. Now we just need to map the output of atan2() to match that of the compass we have above. Likely the simplest ...


10

The difference between the tangent and the binormal is less immediately clear on surfaces, but that shouldn't be too surprising - the binormal was originally defined not for surfaces but for curves, where the concept makes a lot more sense (and where it really lives as a 'normal' in that it's orthogonal to the direction of movement, thus the name). To be ...


Only top voted, non community-wiki answers of a minimum length are eligible