public ContainmentType Contains(BoundingOrientedBox box)
        {
            if (!box.Intersects(this))
            {
                return(ContainmentType.Disjoint);
            }

            XMVector v_center = this.center;
            XMVector v_radius = XMVector.Replicate(this.radius);
            XMVector radiusSq = v_radius * v_radius;

            XMVector boxCenter      = box.Center;
            XMVector boxExtents     = box.Extents;
            XMVector boxOrientation = box.Orientation;

            Debug.Assert(Internal.XMQuaternionIsUnit(boxOrientation), "Reviewed");

            XMVector insideAll = XMVector.TrueInt;

            for (int i = 0; i < BoundingOrientedBox.CornerCount; i++)
            {
                XMVector c = XMVector3.Rotate(boxExtents * CollisionGlobalConstants.BoxOffsets[i], boxOrientation) + boxCenter;
                XMVector d = XMVector3.LengthSquare(XMVector.Subtract(v_center, c));
                insideAll = XMVector.AndInt(insideAll, XMVector.LessOrEqual(d, radiusSq));
            }

            return(XMVector3.EqualInt(insideAll, XMVector.TrueInt) ? ContainmentType.Contains : ContainmentType.Intersects);
        }
        public ContainmentType Contains(BoundingBox box)
        {
            if (!box.Intersects(this))
            {
                return(ContainmentType.Disjoint);
            }

            XMVector v_center = this.center;
            XMVector v_radius = XMVector.Replicate(this.radius);
            XMVector radiusSq = v_radius * v_radius;

            XMVector boxCenter  = box.Center;
            XMVector boxExtents = box.Extents;

            XMVector insideAll = XMVector.TrueInt;

            XMVector offset = boxCenter - v_center;

            for (int i = 0; i < BoundingBox.CornerCount; i++)
            {
                XMVector c = XMVector.MultiplyAdd(boxExtents, CollisionGlobalConstants.BoxOffsets[i], offset);
                XMVector d = XMVector3.LengthSquare(c);

                insideAll = XMVector.AndInt(insideAll, XMVector.LessOrEqual(d, radiusSq));
            }

            return(XMVector3.EqualInt(insideAll, XMVector.TrueInt) ? ContainmentType.Contains : ContainmentType.Intersects);
        }
Exemplo n.º 3
0
        protected void UpdateVelocity(double fElapsedTime)
        {
            XMVector vMouseDelta  = m_vMouseDelta;
            XMVector vRotVelocity = vMouseDelta * m_fRotationScaler;

            m_vRotVelocity = vRotVelocity;

            XMVector vKeyboardDirection = m_vKeyboardDirection;
            XMVector vAccel             = vKeyboardDirection;

            // Normalize vector so if moving 2 dirs (left & forward),
            // the camera doesn't move faster than if moving in 1 dir
            vAccel = XMVector3.Normalize(vAccel);

            // Scale the acceleration vector
            vAccel *= m_fMoveScaler;

            if (m_bMovementDrag)
            {
                // Is there any acceleration this frame?
                if (XMVector3.LengthSquare(vAccel).X > 0)
                {
                    // If so, then this means the user has pressed a movement key
                    // so change the velocity immediately to acceleration
                    // upon keyboard input.  This isn't normal physics
                    // but it will give a quick response to keyboard input
                    m_vVelocity = vAccel;

                    m_fDragTimer = m_fTotalDragTimeToZero;

                    m_vVelocityDrag = vAccel / (float)m_fDragTimer;
                }
                else
                {
                    // If no key being pressed, then slowly decrease velocity to 0
                    if (m_fDragTimer > 0)
                    {
                        // Drag until timer is <= 0
                        XMVector vVelocity     = m_vVelocity;
                        XMVector vVelocityDrag = m_vVelocityDrag;

                        vVelocity -= vVelocityDrag * (float)fElapsedTime;

                        m_vVelocity = vVelocity;

                        m_fDragTimer -= fElapsedTime;
                    }
                    else
                    {
                        // Zero velocity
                        m_vVelocity = XMVector.Zero;
                    }
                }
            }
            else
            {
                // No drag, so immediately change the velocity
                m_vVelocity = vAccel;
            }
        }
        public ContainmentType Contains(XMVector point)
        {
            XMVector v_center = this.center;
            XMVector v_radius = XMVector.Replicate(this.radius);

            XMVector distanceSquared = XMVector3.LengthSquare(point - v_center);
            XMVector radiusSquared   = XMVector.Multiply(v_radius, v_radius);

            return(XMVector3.LessOrEqual(distanceSquared, radiusSquared) ? ContainmentType.Contains : ContainmentType.Disjoint);
        }
        public bool Intersects(XMVector v0, XMVector v1, XMVector v2)
        {
            // Load the sphere.
            XMVector v_center = this.center;
            XMVector v_radius = XMVector.Replicate(this.radius);

            // Compute the plane of the triangle (has to be normalized).
            XMVector n = XMVector3.Normalize(XMVector3.Cross(v1 - v0, v2 - v0));

            // Assert that the triangle is not degenerate.
            Debug.Assert(!XMVector3.Equal(n, XMGlobalConstants.Zero), "Reviewed");

            // Find the nearest feature on the triangle to the sphere.
            XMVector dist = XMVector3.Dot(v_center - v0, n);

            // If the center of the sphere is farther from the plane of the triangle than
            // the radius of the sphere, then there cannot be an intersection.
            XMVector noIntersection = XMVector.Less(dist, -v_radius);

            noIntersection = XMVector.OrInt(noIntersection, XMVector.Greater(dist, v_radius));

            // Project the center of the sphere onto the plane of the triangle.
            XMVector point = v_center - (n * dist);

            // Is it inside all the edges? If so we intersect because the distance
            // to the plane is less than the radius.
            XMVector intersection = Internal.PointOnPlaneInsideTriangle(point, v0, v1, v2);

            // Find the nearest point on each edge.
            XMVector radiusSq = v_radius * v_radius;

            // Edge 0,1
            point = Internal.PointOnLineSegmentNearestPoint(v0, v1, v_center);

            // If the distance to the center of the sphere to the point is less than
            // the radius of the sphere then it must intersect.
            intersection = XMVector.OrInt(intersection, XMVector.LessOrEqual(XMVector3.LengthSquare(v_center - point), radiusSq));

            // Edge 1,2
            point = Internal.PointOnLineSegmentNearestPoint(v1, v2, v_center);

            // If the distance to the center of the sphere to the point is less than
            // the radius of the sphere then it must intersect.
            intersection = XMVector.OrInt(intersection, XMVector.LessOrEqual(XMVector3.LengthSquare(v_center - point), radiusSq));

            // Edge 2,0
            point = Internal.PointOnLineSegmentNearestPoint(v2, v0, v_center);

            // If the distance to the center of the sphere to the point is less than
            // the radius of the sphere then it must intersect.
            intersection = XMVector.OrInt(intersection, XMVector.LessOrEqual(XMVector3.LengthSquare(v_center - point), radiusSq));

            return(XMVector4.EqualInt(XMVector.AndComplementInt(intersection, noIntersection), XMVector.TrueInt));
        }
        public ContainmentType Contains(BoundingFrustum fr)
        {
            if (!fr.Intersects(this))
            {
                return(ContainmentType.Disjoint);
            }

            XMVector v_center = this.center;
            XMVector v_radius = XMVector.Replicate(this.radius);
            XMVector radiusSq = v_radius * v_radius;

            XMVector v_origin      = fr.Origin;
            XMVector v_orientation = fr.Orientation;

            Debug.Assert(Internal.XMQuaternionIsUnit(v_orientation), "Reviewed");

            // Build the corners of the frustum.
            XMVector v_rightTop    = new XMVector(fr.RightSlope, fr.TopSlope, 1.0f, 0.0f);
            XMVector v_rightBottom = new XMVector(fr.RightSlope, fr.BottomSlope, 1.0f, 0.0f);
            XMVector v_leftTop     = new XMVector(fr.LeftSlope, fr.TopSlope, 1.0f, 0.0f);
            XMVector v_leftBottom  = new XMVector(fr.LeftSlope, fr.BottomSlope, 1.0f, 0.0f);
            XMVector v_near        = XMVector.Replicate(fr.Near);
            XMVector v_far         = XMVector.Replicate(fr.Far);

            XMVector[] corners = new XMVector[BoundingFrustum.CornerCount];
            corners[0] = v_rightTop * v_near;
            corners[1] = v_rightBottom * v_near;
            corners[2] = v_leftTop * v_near;
            corners[3] = v_leftBottom * v_near;
            corners[4] = v_rightTop * v_far;
            corners[5] = v_rightBottom * v_far;
            corners[6] = v_leftTop * v_far;
            corners[7] = v_leftBottom * v_far;

            XMVector insideAll = XMVector.TrueInt;

            for (int i = 0; i < BoundingFrustum.CornerCount; i++)
            {
                XMVector c = XMVector3.Rotate(corners[i], v_orientation) + v_origin;
                XMVector d = XMVector3.LengthSquare(XMVector.Subtract(v_center, c));
                insideAll = XMVector.AndInt(insideAll, XMVector.LessOrEqual(d, radiusSq));
            }

            return(XMVector3.EqualInt(insideAll, XMVector.TrueInt) ? ContainmentType.Contains : ContainmentType.Intersects);
        }
Exemplo n.º 7
0
        private static void LoadParticles(Random rand, Particle[] pParticles, int startIndex, XMFloat3 center, XMFloat4 velocity, float spread, int numParticles)
        {
            for (int i = 0; i < numParticles; i++)
            {
                XMFloat3 delta = new XMFloat3(spread, spread, spread);

                while (XMVector3.LengthSquare(delta).X > spread * spread)
                {
                    delta.X = RPercent(rand) * spread;
                    delta.Y = RPercent(rand) * spread;
                    delta.Z = RPercent(rand) * spread;
                }

                pParticles[startIndex + i].pos.X = center.X + delta.X;
                pParticles[startIndex + i].pos.Y = center.Y + delta.Y;
                pParticles[startIndex + i].pos.Z = center.Z + delta.Z;
                pParticles[startIndex + i].pos.W = 10000.0f * 10000.0f;

                pParticles[startIndex + i].velo = velocity;
            }
        }
        public bool Intersects(BoundingSphere sh)
        {
            // Load A.
            XMVector v_centerA = this.center;
            XMVector v_radiusA = XMVector.Replicate(this.radius);

            // Load B.
            XMVector v_centerB = sh.center;
            XMVector v_radiusB = XMVector.Replicate(sh.radius);

            // Distance squared between centers.
            XMVector delta           = v_centerB - v_centerA;
            XMVector distanceSquared = XMVector3.LengthSquare(delta);

            // Sum of the radii squared.
            XMVector radiusSquared = XMVector.Add(v_radiusA, v_radiusB);

            radiusSquared = XMVector.Multiply(radiusSquared, radiusSquared);

            return(XMVector3.LessOrEqual(distanceSquared, radiusSquared));
        }
        public ContainmentType Contains(XMVector v0, XMVector v1, XMVector v2)
        {
            if (!this.Intersects(v0, v1, v2))
            {
                return(ContainmentType.Disjoint);
            }

            XMVector v_center      = this.center;
            XMVector v_radius      = XMVector.Replicate(this.radius);
            XMVector radiusSquared = XMVector.Multiply(v_radius, v_radius);

            XMVector distanceSquared = XMVector3.LengthSquare(v0 - v_center);
            XMVector inside          = XMVector.LessOrEqual(distanceSquared, radiusSquared);

            distanceSquared = XMVector3.LengthSquare(v1 - v_center);
            inside          = XMVector.AndInt(inside, XMVector.LessOrEqual(distanceSquared, radiusSquared));

            distanceSquared = XMVector3.LengthSquare(v2 - v_center);
            inside          = XMVector.AndInt(inside, XMVector.LessOrEqual(distanceSquared, radiusSquared));

            return(XMVector3.EqualInt(inside, XMVector.TrueInt) ? ContainmentType.Contains : ContainmentType.Intersects);
        }
Exemplo n.º 10
0
        public static bool CalculateEigenVectors(
            float m11,
            float m12,
            float m13,
            float m22,
            float m23,
            float m33,
            float e1,
            float e2,
            float e3,
            out XMVector pV1,
            out XMVector pV2,
            out XMVector pV3)
        {
            pV1 = Internal.CalculateEigenVector(m11, m12, m13, m22, m23, m33, e1);
            pV2 = Internal.CalculateEigenVector(m11, m12, m13, m22, m23, m33, e2);
            pV3 = Internal.CalculateEigenVector(m11, m12, m13, m22, m23, m33, e3);

            bool v1z = false;
            bool v2z = false;
            bool v3z = false;

            XMVector zero = XMGlobalConstants.Zero;

            if (XMVector3.Equal(pV1, zero))
            {
                v1z = true;
            }

            if (XMVector3.Equal(pV2, zero))
            {
                v2z = true;
            }

            if (XMVector3.Equal(pV3, zero))
            {
                v3z = true;
            }

            // check for non-orthogonal vectors
            bool e12 = Math.Abs(XMVector3.Dot(pV1, pV2).X) > 0.1f;
            bool e13 = Math.Abs(XMVector3.Dot(pV1, pV3).X) > 0.1f;
            bool e23 = Math.Abs(XMVector3.Dot(pV2, pV3).X) > 0.1f;

            // all eigenvectors are 0- any basis set
            if ((v1z && v2z && v3z) || (e12 && e13 && e23) ||
                (e12 && v3z) || (e13 && v2z) || (e23 && v1z))
            {
                pV1 = XMGlobalConstants.IdentityR0;
                pV2 = XMGlobalConstants.IdentityR1;
                pV3 = XMGlobalConstants.IdentityR2;
                return(true);
            }

            if (v1z && v2z)
            {
                XMVector v_tmp = XMVector3.Cross(XMGlobalConstants.IdentityR1, pV3);

                if (XMVector3.LengthSquare(v_tmp).X < 1e-5f)
                {
                    v_tmp = XMVector3.Cross(XMGlobalConstants.IdentityR0, pV3);
                }

                pV1 = XMVector3.Normalize(v_tmp);
                pV2 = XMVector3.Cross(pV3, pV1);
                return(true);
            }

            if (v3z && v1z)
            {
                XMVector v_tmp = XMVector3.Cross(XMGlobalConstants.IdentityR1, pV2);

                if (XMVector3.LengthSquare(v_tmp).X < 1e-5f)
                {
                    v_tmp = XMVector3.Cross(XMGlobalConstants.IdentityR0, pV2);
                }

                pV3 = XMVector3.Normalize(v_tmp);
                pV1 = XMVector3.Cross(pV2, pV3);
                return(true);
            }

            if (v2z && v3z)
            {
                XMVector v_tmp = XMVector3.Cross(XMGlobalConstants.IdentityR1, pV1);

                if (XMVector3.LengthSquare(v_tmp).X < 1e-5f)
                {
                    v_tmp = XMVector3.Cross(XMGlobalConstants.IdentityR0, pV1);
                }

                pV2 = XMVector3.Normalize(v_tmp);
                pV3 = XMVector3.Cross(pV1, pV2);
                return(true);
            }

            if (v1z || e12)
            {
                pV1 = XMVector3.Cross(pV2, pV3);
                return(true);
            }

            if (v2z || e23)
            {
                pV2 = XMVector3.Cross(pV3, pV1);
                return(true);
            }

            if (v3z || e13)
            {
                pV3 = XMVector3.Cross(pV1, pV2);
                return(true);
            }

            return(true);
        }
Exemplo n.º 11
0
        public static XMVector CalculateEigenVector(float m11, float m12, float m13, float m22, float m23, float m33, float e)
        {
            XMVector v_tmp = new XMFloat3(
                (float)((m12 * m23) - (m13 * (m22 - e))),
                (float)((m13 * m12) - (m23 * (m11 - e))),
                (float)(((m11 - e) * (m22 - e)) - (m12 * m12)));

            // planar or linear
            if (XMVector3.Equal(v_tmp, XMGlobalConstants.Zero))
            {
                float f1, f2, f3;

                // we only have one equation - find a valid one
                if ((m11 - e != 0.0f) || (m12 != 0.0f) || (m13 != 0.0f))
                {
                    f1 = m11 - e;
                    f2 = m12;
                    f3 = m13;
                }
                else if ((m12 != 0.0f) || (m22 - e != 0.0f) || (m23 != 0.0f))
                {
                    f1 = m12;
                    f2 = m22 - e;
                    f3 = m23;
                }
                else if ((m13 != 0.0f) || (m23 != 0.0f) || (m33 - e != 0.0f))
                {
                    f1 = m13;
                    f2 = m23;
                    f3 = m33 - e;
                }
                else
                {
                    // error, we'll just make something up - we have NO context
                    f1 = 1.0f;
                    f2 = 0.0f;
                    f3 = 0.0f;
                }

                if (f1 == 0.0f)
                {
                    v_tmp.X = 0.0f;
                }
                else
                {
                    v_tmp.X = 1.0f;
                }

                if (f2 == 0.0f)
                {
                    v_tmp.Y = 0.0f;
                }
                else
                {
                    v_tmp.Y = 1.0f;
                }

                if (f3 == 0.0f)
                {
                    v_tmp.Z = 0.0f;

                    // recalculate y to make equation work
                    if (m12 != 0.0f)
                    {
                        v_tmp.Y = (float)(-f1 / f2);
                    }
                }
                else
                {
                    v_tmp.Z = (float)((f2 - f1) / f3);
                }
            }

            if (XMVector3.LengthSquare(v_tmp).X > 1e-5f)
            {
                return(XMVector3.Normalize(v_tmp));
            }
            else
            {
                // Multiply by a value large enough to make the vector non-zero.
                v_tmp *= 1e5f;
                return(XMVector3.Normalize(v_tmp));
            }
        }