コード例 #1
0
        /// <summary>
        /// Checks if a collider is out of bounds and outputs the amount of distance.
        /// </summary>
        /// <param name="collider">The collider to check.</param>
        /// <param name="bounds">The bounds in which the collider should stay.</param>
        /// <param name="outOfBoundsDistance">The amount of distance the collider is out of bounds.</param>
        /// <returns><value>true</value> if out of bounds; otherwise, <value>false</value>.</returns>
        public static bool CheckBoundsCollision(IBoxCollider collider, RectangleF bounds, out Vector2 outOfBoundsDistance)
        {
            float?outOfBoundsX = null;
            float?outOfBoundsY = null;

            // Add padding to determine the actual bounds
            float boundsLeft   = bounds.Left + collider.BoundsPadding.X;
            float boundsRight  = bounds.Right - collider.BoundsPadding.X;
            float boundsTop    = bounds.Top + collider.BoundsPadding.Y;
            float boundsBottom = bounds.Bottom - collider.BoundsPadding.Y;

            // Determine the out of bounds distance on the X-axis
            if (collider.Hitbox.Left < boundsLeft)
            {
                outOfBoundsX = -Math.Abs(boundsLeft - collider.Hitbox.Left);
            }
            else if (collider.Hitbox.Right > boundsRight)
            {
                outOfBoundsX = collider.Hitbox.Right - boundsRight;
            }

            // Determine the out of bounds distance on the Y-axis
            if (collider.Hitbox.Top < boundsTop)
            {
                outOfBoundsY = -Math.Abs(boundsTop - collider.Hitbox.Top);
            }
            else if (collider.Hitbox.Bottom > boundsBottom)
            {
                outOfBoundsY = collider.Hitbox.Bottom - boundsBottom;
            }

            outOfBoundsDistance = new Vector2(outOfBoundsX ?? 0F, outOfBoundsY ?? 0F);
            return(outOfBoundsDistance != Vector2.Zero);
        }
コード例 #2
0
        /// <summary>
        /// Checks if a collision between two colliders happend and outputs the amount of distance the first collider intersects with the second.
        /// </summary>
        /// <param name="first">The main collider to check the collision for.</param>
        /// <param name="second">The second collider to check the collision with.</param>
        /// <param name="intersection">The amount of distance the first collider intersects with the second.</param>
        /// <returns><value>true</value> if a collision was detected; otherwise, <value>false</value>.</returns>
        public static bool CheckColliderCollision(IBoxCollider first, IBoxCollider second, out Vector2 intersection)
        {
            // The main collider can have a negative or positive intersection, based on the location of it's center relative to the center of the other collider.
            // This way, we can determine where a collision happened and how the collider's position should/could be adjusted.
            // For example: a positive X intersection would mean a collision happened on the left side of second collider and the intersection should be subtracted.

            float?intersectionX = null;
            float?intersectionY = null;

            Vector2 firstCenter  = first.Hitbox.Center;
            Vector2 secondCenter = second.Hitbox.Center;

            if (firstCenter.X < secondCenter.X)
            {
                if (first.Hitbox.Right > second.Hitbox.Left)
                {
                    // Positive X intersection
                    intersectionX = first.Hitbox.Right - second.Hitbox.Left;
                }
            }
            else if (first.Hitbox.Left < second.Hitbox.Right)
            {
                // Negative X intersection
                intersectionX = first.Hitbox.Left - second.Hitbox.Right;
            }

            if (intersectionX.HasValue)
            {
                if (firstCenter.Y < secondCenter.Y)
                {
                    if (first.Hitbox.Bottom > second.Hitbox.Top)
                    {
                        // Positive Y intersection
                        intersectionY = first.Hitbox.Bottom - second.Hitbox.Top;
                    }
                }
                else if (first.Hitbox.Top < second.Hitbox.Bottom)
                {
                    // Negative Y intersection
                    intersectionY = first.Hitbox.Top - second.Hitbox.Bottom;
                }

                if (intersectionY.HasValue)
                {
                    intersection = new Vector2(intersectionX.Value, intersectionY.Value);
                    return(true);
                }
            }

            intersection = Vector2.Zero;
            return(false);
        }
コード例 #3
0
ファイル: Character.cs プロジェクト: troyanich/Test
    public Character(TestableGameObject obj,
                     IRigidBody body,
                     IBoxCollider collider
                     /*[Resource("Characters/char")] TestableGameObject character*/
                     ) : base(obj)
    {
        this.obj         = obj;
        this.collider    = collider;
        this.body        = body;
        body.isKinematic = true;
        TestableGameObject character = new UnityGameObject(GameManager.instance.Character);

        character.transform.Parent        = obj.transform;
        character.transform.LocalPosition = Vector3.zero;
        obj.transform.Position            = GameManager.instance.LevelController.StartTransform.position;
        GameManager.instance.CanRun       = true; //старт
    }
コード例 #4
0
ファイル: Ball.cs プロジェクト: DonkerNet/pong
        public void OnColliderCollision(IBoxCollider otherCollider, Vector2 intersection)
        {
            PaddleBase paddle = otherCollider as PaddleBase;

            if (paddle == null)
            {
                return; // Only check collisions with paddles
            }
            if (paddle.PaddleSide == PaddleSide.Left && (Angle.IsBetween(270, 360, true) || Angle.IsBetween(0, 90, true)))
            {
                return; // Ignore a left paddle collision if the ball is already facing right
            }
            if (paddle.PaddleSide == PaddleSide.Right && (Angle.IsBetween(90, 270, true)))
            {
                return; // Ignore a right paddle collision if the ball is already facing left
            }

            /*
             * When the ball hit a player paddle we need to bounce it back based on the location of the paddle.
             * The further the Y center of the ball is to the Y center of the paddle, the larger the angle will be.
             *
             * For example:
             * If the ball's Y center is above the paddle's, it will bounce towards the top of the screen.
             * If it's below, it will bounce towards the bottom of the screen.
             */

            float degreesPerPixel = SettingsConstants.BallAngleRange / otherCollider.Hitbox.Height;

            // Left paddle collision
            if (paddle.PaddleSide == PaddleSide.Left)
            {
                // Adjust the angle
                const float startAngle = SettingsConstants.BallRightDirectionAngleStart + (SettingsConstants.BallAngleRange / 2F);
                Angle = startAngle + (Hitbox.Center.Y - otherCollider.Hitbox.Center.Y) * degreesPerPixel;
            }
            // Right paddle collision
            else
            {
                // Adjust the angle
                const float startAngle = SettingsConstants.BallLeftDirectionAngleStart + (SettingsConstants.BallAngleRange / 2F);
                Angle = startAngle + (otherCollider.Hitbox.Center.Y - Hitbox.Center.Y) * degreesPerPixel;
            }

            _audioManager.PlaySfx("BallPaddleHit");
        }
コード例 #5
0
        /// <summary>
        /// Intersects the ray with an collider object.
        /// </summary>
        /// <param name="collider"></param>
        /// <returns>The distance to the object or null if the object was not hit.</returns>
        public double?Intersect(IBoxCollider collider)
        {
            if (collider == null)
            {
                return(null);
            }

            float tmin, tmax, tymin, tymax, tzmin, tzmax;

            var bounds = new[] { collider.LowerCorner, collider.UpperCorner };

            if (this.direction.X >= 0)
            {
                tmin = (bounds[0].X - this.origin.X) / this.direction.X;
                tmax = (bounds[1].X - this.origin.X) / this.direction.X;
            }
            else
            {
                tmin = (bounds[1].X - this.origin.X) / this.direction.X;
                tmax = (bounds[0].X - this.origin.X) / this.direction.X;
            }
            if (this.direction.Y >= 0)
            {
                tymin = (bounds[0].Y - this.origin.Y) / this.direction.Y;
                tymax = (bounds[1].Y - this.origin.Y) / this.direction.Y;
            }
            else
            {
                tymin = (bounds[1].Y - this.origin.Y) / this.direction.Y;
                tymax = (bounds[0].Y - this.origin.Y) / this.direction.Y;
            }
            if ((tmin > tymax) || (tymin > tmax))
            {
                return(null);
            }

            if (tymin > tmin)
            {
                tmin = tymin;
            }
            if (tymax < tmax)
            {
                tmax = tymax;
            }
            if (this.direction.Z >= 0)
            {
                tzmin = (bounds[0].Z - this.origin.Z) / this.direction.Z;
                tzmax = (bounds[1].Z - this.origin.Z) / this.direction.Z;
            }
            else
            {
                tzmin = (bounds[1].Z - this.origin.Z) / this.direction.Z;
                tzmax = (bounds[0].Z - this.origin.Z) / this.direction.Z;
            }
            if ((tmin > tzmax) || (tzmin > tmax))
            {
                return(null);
            }
            if (tzmin > tmin)
            {
                tmin = tzmin;
            }
            if (tzmax < tmax)
            {
                tmax = tzmax;
            }
            return(tmin);
        }
コード例 #6
0
ファイル: PaddleBase.cs プロジェクト: DonkerNet/pong
 public void OnColliderCollision(IBoxCollider otherCollider, Vector2 intersection)
 {
     // Do nothing, since the ball will adjust itself instead
 }
コード例 #7
0
ファイル: Box.cs プロジェクト: banderous/Uniject
 public Box(TestableGameObject obj, IBoxCollider collider,
     [Resource("mesh/cube")] TestableGameObject cubeMesh)
 {
     this.Obj = obj;
     cubeMesh.transform.Parent = obj.transform;
 }
コード例 #8
0
ファイル: Box.cs プロジェクト: foujitailai/Uniject
 public Box(TestableGameObject obj, IBoxCollider collider,
            [Resource("mesh/cube")] TestableGameObject cubeMesh)
 {
     this.Obj = obj;
     cubeMesh.transform.Parent = obj.transform;
 }