Beispiel #1
0
        private static JellyVector2 ClosestPoint(JellyVector2 v, JellyVertex[] poly)
        {
            float        minlen = float.MaxValue;
            int          i = 0;
            JellyVector2 cpt = null, tcpt;
            float        len;

            while (i < poly.Length - 1)
            {
                tcpt = JellyMath.ClosestPointOnSegment(poly[i].Position, poly[i + 1].Position, v);
                ++i;
                len = (v - tcpt).Length();
                if (len < minlen)
                {
                    minlen = len;
                    cpt    = tcpt;
                }
            }
            tcpt = JellyMath.ClosestPointOnSegment(poly[i].Position, poly[0].Position, v);
            len  = (v - tcpt).Length();
            if (len < minlen)
            {
                minlen = len;
                cpt    = tcpt;
            }
            return(cpt);
        }
Beispiel #2
0
        private void CreateCurveEdgeBuffer()
        {
            int cvc        = 0;
            int startindex = 0;

            while (cvc < _divedgebuffer.Length - 2)
            {
                JellyMath.ComputeBezierPoint(_divedgebuffer[cvc], _divedgebuffer[cvc + 1], _divedgebuffer[cvc + 2], ref _crvedgebuffer, ref startindex, DivideIter);
                ++cvc;
            }
            JellyMath.ComputeBezierPoint(_divedgebuffer[cvc], _divedgebuffer[cvc + 1], _divedgebuffer[0], ref _crvedgebuffer, ref startindex, DivideIter);
            ++cvc;
            JellyMath.ComputeBezierPoint(_divedgebuffer[cvc], _divedgebuffer[0], _divedgebuffer[1], ref _crvedgebuffer, ref startindex, DivideIter);
        }
Beispiel #3
0
        public bool Select(JellyVector2 mpos, out int id, out float u, out float v, out float w)
        {
            id = 0;
            foreach (ArePreLink apl in _areprelinks)
            {
                if (JellyMath.PointTriangle(mpos, apl.jva.Position, apl.jvb.Position, apl.jvc.Position, out u, out v, out w))
                {
                    _face.Frame = 1;
                    return(true);
                }
                else
                {
                    ++id;
                }
            }
            u  = -1;
            v  = -1;
            w  = -1;
            id = int.MaxValue;

            _face.Frame = 0;
            return(false);
        }
Beispiel #4
0
        public static void ProcessEdgeForce(JellyVertex[] polygon, JellyVertex jv)
        {
            if (JellyMath.PointInPolygon(polygon, jv.Position, new JellyVector2(-10000, -10000)))
            {
                jv.Velocity.X = -jv.Velocity.X * JellyWorld.Friciton;
                jv.Velocity.Y = -jv.Velocity.Y * JellyWorld.Friciton;
                jv.FX         = jv.FY = 0;
                jv.Position   = ClosestPoint(jv.Position, polygon);
            }
            else
            {
#if _DIS_RANGE_
                const float threshold = 44.7213595499958f;
#else
                const float threshold = 3162.27766016838f;
#endif
                const float  forcek = 100000;
                int          i      = 0;
                JellyVector2 cpt;
                bool         inVertex, inThreshold, inBack;
                while (i < polygon.Length - 1)
                {
                    FindClosestPointThreshold(jv.Position, polygon[i].Position, polygon[i + 1].Position, threshold * threshold,
                                              out cpt, out inVertex, out inThreshold, out inBack);
                    ++i;
                    if (!inBack && inThreshold)
                    {
                        JellyVector2 diff  = jv.Position - cpt;
                        float        dissq = diff.LengthSquared();
                        dissq = dissq == 0 ? 0.001f : dissq;
#if _DIS_RANGE_
                        float norsize = forcek / dissq - 50;
#else
                        float norsize = forcek / dissq;
#endif
                        norsize = norsize < 0 ? 0 : norsize;
                        if (inVertex)
                        {
                            norsize *= 0.5f;
                        }
                        diff.Normalize();
                        diff     *= norsize;
                        jv.Force += diff;
                    }
                }
                FindClosestPointThreshold(jv.Position, polygon[i].Position, polygon[0].Position, threshold * threshold,
                                          out cpt, out inVertex, out inThreshold, out inBack);
                if (!inBack && inThreshold)
                {
                    JellyVector2 diff  = jv.Position - cpt;
                    float        dissq = diff.LengthSquared();
                    dissq = dissq == 0 ? 0.001f : dissq;
#if _DIS_RANGE_
                    float norsize = forcek / dissq - 50;
#else
                    float norsize = forcek / dissq;
#endif
                    norsize = norsize < 0 ? 0 : norsize;
                    if (inVertex)
                    {
                        norsize *= 0.5f;
                    }
                    diff.Normalize();
                    diff     *= norsize;
                    jv.Force += diff;
                }
            }
        }