We’re rewarding the question askers & reputations are being recalculated! Read more.
41

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


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


23

Yes, To name a few: Pannini Mercator Fisheye Miller The Pannini projection, for example, can capture wide fields of view in nice ways. (totally just my opinion) I think implementation details would be beyond the scope of this specific question. EDIT: Thanks for the comment, I did misspell Pannini. And to make this edit worthwhile here are a few more: ...


18

I use the following method which is pretty much just an implementation of this algorithm. It's in C# but translating it to ActionScript should be trivial. bool IsIntersecting(Point a, Point b, Point c, Point d) { float denominator = ((b.X - a.X) * (d.Y - c.Y)) - ((b.Y - a.Y) * (d.X - c.X)); float numerator1 = ((a.Y - c.Y) * (d.X - c.X)) - ((a.X - c....


18

So long as the matrix M is invertible (which it generally will be, unless you're doing something very unusual), then computing the matrix inverse of M will give you a matrix that does what you want. That is, if M performs some transformation, inverse(M) performs the "opposite" transformation. Most matrix/vector libraries provide a means for computing the ...


15

It depends on what you mean by "that could be used in a 3D system such as OpenGL". :) Narrowly speaking, 3D graphics hardware and APIs like OpenGL only deal correctly with linear projections - projections that map straight lines in world space to straight lines on the image. They never distort something into a curved shape (unless it was curved to begin ...


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


13

speed = constant_factor / distance With constant_factor at 60, you get: 50->function->1.2 20->function->3.0 If you want to damp the curve a bit, add an exponentiation: speed = (constant_factor / distance) ^ (1 / damping_factor) With constant_factor at 80, and damping_factor at 2, you get: 50->function->1.26 20->function->2.0 ...


12

Why choose? You can have both. (Without any added complexity to logic and without any additional memory requirement thanks to unions.) struct Mat4 { union { struct { float m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44; }; float[...


12

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


11

The point is between the 2 parallel lines if it's one side of one line and the other side of the other line (providing the lines point in the same direction). You can use the top answer from this question at stackoverflow to work out which side of a line (defined by 2 points on it) a point lies on. An alternative method would be to calculate the distance ...


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

Vector3 vT = v2 + headingNorm * 3; Be careful though, if v2 and v1 happen to be closer than 3 units away this will put you on the far side of v1. Maybe you want this to make the unit step back to make room for the attack. But then again be careful, because that means as you approach that attack point you will overshoot then correct and overshoot the ...


10

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-direction. For example, a vector of (3,4) goes 3 units in x direction and 4 units in the y direction, resulting in an angled line with a length of 5 (3² + 4² = 9+16 = 25, root of that is 5). So the vector basically gives you two pieces ...


10

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


9

You'll need to duplicate the normal for the corner N times (where N is the number of "sides" it is shared amongst). If you try to use one value for all "sides," you'll end up averaging it, and your lighting will not appear to have that hard edge that you're looking for.


8

Here is an algorithm for intersection only (doesn't cover touching) that I believe is fast. if t0, t1 and t2 are all on the same side of line P0P1, return NOT INTERSECTING if P0 AND P1 are on the other side of line t0t1 as t2, return NOT INTERSECTING if P0 AND P1 are on the other side of line t1t2 as t0, return NOT INTERSECTING if P0 AND P1 are on the other ...


8

Ahh yes. I threw my math at it and I think I hit it. You're correct it does involve the Pythagorean theorem and some scaling. You start with your normalized vector that represents your ray. It has an x component and a y component. First we want to see how long it is when it travels one unit in the x direction. So what do we do? We want to scale the entire ...


8

Calculate a vector from B to A, normalize it (divide by the vector's length), then multiply by the circle size: vx = A.x - B.x vy = A.y - B.y length = sqrt(vx*vx + vy*vy) C.x = vx / length * size + A.x C.y = vy / length * size + A.y For the angle you can use the atan2 function, if your language has it.


8

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, hundreds or thousands. Obviously vector addition is an overly simplistic example, but there are more complex vector operations and the same applies to those too.


7

First of all, re: "why we cannot just use the normalised sum of the sampled-normal vector, and the surface-normal?" If they're in the same space already, summing these two just has the effect of halving the strength of the normal map - it effectively blends 50% between the normal map and the non-normal-mapped geometric normals. If they're not in the same ...


7

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 construct a matrix so that, when multiplied with a list of numbers representing a source position (say, the position of a vertex in a mesh) we get a list of ...


6

To get a smooth random walk, you could use Catmull-Rom splines. This kind of spline takes a sequence of points and generates smooth curves that pass through each point. So, you could generate random waypoints for the sprite to move through and animate it along a Catmull-Rom spline through the waypoints. For the spline to work you'll need a total of four ...


6

Mathematically, the quantity you're asking about is called the operator norm. Unfortunately, there's no simple formula for it. If it's a fully general affine transformation - for instance, if it could have an arbitrary combination of rotations and nonuniform scales, in any order - then I'm afraid there's nothing for it but to use singular value ...


5

$$ \begin{align} P &= t (B-A) + A \\ P_x &= t (B_x - A_x) + A_x \\ P_y &= t (B_y - A_y) + A_y \\ \end{align} $$ $$ \frac {P_x - A_x} {B_x - A_x} = \frac {P_y - A_y} {B_y - A_y} $$ $$ \begin{align} P_y &= (P_x - A_x) \times \frac {B_y - A_y} {B_x - A_x} + A_y \\ &= (P_x - 2) \times \frac {5 - -1} {4 - 2} + -1 \\ &= (P_x - 2) \times ...


5

One easy way is to think of both coordinate systems as transforms from the unit vectors (1,0,0) (0,1,0) and (0,0,1). You start off in this coordinate space (I will call it '1')whose transform matrix is the identity matrix: [1,0,0] I = [0,1,0] [0,0,1] then your first coordinate space (I will call it '2') has the transform matrix: [Xx,Xy,Xz] A = ...


5

Short Answer Because adding two vectors together and normalizing the result will give you a vector that is halfway between the two of them. I don't think that corresponds to what you were thinking it would do. Long Answer Take for instance the following picture from an unrelated subject (Blinn-Phong shading model) and pay attention to the H vector: The H ...


5

Assuming you've benchmarked this and are sure this is a bottleneck keep reading. If not stop. Don't worry and be happy :). It's true that you will need to run the collision check algorithm every time you fire a bullet. And depending on how long it takes the bullet/laser to disappear you will have to do it multiple frames. However when you implement a solid ...


5

This answer still ignores the attempt to use matrix rotation, but I realized that there was a simple yet general solution. First, assuming that the shape is encoded as coordinates of blocks in a grid, you have an arbitrary shape containing blocks with coordinates in the X and Y axes from 0 to n, where n+1 is the maximum size of a block (traditional Tetris ...


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