コード例 #1
0
        public RectF_IntersectSide GetCollisionNormal(ComponentCollision other)
        {
            RectangleF otherCollider = other.Collider;


            return(Collider.IntersectSide(otherCollider));


            // bool b_l_Contact = otherCollider.Contains(new Point(Collider.Left, Collider.Bottom));
            // bool b_r_Contact = otherCollider.Contains(new Point(Collider.Right, Collider.Bottom));
            // bool t_l_Contact = otherCollider.Contains(new Point(Collider.Left, Collider.Top));
            // bool t_r_Contact = otherCollider.Contains(new Point(Collider.Right, Collider.Top));

            //if(b_r_Contact)
            //{
            //     if (b_l_Contact) return new Vector2(0, 1.0f);
            //     if (t_r_Contact) return new Vector2(-1.0f, 0);
            //}
            //else if(t_l_Contact)
            //{
            //     if (b_l_Contact) return new Vector2(1.0f, 0);
            //     if (t_r_Contact) return new Vector2(0, -1.0f);
            //}

            //Console.WriteLine("Collider contacts failed!");
            //return new Vector2(0, 1.0f);
        }
コード例 #2
0
        public override void Update(GameTime time)
        {
            float dt = (float)time.ElapsedGameTime.TotalSeconds;
            //ComponentMovement  movement  = GetSiblingComponent<ComponentMovement>();
            ComponentCollision collision = GetSiblingComponent <ComponentCollision>();
            ComponentTransform transform = GetSiblingComponent <ComponentTransform>();
            ComponentPhysics   physics   = GetSiblingComponent <ComponentPhysics>();

            bool inAir = true;

            //Vector2 oldPos = transform.Pos;
            //transform.Pos = new Vector2(oldPos.X + (Velocity.X * dt), oldPos.Y + (Velocity.Y * dt));


            //Only deal with one axis at a time
            #region newcollision

            bool right = (physics.Velocity.X >= 0);
            transform.Pos += new Vector2(physics.Velocity.X * dt, 0.0f);
            foreach (ComponentCollision collider in ComponentCollision.ColliderList)
            {
                if (collision != null && collider != null && collider != collision)
                {
                    //RectF_IntersectSide side = collision.GetCollisionNormal(collider);

                    bool collided = collider.Collider.Contains(((right) ? collision.Collider.RightMid : collision.Collider.LeftMid));
                    //(
                    //    (
                    //    collider.Collider.Contains(
                    //    ((right)? collision.Collider.TopRight : collision.Collider.TopLeft))
                    //    &&
                    //    ! collider.Collider.Contains(
                    //    ((!right) ? collision.Collider.TopRight : collision.Collider.TopLeft))
                    //    )
                    //||
                    //    (
                    //    collider.Collider.Contains(
                    //    ((right) ? collision.Collider.BottomRight : collision.Collider.BottomLeft))
                    //    &&
                    //    !collider.Collider.Contains(
                    //    ((!right) ? collision.Collider.BottomRight : collision.Collider.BottomLeft))
                    //    )
                    //);

                    if (collided)//(side == ((right)? RectF_IntersectSide.Right : RectF_IntersectSide.Left))
                    {
                        //Transform worldTrans = transform.WorldTransform;
                        float xDiff = ((right) ? collision.GetOverlapAmountRight(collider) : collision.GetOverlapAmountLeft(collider));


                        transform.Pos   += new Vector2(((right) ? -xDiff : xDiff), 0.0f);
                        physics.Velocity = new Vector2(0.0f, ((physics.InAir) ? physics.Velocity.Y : 0.0f));
                        break;
                    }
                }
            }


            bool down = (physics.Velocity.Y >= 0);
            transform.Pos += new Vector2(0.0f, physics.Velocity.Y * dt);
            foreach (ComponentCollision collider in ComponentCollision.ColliderList)
            {
                if (collision != null && collider != null && collider != collision)
                {
                    //RectF_IntersectSide side = collision.GetCollisionNormal(collider);

                    //bool collided = collider.Collider.Contains(
                    //                       ((down) ? collision.Collider.BottomRight : collision.Collider.TopRight))
                    //                       &&
                    //                       collider.Collider.Contains(
                    //                       ((down) ? collision.Collider.BottomLeft : collision.Collider.TopLeft));



                    bool collided = collider.Collider.Contains(((down) ? collision.Collider.BottomMid : collision.Collider.TopMid));

                    //   (
                    //    (
                    //    collider.Collider.Contains(
                    //    ((down) ? collision.Collider.BottomRight : collision.Collider.TopRight))
                    //    &&
                    //    !collider.Collider.Contains(
                    //    ((!down) ? collision.Collider.BottomRight : collision.Collider.TopRight))
                    //    )
                    //||
                    //    (
                    //    collider.Collider.Contains(
                    //    ((down) ? collision.Collider.BottomLeft : collision.Collider.TopLeft))
                    //    &&
                    //    !collider.Collider.Contains(
                    //    ((!down) ? collision.Collider.BottomLeft : collision.Collider.TopLeft))
                    //    )
                    // );

                    if (collided)//(side == ((down) ? RectF_IntersectSide.Bottom : RectF_IntersectSide.Top))
                    {
                        if (down)
                        {
                            inAir = false;
                        }
                        //Transform worldTrans = transform.WorldTransform;
                        float yDiff = ((down) ? collision.GetOverlapAmountBottom(collider) : collision.GetOverlapAmountTop(collider));
                        transform.Pos   += new Vector2(0.0f, ((down) ? -yDiff : yDiff));
                        physics.Velocity = new Vector2(((physics.InAir) ? 0.0f : physics.Velocity.X), 0.0f);
                        break;
                    }
                }
            }



            #endregion



            //_sidesCollided[0] = false;
            //_sidesCollided[1] = false;
            //_sidesCollided[2] = false;
            //_sidesCollided[3] = false;

            //_collidedRects[0] = null;
            //_collidedRects[1] = null;
            //_collidedRects[2] = null;
            //_collidedRects[3] = null;


            //foreach (ComponentCollision collider in ComponentCollision.ColliderList)
            //{
            //    if(collision != null && collider != null && collider != collision)
            //    {
            //        if (collision.IsCollided(collider))
            //        {
            //            Transform worldTrans = transform.WorldTransform;
            //            //Vector2 vecBack = _lastPos - worldTrans.Translate;

            //            RectF_IntersectSide dirCollision = collision.Collider.IntersectSide(collider.Collider);

            //            switch (dirCollision)
            //            {
            //                case RectF_IntersectSide.Left:
            //                    //transform.Pos += new Vector2((collision.GetOverlapAmountRight(collider)), 0.0f);
            //                    //physics.Velocity = new Vector2(0, ((physics.InAir) ? 0.0f : physics.Velocity.Y)); //((physics.WasInAirLastFrame) ? 0.0f : physics.Velocity.Y)
            //                    _sidesCollided[0] = true;
            //                    _collidedRects[0] = collider.Collider;
            //                    break;
            //                case RectF_IntersectSide.Right:
            //                    //transform.Pos += new Vector2(-(collision.GetOverlapAmountRight(collider)), 0.0f);
            //                    //physics.Velocity = new Vector2(0, ((physics.InAir) ? 0.0f : physics.Velocity.Y));
            //                    _sidesCollided[2] = true;
            //                    _collidedRects[2] = collider.Collider;
            //                    break;
            //                case RectF_IntersectSide.Inside:
            //                    //transform.Pos = LastPos + (physics.Velocity.MultiplyLength(.5f) * (float)time.ElapsedGameTime.TotalSeconds);
            //                    //physics.Velocity = physics.Velocity;
            //                    transform.Pos -= new Vector2(0.0f, (collision.GetOverlapAmountBottom(collider)));
            //                    //physics.Velocity = new Vector2(0, 0);
            //                    //physics.AccelerationX = 0.0f;
            //                    //physics.AccelerationY = 0.0f;

            //                    //physics.InAir = false;
            //                    break;
            //                case RectF_IntersectSide.Bottom:
            //                    //float temp = ((physics.InAir) ? 0.0f : physics.Velocity.X);
            //                    _sidesCollided[3] = true;
            //                    _collidedRects[3] = collider.Collider;

            //                    //transform.Pos += new Vector2(0.0f, -(collision.GetOverlapAmountBottom(collider)));
            //                    //physics.Velocity = new Vector2(((physics.InAir) ? 0.0f : physics.Velocity.X), 0.0f);
            //                    break;
            //                case RectF_IntersectSide.Top:
            //                    _sidesCollided[1] = true;
            //                    _collidedRects[1] = collider.Collider;

            //                    //transform.Pos += new Vector2(0.0f, (collision.GetOverlapAmountBottom(collider)));
            //                    //physics.Velocity = new Vector2(((physics.InAir) ? 0.0f : physics.Velocity.X), 0.0f);
            //                    break;
            //                default:
            //                    break;
            //            }
            //            //RectF_IntersectSide checkBelowResult = new RectangleF(collider.Collider, 0.0f, 0.1f).IntersectSide(collision.Collider);
            //            inAir = !(/*checkBelowResult == RectF_IntersectSide.Bottom || */dirCollision == RectF_IntersectSide.Inside);

            //        }
            //    }
            //}

            ////Left
            //if (_sidesCollided[0])
            //{
            //    transform.Pos += new Vector2((collision.GetDistanceLeft(_collidedRects[0])),0.0f);
            //    physics.Velocity = new Vector2(0.0f, ((physics.InAir) ? 0.0f : physics.Velocity.Y));
            //}
            ////Top
            //if (_sidesCollided[1])
            //{
            //    //float temp = ((physics.InAir) ? 0.0f : physics.Velocity.X);
            //    transform.Pos += new Vector2(0.0f, (collision.GetDistanceTop(_collidedRects[1])));
            //    physics.Velocity = new Vector2(((physics.InAir) ? 0.0f : physics.Velocity.X), 0.0f);
            //}
            ////Right
            //if (_sidesCollided[2])
            //{
            //    transform.Pos += new Vector2(-(collision.GetDistanceRight(_collidedRects[2])), 0.0f);
            //    physics.Velocity = new Vector2(0, ((physics.InAir) ? 0.0f : physics.Velocity.Y));
            //}
            ////Bottom
            //if (_sidesCollided[3])
            //{
            //    //float temp = ((physics.InAir) ? 0.0f : physics.Velocity.X);
            //    transform.Pos += new Vector2(0.0f, -(collision.GetDistanceBottom(_collidedRects[3])));
            //    physics.Velocity = new Vector2(((physics.InAir) ? 0.0f : physics.Velocity.X), 0.0f);
            //}

            physics.InAir = inAir;

            LastPos = transform.Pos;
        }
コード例 #3
0
 public float GetDistanceLeft(ComponentCollision other)
 {
     return(Fabs(GetOverlapAmountLeft(other)));
 }
コード例 #4
0
 public float GetDistanceBottom(ComponentCollision other)
 {
     return(Fabs(GetOverlapAmountBottom(other)));
 }
コード例 #5
0
 public float GetOverlapAmountLeft(ComponentCollision other)
 {
     return(other.Collider.Right - Collider.Left);
 }
コード例 #6
0
 public float GetOverlapAmountTop(ComponentCollision other)
 {
     return(other.Collider.Bottom - Collider.Top);
 }
コード例 #7
0
 public bool IsCollided(ComponentCollision other)
 {
     return(Collider.Intersects(other.Collider));
 }