【寒江雪】实现物体的匀速圆周运动

让物体做圆周运动

圆心在原点,半径为1的匀速圆周运动

curAngle += angleSpeed * Mathf.Deg2Rad;
float x = Mathf.Cos(curAngle);
float y = Mathf.Sin(curAngle);

圆心在原点,半径可变的匀速圆周运动

curAngle += angleSpeed * Mathf.Deg2Rad;
float x = radius * Mathf.Cos(curAngle);
float y = radius * Mathf.Sin(curAngle);

圆心可变,半径可变的匀速圆周运动

curAngle += angleSpeed * Mathf.Deg2Rad;
float x = radius * Mathf.Cos(curAngle)+centerX;
float y = radius * Mathf.Sin(curAngle)+centerY;

利用向心加速度实现物体的匀速圆周运动

public float angleSpeed = 2.0f * Mathf.PI / 120.0f;
public float curAngle = 0.0f;
[Range(0, 10)]
public float radius = 5.0f;

[Range(-10, 10)]
public float centerX = 5.0f;
[Range(-10, 10)]
public float centerY = 5.0f;

public float rx;
public float ry;
public float vx;
public float vy;

void Start() {
        angleSpeed = 2.0f * Mathf.PI / 180.0f;
        rx = radius * Mathf.Cos(angleSpeed * 0);
        ry = radius * Mathf.Sin(angleSpeed * 0);
        vx = -radius*angleSpeed*Mathf.Sin(angleSpeed*0);
        vy = radius * angleSpeed*Mathf.Cos(angleSpeed*0);
        transform.position = new Vector3(rx + radius, ry + radius, 0);
}

void Update() {
        rx += vx;
        ry += vy;
        vx += -angleSpeed * angleSpeed * rx;
        vy += -angleSpeed * angleSpeed * ry;
        //curAngle += angleSpeed * Mathf.Deg2Rad;
        float x = rx + centerX;
        float y = ry + centerY;
        transform.position = new Vector3(x, y, 0);
        Debug.DrawLine(Vector3.zero, transform.position, Color.red);
}

猜你喜欢

转载自blog.csdn.net/lkysyzxz/article/details/81038076
今日推荐