Search code examples
pythonvisual-studiopython-2.7visual-studio-2013game-physics

How to make 4 vectors (all which you know the direction but you can set the magnitude) add up to a known vector?


So if you have 4 vectors, and you know their directions (which all could be different), and you could change all their magnitudes, what would you set the magnitudes equal to if you want all 4 vectors to add up to another vector which you know?

This might seem like a little bit too specific question, but the reason I ask this is for a program I'm making with kRPC for KSP in which 4 tilted engines hover an aircraft, even when the entire aircraft is tilted. I tried searching it, but I didn't know exactly what to search. I don't know a lot about the math of vectors. Thanks!


Solution

  • It may not always possible, it will depend on the vectors. Technically your target vector must be in the linear span of your four input vectors.

    You can write it as the following matrix equation:

    Ax = b

    With, b the target vector, x the coefficients summing coefficients and A a matrix made by stacking your four vectors column-wise. The x vector exists if and only if the A matrix is invertible.

    You can solve this using the numpy.linalg.solve function.

    In your case, you may have problem if you have 4 vectors of dimension 3 (or 2): in this case there is not only one solution, and it gets tricky. In practice, you would need to ditch one (or two) of the vectors to keep only 3 (or 2) independent vectors.

    You can still use numpy.linalg.lstsq to get an approximative solution though.