Hot answers tagged

38 votes

Benefit of using Vector2 data type over separate x & y variables

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, ...
user avatar
  • 14.5k
36 votes

If a 3D vector represents a point, how can it have a length?

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 ...
user avatar
  • 15.7k
35 votes
Accepted

How can I rotate an object based on another's offset to it?

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 ...
user avatar
30 votes
Accepted

How can I find the perpendicular to a 2D vector?

I always forget how to do this when I need it so I wrote a couple of extension methods. ...
user avatar
28 votes

Why can't I use the operator '>=' with Vector3s?

Defining >= for a Vector3 type makes no sense. What determines if one vector is greater than another? Their magnitude or ...
user avatar
28 votes
Accepted

How can I get the normal vector for a plane from a set of vertices?

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 (...
user avatar
  • 507
20 votes
Accepted

If a 3D vector represents a point, how can it have a length?

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 ...
user avatar
  • 673
17 votes
Accepted

How do I tell if my enemy can see my player?

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 ...
user avatar
16 votes
Accepted

How do I interpret the dot product of non-normalized vectors?

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 ...
user avatar
  • 115k
16 votes
Accepted

Why can't I use the operator '>=' with Vector3s?

To simplify the answer, Vector3 is a custom struct provided by the UnityEngine namespace. ...
user avatar
  • 5,176
16 votes

What are some good examples of the practical usages of cross product in game development?

Generating Normals As Maximus Minimus notes in the comments, when we have a mesh without normal vectors (say just raw vertex positions from a procedural generator or 3D scanned point cloud), we can ...
user avatar
  • 115k
15 votes
Accepted

How can I "zero out" velocity in an arbitrary direction?

This is pretty simple linear algebra. You can make a little helper method for it: ...
user avatar
  • 115k
14 votes

Benefit of using Vector2 data type over separate x & y variables

Compare the function signatures of both RotatePoints versions. Lone variables: ...
user avatar
13 votes
Accepted

What's the 'proper' way to get unit vectors?

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 ...
user avatar
  • 3,792
12 votes
Accepted

How can I check if a player-drawn line follows a path?

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....
user avatar
  • 13.3k
11 votes
Accepted

Why does a unit vector show direction?

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: ...
user avatar
  • 3,182
11 votes

Benefit of using Vector2 data type over separate x & y variables

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. ...
user avatar
10 votes

How can I draw a line of certain length and direction?

You can simply get the vector pointing in the right direction, then scale it by the distance you want and add that to the initial point. This will define a new end point for your line. ...
user avatar
  • 72.7k
10 votes
Accepted

Does vector transformation order matter?

Because of the linearity (aka distributive property) of vector addition and matrix multiplication, it doesn't matter! Yay! ...
user avatar
  • 3,567
10 votes
Accepted

Moving a 2D object along circular arc between two points

You can use Bezier Curves. Given 2 given points, you have to define one third point yourself that controls where the curve is made. In your example you can create the point between your two given ...
user avatar
  • 4,238
9 votes
Accepted

What is a direction vector and why should it be normalized?

A 2D vector has two values (x and y), and it basically says how far you go from the point of origin in the x- and in the y-...
user avatar
  • 2,056
9 votes
Accepted

Unity: transform.LookAt(target) not "looking at" target?

The Problem: Your eyes default rotation makes them look to the negative X axis. However, Transform.LookAt() rotates the transform, so that the Transform.forward looks at the target. The Transform....
user avatar
8 votes
Accepted

How to find point on a circle that's opposite another point?

Calculate a vector from B to A, normalize it (divide by the vector's length), then multiply by the circle size: ...
user avatar
  • 1,261
8 votes

If a 3D vector represents a point, how can it have a length?

Does that mean that the magnitude is simply it is distance from the origin point (0, 0, 0)? You can see it that way, but only seeing it that way may lead to a wrong understanding. First of all, a ...
user avatar
  • 803
8 votes

Benefit of using Vector2 data type over separate x & y variables

Operator overloading. Vector v3 = v2 + v1; There is now only one place in your code where you have to write, test and debug vector addition, as opposed to tens, ...
user avatar
7 votes

What are normal, tangent and binormal vectors and how are they used?

I know this is old but i would like to add a little practical information. As stated by others but more specifically related to graphical programming. A Normal aka (a surface normal) is orthogonal to ...
user avatar
6 votes

How can I find the perpendicular to a 2D vector?

If , then a-perp obtained by a counterclockwise rotation by 90 degrees, i.e., See this link :)
user avatar
  • 161
6 votes

How do I convert two vectors into a bounding box with 8 vertices?

For an axis-aligned bounding box represented by a minimum and maximum corner, you can extract six pieces of information, xMin, ...
user avatar
  • 4,074

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