Skip to main content
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, ...
Bálint's user avatar
  • 14.9k
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 ...
John Hamilton's user avatar
29 votes
Accepted

How do game engines avoid recalculating normals upon mesh rotation?

They don't. When you rotate a mesh, the normals in memory are not changed in any way. All that changes is the object's transformation matrix is updated. The raw mesh vertex and normal data remains ...
DMGregory's user avatar
  • 135k
18 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 ...
DMGregory's user avatar
  • 135k
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 ...
DMGregory's user avatar
  • 135k
16 votes

How do game engines avoid recalculating normals upon mesh rotation?

DMGregory's answer explains how this is actually handled, but I'd like to point out a couple of misconceptions: Assume that a face of a mesh is defined by vertices ...
Kevin Reid's user avatar
  • 5,498
14 votes

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

Compare the function signatures of both RotatePoints versions. Lone variables: ...
Stephane Hockenhull's user avatar
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: ...
Lasse's user avatar
  • 3,241
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. ...
Raphael Schmitz's user avatar
8 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 :)
Yas's user avatar
  • 181
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, ...
Maximus Minimus's user avatar
7 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 ...
Theraot's user avatar
  • 27.2k
6 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 ...
Bálint's user avatar
  • 14.9k
6 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 ...
DMGregory's user avatar
  • 135k
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 ...
Vaillancourt's user avatar
  • 16.3k
5 votes
Accepted

How to project a Vector3 onto the xz plane?

You can 0-out the Y coordinate. This is called an orthographic projection: ...
starikcetin's user avatar
  • 5,295
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) (...
HolyBlackCat's user avatar
  • 2,035
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 ...
wychmaster's user avatar
4 votes
Accepted

Difference between World matrix and Model matrix?

The names "world matrix" and "model matrix" are just terminology, and are not 100% standardised, so depending which source you read you may see them being used to refer to a number ...
Maximus Minimus's user avatar
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 ...
rootmenu's user avatar
  • 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, ...
DMGregory's user avatar
  • 135k
3 votes
Accepted

How to set rotation of object using angle between two points?

You should always question whether an angle is actually necessary, or if a vector would suffice. Here, all we need is the vector from the start to the end: ...
DMGregory's user avatar
  • 135k
3 votes
Accepted

Should 3D transformations be represented by a 4x4 matrix or a 3x4 matrix?

Since 3D transformations are represented by 4x4 homogeneous matrices Correction: Might be represented by 4x4 matrices ;) Now I can see how this would be a great optimization at first as it ...
wychmaster's user avatar
3 votes
Accepted

LibGDX Efficient way to reduce a vector's value gradually until it reaches 0,0

If you don't need the deceleration to be the same fixed amount each frame, you can instead apply a "drag multiplier". This is used in many games as a crude approximation of air resistance, ...
DMGregory's user avatar
  • 135k
3 votes
Accepted

Invert parent transform (doesn't work for combination of rotation and scale)

A 4x4 homogeneous matrix can represent any affine transformation. That is, any combination of: Translation by any offset Rotation around any axis by any angle Scale along multiple arbitrary axes by ...
DMGregory's user avatar
  • 135k
3 votes
Accepted

How to fix diagonal velocity being greater than axis-aligned?

Is there a fix for this so no matter what of the 8 I travel its the same speed? Yes... You want to apply a total force and split it between x and y To do this for any angle, you apply ...
Basic's user avatar
  • 1,278
3 votes

How to fix diagonal velocity being greater than axis-aligned?

You use trigonometry. Here we can just use the Pythagorean theorem: a² + b² = c² If the player is moving at a 45° angle, a and <...
Kevin's user avatar
  • 6,235
3 votes

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

Many.. many things: calculating normals for light/physics calculation using for calc distances (point/plane) checking orientation of (2D)-Polys (are they left or right wound?) calculating 2D-area of ...
Peter Parker's user avatar

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