Search Unity

erratic movement when rotating an arbitrary point around another arbitrary point

Discussion in 'Editor & General Support' started by nowletsstart, Jun 29, 2018.

  1. nowletsstart

    nowletsstart

    Joined:
    Jan 1, 2016
    Posts:
    78
    I have written the following methods to rotate an arbitrary point around another arbitrary point by an angle over a duration. The point in question now moves erratically, but ends up around where I believe is the desired destination. I need to get it to move smoothly to the angle.

    Please note that the points are independent of a game object.

    From the diagram below, I am trying to move one point of a bezier curve (drawn with a LineRenderer) around another point by an angle over a given period of time. None of these points coincide with the position of the game object which contains the bezier curve. 


    Screen Shot 2018-06-29 at 12.23.49 PM.png



    Code (CSharp):
    1. IEnumerator runMovement()  {
    2.  
    3.     yield return new WaitForSeconds(2.0f);
    4.     Vector2 pivot = points[3];
    5.  
    6.     StartCoroutine(RotateControlPointWithDuration(pivot, 2.0f, 90.0f));
    7.  
    8. }
    9.  
    10.    IEnumerator RotateControlPointWithDuration(Vector2 pivot, float duration, float angle)
    11. {
    12.     float currentTime = 0.0f;
    13.     float ourTimeDelta = 0;
    14.     Vector2 startPos = points[0];
    15.  
    16.     ourTimeDelta = Time.deltaTime;
    17.     float angleDelta = angle / duration; //how many degress to rotate in one second
    18.  
    19.     while (currentTime < duration)
    20.     {
    21.         currentTime += Time.deltaTime;
    22.         ourTimeDelta = Time.deltaTime;
    23.  
    24.         points[0] = new Vector2(Mathf.Cos(angleDelta * ourTimeDelta) * (startPos.x - pivot.x) - Mathf.Sin(angleDelta * ourTimeDelta) * (startPos.y - pivot.y) + pivot.x,
    25.                                                         Mathf.Sin(angleDelta * ourTimeDelta) * (startPos.x - pivot.x) + Mathf.Cos(angleDelta * ourTimeDelta) * (startPos.y - pivot.y) + pivot.y);
    26.  
    27.         yield return null;
    28.     }
    29. }