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, ...
  • 14.6k
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 ...
31 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. ...
17 votes

How does a 4x4 matrix represent an object in space and matrix lore?

How is Matrix Multiplication a Transformation? A matrix is just a big grid of numbers with rules that define how we can multiply it with other grids or lists of numbers. In games, we usually want to ...
  • 118k
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 ...
  • 118k
14 votes

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

Compare the function signatures of both RotatePoints versions. Lone variables: ...
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: ...
  • 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. ...
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-...
  • 2,056
8 votes
Accepted

What does a linear transformation preserve?

Linear transformations preserve: Collinearity. If three points are collinear before the transformation, they remain collinear afterwards. Parallelism. If two lines are parallel before the ...
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, ...
7 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 :)
  • 171
6 votes
Accepted

Detect if two objects are going to collide

I thought this would be an interesting challenge to test some of the math I learned in school, so here's what I figured out. This approach assumes each object can be considered a circle for collision ...
  • 299
5 votes

How can I reverse the effect of a transformation matrix?

If your transformation matrix is a rotation matrix then you can simplify the problem by taking advantage of the fact that the inverse of a rotation matrix is the transpose of that matrix. If your ...
5 votes
Accepted

How do splines work?

A coder's guide to spline-based procedural geometry is a video from Unity event Unite 2015. In the video, the presenter gives visual explanation of how splines can be created and modified. Then, he ...
  • 1,149
5 votes
Accepted

Need help with getting a direction vector between two given points

In your situation you need a simple subtraction, with a normalization. vectDistance = B - A vectDirection = vectDistance / lenght(vectDistance) This will give ...
  • 15.8k
5 votes
Accepted

How to mirror/reflect/flip a 4D transformation matrix

To get a transformation matrix equivalent to the one you have, but reflected across a major axis you can compose it (multiply it by) a reflection matrix. That is, if you have your input matrix M and ...
  • 19k
5 votes
Accepted

2d Scale width and height based on distance

The very complex and hard to understand calculation people do to get an object's size at a specific distance is: divide by Z. Even the big APIs, like DirectX and OpenGL are basically just rasterizing ...
  • 14.6k
5 votes
Accepted

Is it possible to calculate a direction vector without sqrt?

If you want a correct mathematical result (correctly rounded to within floating point precision), a square root is the way to go. It's true that it's more expensive than a multiply, but it's still ...
  • 118k
4 votes

Mixing Perspective and Orthographic Projections?

I suspect the method actually used doesn't actually rely on parallel projection and it only seems so through the approximation of the article writer and subsequent translation. The problem with ...
  • 41
4 votes

In unity, how do I rotate a vector in relation to the player?

Use Transform.TransformDirection() method where Transform is your player transform. ...
  • 595
4 votes

How to determine whether the direction of rotating a 2D vector to another one is counter-clockwise or clockwise using the cross product?

I think the book means if a.Cross(b).z > 0 then ... else ... which translates to if (a.x * b.y - a.y * b.x > 0) (...
  • 1,940
4 votes
Accepted

How to scale major and minor radius of a quarter torus

To my knowledge, this is not possible using a transformation matrix since matrices represent linear transformations and the transformation you are asking for isn't linear. The answer in this SO ...
3 votes
Accepted

Adjust postion only in X axis

The simplest and non mathematical (noob) way to do this is to make Game object B a child of Game object A , then make local position x as zero and remove B from the parent A. ...
  • 1,469
3 votes

How to calculate normal vector of a Bézier curve?

If vector (x,y) is a tangent vector of your curve, then the normal vector is simply (y,-x). So you just need to find a tangent ...
  • 23.5k
3 votes

Mixing Perspective and Orthographic Projections?

What you want is a curvilinear perspective projection matrix to correct for the perspective projection distortion. aka barrel or pincushion projection: http://en.wikipedia.org/wiki/...
  • 326
3 votes

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

You could also use a more general approach: The math for your problem already exists in the form of the scalarproduct (or dot product). You only need to get the directions of your weapons forward ...
  • 131
3 votes
Accepted

Convert global 3d coordinate system to a local one with specific direction

Construct a matrix like so, where each vector is one column of the matrix: [ House's X+ direction | House's Y+ direction | House's Z+ direction | House's position] The fourth row should be [0, 0, 0, ...
  • 118k

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