示例#1
0
        /// <summary>
        /// 指定ボーンのすべての子へのラインに対して最近接点距離を返す
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="bone"></param>
        /// <param name="lineWidth"></param>
        /// <returns></returns>
        public static float ClosestPtBoneLine(Vector3 pos, Transform bone, float lineWidth, out Vector3 d)
        {
            float mindist = 10000;

            d = bone.position;

            // 子がいない場合はそのまま
            if (bone.childCount == 0)
            {
                mindist = Mathf.Max(Vector3.Distance(pos, bone.position) - lineWidth, 0.0f);
                return(mindist);
            }

            // すべての子へのラインに対して判定
            for (int i = 0; i < bone.childCount; i++)
            {
                var child = bone.GetChild(i);

                var spos = bone.position;
                var epos = child.position;

                var w = MathUtility.ClosestPtPointSegment(pos, spos, epos);

                float dist = Mathf.Max(Vector3.Distance(pos, w) - lineWidth, 0.0f);

                //mindist = Mathf.Min(dist, mindist);
                if (dist < mindist)
                {
                    mindist = dist;
                    d       = w;
                }
            }

            return(mindist);
        }
示例#2
0
            /// <summary>
            /// 球衝突判定
            /// </summary>
            /// <param name="nextpos0">エッジの始点</param>
            /// <param name="nextpos1">エッジの終点</param>
            /// <param name="corr0"></param>
            /// <param name="corr1"></param>
            /// <param name="radius"></param>
            /// <param name="cindex"></param>
            /// <returns></returns>
            bool SphereColliderDetection(float3 nextpos0, float3 nextpos1, ref float3 corr0, ref float3 corr1, float radius, int cindex)
            //bool SphereColliderDetection(float3 nextpos0, float3 nextpos1, float3 oldpos0, float3 oldpos1, ref float3 corr0, ref float3 corr1, float radius, int cindex)
            {
                var cpos    = nextPosList[cindex];
                var coldpos = posList[cindex];
                var cradius = radiusList[cindex];

                // コライダー球とエッジの最接近点を求める
                float3 d = MathUtility.ClosestPtPointSegment(coldpos, nextpos0, nextpos1);
                //float3 d = MathUtility.ClosestPtPointSegment(coldpos, oldpos0, oldpos1);
                float3 n = math.normalize(d - coldpos);
                float3 c = cpos + n * (cradius + radius);

                // c = 平面位置
                // n = 平面方向
                // 平面衝突判定と押し出し
                float3 outpos0, outpos1;
                bool   ret0 = MathUtility.IntersectPointPlane(c, n, nextpos0, out outpos0);
                bool   ret1 = MathUtility.IntersectPointPlane(c, n, nextpos1, out outpos1);

                if (ret0)
                {
                    corr0 += outpos0 - nextpos0;
                }
                if (ret1)
                {
                    corr1 += outpos1 - nextpos1;
                }

                return(ret0 || ret1);
            }