1
\$\begingroup\$

enter image description here

From the bezier curve image above, how can I set the angle r manually, so that changing it does not affect the distance between A and B?

\$\endgroup\$
2
\$\begingroup\$
// Form the direction the tangent should point when the angle is zero.
var zeroAngleDirection = (D - A).normalized;

// Compute a rotation by the desired angle, clockwise about the positive z axis.
var twist = Quaternion.Euler(0, 0, -angle);

// Set point B a desired distance away from A, in the rotated direction.
B = A + twist * zeroAngleDirection * desiredLength;
\$\endgroup\$
  • \$\begingroup\$ This works perfectly. I created a slider for changing the angle (which is positive or negative) from the inspector editor. When I manually move the control points around (using the mouse in the scene view) I use Vector2.Angle to update the slider angle value. But Vector2.Angle always gives positive angles. How can I get the angle values of Vector2.Angle to coincide (positive and negative) with that of the code above? \$\endgroup\$ – TenOutOfTen Oct 3 '18 at 18:26
  • \$\begingroup\$ You might want to use Mathf.Atan2 instead. \$\endgroup\$ – DMGregory Oct 3 '18 at 18:28
  • \$\begingroup\$ I used Mathf.Atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Mathf.PI; but it's also only giving me positive values \$\endgroup\$ – TenOutOfTen Oct 3 '18 at 18:56
  • 1
    \$\begingroup\$ Make sure you're measuring the angle you mean to. You want the difference in the angle between vector AD and vector AB. So Mathf.DeltaAngle(Mathf.Rad2Deg * Mathf.Atan2(p2.y - p1.y, p2.x - p1.x), Mathf.Rad2Deg * Mathf.Atan2(p4.y-p1.y, p4.x-p1.x)) Possibly negated? Or you can use your Vector2.Angle approach and recover the sign by checking whether B is to the right or to the left of D relative to A. \$\endgroup\$ – DMGregory Oct 3 '18 at 21:02
  • \$\begingroup\$ This might seem simple but it's beating me. Say from the above diagram I want to move D using your solution with the direction still var zeroAngleDirection = (D - A).normalized;. How should I make a "copy" or get the original value of D? I made a slider that I use to change the angle in the editor class so the value of D is always being updated. \$\endgroup\$ – TenOutOfTen Oct 24 '18 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.