Given the length of two vectors, find the angles that end on a point (2d)

36 Views Asked by At

This is a bit like a very simple inverse kinematics, but should be so simple that it can be done exactly. Er, well, I think it can!

Problem definition: given armLength0, armLength1, and target (x,y), find angle0 and angle1 where

  1. Start at the origin
  2. Rotate angle0, then move forward a fixed length armLength0
  3. Rotate angle1, then move forward fixed length armLength1
  4. End on the target (x,y) coordinate.

It isn't quite a pure trig SAS, because I don't know any of the angles. I can find some angles:

  • targetLength = sqrt(x^2+y^2)
  • cos(angle1)=(armLength0^2 + armLength1^2 − targetLength^2)/(2*armLength0*armLength1) SSS

aw man, SOHCAHTOA it has been too long.

angle0 is the sum of θA and θB:

  • cos(θA) = x / targetLength CAH
  • cos(θB) = (armLength0^2 + targetLength^2 - armLength1^2/(2*armLength0*targetLength)) SSS

Huh. Seems dodgy.

2

There are 2 best solutions below

0
On BEST ANSWER

You know the two arm lengths, $L_0$ and $L_1$. And you know $(x,y)$, so you also know the third side length of the triangle: $L=\sqrt{x^2+y^2}$.

Now you may use the law of cosines to find angle1, $\theta_1$. $$\cos(\theta_1)=\frac{L^2-L_1^2-L_2^2}{-2L_1L_2}\implies\theta_1=\arccos\left(\frac{L^2-L_1^2-L_2^2}{-2L_1L_2}\right)$$ (Applying $\arccos$ will give you an angle in $[0,\pi]$.)

Now angle0, $\theta_0$, is the sum of two angles. One of these is found in the same way using the law of cosines. The other is the arctangent of $y$ over $x$.

$$\theta_0=\arccos\left(\frac{L_2^2-L_1^2-L^2}{-2L_1L}\right)+\arctan\left(\frac{y}{x}\right)$$

(This all seems similar to what you found. I'm just tossing in the inverse trig functions. And using arctangent instead of arccos in one instance.)

0
On

This answer assumes radians are used for the angles.

Let $(x_0,y_0)$ be the point reached after step 2, then if $l_0$ is arm length $0$ and $\theta_0$ is angle $0$, the definition of sin and cos tells us $$(x_0,y_0) = (l_0 \cos (\theta_0), l_0 \sin (\theta_0))$$

The next part is a bit trickier, but notice that the angle formed between arm length 1, which we can call $l_1$, and the parallel line to the x-axis can be calculated using the interior angles of the traversal as $$\phi=(\pi -\theta_0)-\theta_1$$ and $$(x,y) = (x_0,y_0) + (l_1 \cos (\phi), -l_1 \sin (\phi))$$

Taken together, you are trying to solve for $\theta_0,\theta_1$ for equations $$x = l_0 \cos (\theta_0) + l_1 \cos (\pi - \theta_0 - \theta_1)$$ $$y = l_0 \sin (\theta_0) - l_1 \sin (\pi - \theta_0 - \theta_1)$$

which can be done numerically using a root finding algorithm.