Exemplo n.º 1
0
        private static bool FindMTD(Vector2f[] xAxis, float[] taxis, int iNumAxes, ref Vector2f N, ref float t)
        {
            // find collision first
            int mini = -1;

            t = 0.0f;
            for (int i = 0; i < iNumAxes; i++)
            {
                if (taxis[i] > 0)
                {
                    if (taxis[i] > t)
                    {
                        mini = i;
                        t    = taxis[i];
                        N    = xAxis[i];
                        AppMath.Vec2Normalize(ref N); //.Normalise();
                    }
                }
            }

            // found one
            if (mini != -1)
            {
                return(true);
            }

            // nope, find overlaps
            mini = -1;
            for (int i = 0; i < iNumAxes; i++)
            {
                float n = (float)AppMath.Vec2Length(AppMath.Vec2NormalizeReturn(xAxis[i]));   //xAxis[i].Normalise();
                taxis[i] /= n;

                if (taxis[i] > t || mini == -1)
                {
                    mini = i;
                    t    = taxis[i];
                    N    = xAxis[i];
                }
            }

            if (mini == -1)
            {
                throw new Exception("Collision exception.");
            }

            return(mini != -1);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="A">First gameObject</param>
        /// <param name="B">Second gameObject</param>
        /// <param name="onlyFirst">only seperate the first object</param>
        public static void SeparateEntites(cGameObject A, cGameObject B, bool onlyFirst = false)
        {
            Vector2f to              = A.GetCenterPos() - B.GetCenterPos();
            float    dist            = (float)AppMath.Vec2Length(to);
            float    amountOfOverlap = (dist - A.BoundingRadius - B.BoundingRadius); // *0.5f
            Vector2f offset          = amountOfOverlap * (to / dist);

            if (onlyFirst)
            {
                A.Position -= offset;
                return;
            }

            offset     *= 0.5f;
            A.Position -= offset;
            B.Position += offset;
        }