融入动画技术的交互应用

融入动画技术的交互应用

在初始化中先初始化刚体的初速度,给刚体随机方向大小的初速度,在更新函数中,以50fps更新力,其方向始终指向画布中心。

Rigidbody2D rb2d;
public float strength = 1.0f;
Vector3 center;
void Start () {
    center = new Vector3(0, 1, 0);
    rb2d = GetComponent<Rigidbody2D>();
    Vector2 velocity = Random.insideUnitCircle;
    rb2d.velocity = velocity;
}
void FixedUpdate () {
    Vector3 dir = transform.position-center;
    Vector3 Force = -dir.normalized * strength * 10;
    rb2d.AddForce(Force);

}

2、 引力2——基于万有引力的画笔实现(给定具有刚体属性的引力中心attractor):
根据万有引力公式F=Gm1m2/(r*r) 实现刚体attractor对笔刷的引力,其方向始终指向attractor。限制了初始速度方向以及距离太小导致引力太大。
void Start()
{ …………
attractor = GameObject.Find(“attractor”);
rb2d = GetComponent();
dir = attractor.transform.position - transform.position;
do{ //限定初始速度的方向
velocity = base_velocity * Random.insideUnitCircle;
dot_value = Vector2.Dot(dir, velocity);
angle = Mathf.Acos(dot_value / (dir.magnitude * velocity.magnitude));
} while (angle < Mathf.PI / 3 && angle > -Mathf.PI / 3);
rb2d.velocity = velocity;
}
void FixedUpdate(){
dir = transform.position - center;
Vector2 dir2d = new Vector2(dir.x, dir.y);
Force = -dir2d.normalized G strength * rb2d.mass * attractor.GetComponent().mass / (distance * distance);
rb2d.AddForce(Force);
…………
}

3、 空气阻力:
根据简易的空气阻力公式F=1/2CS*v^2实现对运动物体的空气阻力,其方向与速度方向相反。
public float C_Drag=1.0f;//阻力系数
Rigidbody2D rb2d;
float S;//面积
void Start () {
S = 1.0f;
Vector2 velocity = Random.insideUnitCircle;
rb2d = GetComponent();
rb2d.velocity = velocity;
}
void FixedUpdate () {
float v = rb2d.velocity.magnitude;
Vector2 Force = -rb2d.velocity.normalized * C_Drag * S * v * v * 0.5f;
rb2d.AddForce(Force);
}
引力1:
引力1
引力2:
在这里插入图片描述
空气阻力:
在这里插入图片描述
Oscillator:在x轴与y轴方向上分别实现简单的简谐振动。
public float x_Amamplitude = 0.01f;
public float y_Amamplitude = 0.01f;
public float x_velocity = 0.5f;
public float y_velocity = 0.5f;

Vector2 aVelocity;
Vector2 angle;
Vector3 location;
void Start () {
    aVelocity = new Vector2(Random.Range(-x_velocity, x_velocity), Random.Range(-y_velocity, y_velocity));
    angle = new Vector2(0,0);
    location = new Vector3(0, 0, 0);
}


void FixedUpdate () {
    location.x = x_Amamplitude * Mathf.Sin(angle.x);
    location.y = y_Amamplitude * Mathf.Sin(angle.y);
    transform.localPosition += location;
    angle += aVelocity;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值