二维绕任意点旋转_围绕另一个任意点旋转任意点的问题

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.

IEnumerator runMovement() {

yield return new WaitForSeconds(2.0f);

Vector2 pivot = points[3];

StartCoroutine(RotateControlPointWithDuration(pivot, 2.0f, 90.0f));

}

IEnumerator RotateControlPointWithDuration(Vector2 pivot, float duration, float angle)

{

float currentTime = 0.0f;

float ourTimeDelta = 0;

Vector2 startPos = points[0];

ourTimeDelta = Time.deltaTime;

float angleDelta = angle / duration; //how many degress to rotate in one second

while (currentTime < duration)

{

currentTime += Time.deltaTime;

ourTimeDelta = Time.deltaTime;

points[0] = new Vector2(Mathf.Cos(angleDelta * ourTimeDelta) * (startPos.x - pivot.x) - Mathf.Sin(angleDelta * ourTimeDelta) * (startPos.y - pivot.y) + pivot.x,

Mathf.Sin(angleDelta * ourTimeDelta) * (startPos.x - pivot.x) + Mathf.Cos(angleDelta * ourTimeDelta) * (startPos.y - pivot.y) + pivot.y);

yield return null;

}

}

解决方案

The pattern you want is just

public IEnumerator HowToSmoothly()

{

// move "X" from value "A" to value "B"

float duration = 2.5f;

float delta = B - A;

float startTime = Time.time;

float finishTime = Time.time+duration;

while(Time.time

{

float soFarTime = Time.time-startTime;

float fractionThisFrame = soFarTime / duration;

float valueThisFrame = A + delta * fractionThisFrame;

X = valueThisFrame

if (X > B) X = B;

yield return 0;

}

X = B;

yield break;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值