欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

碰撞反弹计算反弹线速度

程序员文章站 2022-05-15 21:30:43
...
/// <summary>
/// 碰撞反弹计算反弹线速度忽略Y
/// </summary>
/// <param name="ColliderPoint">碰撞点</param>
/// <param name="ColliderNoChangeDirObj">被碰撞的的对象,在碰撞过程中不需要改变方向的对象</param>
/// <param name="ColliderNeedChangeDirObjVelocity">碰撞过程中需要改变方向对象的线速度</param>
/// <returns></returns>
public Vector3 ColliderReboundIgnoreY(Vector3 ColliderPoint, GameObject ColliderNoChangeDirObj, Vector3 ColliderNeedChangeDirObjVelocity)
{
    //忽略Y

    //法线
    Vector3 _FaXian = ColliderPoint - ColliderNoChangeDirObj.transform.position;
    _FaXian.y = 0;
    _FaXian = _FaXian.normalized;

    //垂直法线的向量,方向沿法线方向的向右
    Vector3 _FaXian_Right;
    if (_FaXian.z == 0)
    {
        _FaXian_Right= new Vector3(0, 0, -1);
    }
    else
    {
        _FaXian_Right = new Vector3(-_FaXian.z / _FaXian.x, 0, 1).normalized;
    }

    //武器线速度
    ColliderNeedChangeDirObjVelocity.y = 0;
    ColliderNeedChangeDirObjVelocity = ColliderNeedChangeDirObjVelocity.normalized;

    //武器线速度上某一点
    Vector3 _WeaponVelocity_Point = ColliderPoint + (-ColliderNeedChangeDirObjVelocity);//武器线速度上一点

    //发现上某一点
    Vector3 _FaXian_Point = ColliderPoint + _FaXian;//法线上一点

    //线速度上某一点到法线的距离
  // = DistancePoint2Line(_WeaponVelocity_Point, _FaXian, _FaXian_Point);

    float fProj = Vector3.Dot(_WeaponVelocity_Point - _FaXian_Point, _FaXian.normalized);
    float PointToFaXianDistance = Mathf.Sqrt((_WeaponVelocity_Point - _FaXian_Point).sqrMagnitude - fProj * fProj);

    //判断武器驶入的方向在法线左侧还是右侧
    Vector3 right;
    if (Vector3.Dot(_FaXian_Right, -_FaXian) > 0)//从法线右侧射入
    {
        right = -_FaXian_Right;
    }
    else
    {
        right = _FaXian_Right;
    }

    //计算出反射方向上某一点
    Vector3 _FaShe_Point = _WeaponVelocity_Point + right * 2 * PointToFaXianDistance;

    //计算反射向量
    Vector3 _FaShe = _FaShe_Point - ColliderPoint;
    _FaShe.y = 0;
    _FaShe = _FaShe.normalized;

    return _FaShe;

}

碰撞反弹计算反弹线速度

相关标签: 反弹