Hot answers tagged

45

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 }.


44

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 ...


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 ...


31

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 ...


25

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 ...


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)² &...


23

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 ...


21

LERP - Linear Interpolation I gave this answer for a similar problem some days ago, but here we go: Linear Interpolation is a function that gives you a number between two numbers, based on the progress. You could actually, get a point between two points. The Great Formula - How to calculate it The general LERP Formula is given by pu = p0 + (p1 - p0) * u. ...


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

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

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

You are looking for the wondrous atan2. // v1 moving object float boxX = this.mScene.getLastChild().getX(); float boxY = this.mScene.getLastChild().getY(); // v2 user touch float touchX = pSceneTouchEvent.getX(); float touchY = pSceneTouchEvent.getY(); double theta = 180.0 / Math.PI * Math.atan2(boxX - touchX, touchY - boxY); Normally it is used as ...


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

The angle you need to rotate by is the the angle your velocity vector makes with the positive x-axis. This angle can be calculated using the inverse tan of the slope of the vector. In XNA, we use the Math.Atan2 function. Give the function the y coordinate and the x coordinate of the velocity vector (in that order). Atan2 will return an angle between +PI/2 ...


15

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 ...


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

Let's first discuss the dot product. A·B is a measure of A's component in the direction of B or vice versa; of the magnitudes of both vectors as well as their similarity in direction. Vectors pointing in the same direction have a dot product equal to the product of their lengths, perpendicular vectors have a dot product of zero. Turn the vectors even ...


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 ...


13

Is there any notable performance between Vector2s and Vector3s, for example when adding or multiplying them, or when calling Normalize, Transform, or Distance? Yes, you have one more coordinate so you will use more CPU cycles. But it is very unlikely that it will ever give you any trouble. XNA 4 is using SIMD extensions for vector math (EDIT: on Windows ...


13

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); ...


12

From MathWorld: Given the plane Then the normal vector is The normal unit vector n is given by: Therefore, for the plane 5x+2y+3z-1=0, The normal vector N is N = [5,2,3] The magnitude |N| is |N| = sqrt(5^2 + 2^2 + 3^2) |N| = 6.1644 The normal unit vector n is therefore approximately: n = N / |N| n = [0.8111, 0.3244, 0.4866] which ...


12

Given only a point and a direction there is no defined 'right' or 'left'. Imagine being a falling raindrop, which direction is right or left for you in that case? In order to calculate (or even define) a right or left you need two directions, typically forward and up. You seem to already have a forward direction, so you need to define a up direction. ...


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

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 ...


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