コード例 #1
0
ファイル: PhysicsEngine.cs プロジェクト: AlienSmith/Origin
        private static Tuple <CollisionInfo, CollisionInfo> CheckWouldCollide(CollisionComponent movingObject, Vector2 movement, CollisionComponent otherObject)
        {
            Vector2 origin      = movingObject.Entity.Position + movement;
            Vector2 otherOrigin = otherObject.Entity.Position;

            //TODO Add F*****g Triangles

            if (movingObject.BoundaryType == CollisionComponent.CollisionBoundaryType.Square && otherObject.BoundaryType == CollisionComponent.CollisionBoundaryType.Square)
            {
                Tuple <CollisionInfo, CollisionInfo> currentTuple = AABBCollision(movingObject, movement, otherObject, origin, otherOrigin);
                if (currentTuple != null)
                {
                    return(currentTuple);
                }
            }
            else if (movingObject.BoundaryType == CollisionComponent.CollisionBoundaryType.Square && otherObject.BoundaryType == CollisionComponent.CollisionBoundaryType.Triangle)
            {
                TriangleColliderComponent theotherobject = (TriangleColliderComponent)otherObject;
                BoxColliderComponent      playerobject   = (BoxColliderComponent)movingObject;
                if (TriangleColliderComponent.PlayerToTriangle(origin, (TriangleColliderComponent)otherObject))
                {
                    Vector2 P         = origin - otherOrigin;
                    Vector2 N         = theotherobject.NormalVector;
                    float   L2limites = playerobject.Height * TriangleColliderComponent.MagicNumber;
                    float   L1limites = theotherobject.size * TriangleColliderComponent.MagicNumber;
                    Vector2 L2        = MathmaticHelper.VectorHelper.projPtoN(P, N);
                    Vector2 L1        = MathmaticHelper.VectorHelper.perpPtoN(P, N);
                    if (L2.Length() < L2limites && L1.Length() < L1limites)
                    {
                        Vector2 CollisionPoint = origin - theotherobject.NormalVector * L2limites;
                        return(new Tuple <CollisionInfo, CollisionInfo>(new CollisionInfo(otherObject, CollisionPoint, theotherobject.NormalVector * (-1)), new CollisionInfo(movingObject, CollisionPoint, theotherobject.NormalVector)));
                    }
                }
                else
                {
                    Tuple <CollisionInfo, CollisionInfo> currentTuple = AABBCollision(movingObject, movement, otherObject, origin, otherOrigin);
                    if (currentTuple != null)
                    {
                        return(currentTuple);
                    }
                }
            }
            else
            {
                throw new NotImplementedException("I don't got that collider shit made");
            }

            return(null);
        }
コード例 #2
0
        /// <summary>
        /// Diagonals the collision.
        ///
        /// TODO DOES NOT HANDLE ignore collisions.
        /// </summary>
        /// <param name="movingObject">The moving object.</param>
        /// <param name="movement">The movement.</param>
        /// <param name="otherObject">The other object.</param>
        /// <param name="origin">The origin.</param>
        /// <param name="otherOrigin">The other origin.</param>
        /// <param name="ignoreCollisions">The ignore collisions.</param>
        /// <returns></returns>
        private static Tuple <CollisionInfo, CollisionInfo> DiagonalCollision(CollisionComponent movingObject, Vector2 movement, CollisionComponent otherObject, Vector2 origin, Vector2 otherOrigin)
        {
            TriangleColliderComponent theotherobject = (TriangleColliderComponent)otherObject;
            BoxColliderComponent      playerobject   = (BoxColliderComponent)movingObject;

            if (TriangleColliderComponent.PlayerToTriangle(origin, theotherobject))
            {
                Vector2 P         = origin - otherOrigin;
                Vector2 N         = theotherobject.NormalVector;
                float   L2limites = playerobject.Height * TriangleColliderComponent.MagicNumber;
                float   L1limites = theotherobject.size * TriangleColliderComponent.MagicNumber;
                Vector2 L2        = MathmaticHelper.VectorHelper.projPtoN(P, N);
                Vector2 L1        = MathmaticHelper.VectorHelper.perpPtoN(P, N);
                if (L2.Length() < L2limites && L1.Length() < L1limites)
                {
                    Vector2 CollisionPoint = origin - theotherobject.NormalVector * L2limites;
                    Debug.WriteLine(theotherobject.NormalVector.X + " " + theotherobject.NormalVector.Y);
                    return(new Tuple <CollisionInfo, CollisionInfo>(new CollisionInfo(otherObject, CollisionPoint, theotherobject.NormalVector), new CollisionInfo(movingObject, CollisionPoint, theotherobject.NormalVector * (-1))));
                }
            }

            return(null);
        }