Splitting a vector into two axis aligned vectors

1k Views Asked by At

I'm not familiar with mathematical terms, so I'll try my best to explain this issue. Also, I don't know if this is a programming question or a mathematical one. I guess both...

I'm making a 2D platformer game. Suppose we have a ground represented by a line segment. The "upper" part of this line segment is the "ground collider", when the character hits it, this ground collider returns a direction vector which points up, perpendicular to the direction of the line.

Let's say that this up direction is the "y" axis of my space, and the ground direction is the "x" axis of my space.

Given an arbitrary point inside this space, I need to find the two vectors on the y-axis and x-axis that represent this point.

I hope I've been clear enough.

Axis Aligned Vectors

To help understanding the picture:

  1. The ground is the blue line
  2. The ground specifies the direction of the X (pink) and Y (red) axis.
  3. Given an arbirtrary point (green)...
  4. We have a vector (yellow) that represents it
  5. The question is, what are the values of the y-aligned (in white) and x-aligned (also in white) vectors, that are relative to the yellow vector?

Thanks in advance.

1

There are 1 best solutions below

1
On BEST ANSWER

What you're looking for is projections. For example, suppose that the ground vector is pointing in the direction of $\vec u = (3,4)$, and that the arbitrary point that we want to project is at $\vec v = (1,5)$. Then the ground-aligned portion of this arbitrary point (that is, its projection onto the ground vector) is given by: $$ \text{proj}_{\vec u}(\vec v) = \frac{\vec v \cdot \vec u}{\vec u \cdot \vec u}\vec u = \frac{(1, 5) \cdot (3,4)}{(3,4) \cdot (3,4)}\vec u = \frac{3 + 20}{9 + 16}\vec u = \frac{23}{25}(3,4) = \left(\frac{69}{25}, \frac{92}{25} \right) = (2.76, 3.68) $$

Note that the dot product of two $2$-dimensional vectors $\vec a = (x_1, y_1)$ and $\vec b = (x_2, y_2)$ is given by: $$ \vec a \cdot \vec b = x_1x_2 + y_1y_2 $$