예제 #1
0
        public void SetParent(Node parent)
        {
            if (this.parent != null)
                this.parent.children.Remove(this);

            this.parent = parent;

            if (parent != null)
                parent.children.Add(this);
        }
예제 #2
0
        public static bool CheckQuad(Node nodeA, Node nodeB)
        {
            if ((nodeA.Position.X + nodeA.Sprite.Width < 800 && nodeB.Position.X < 800
                || nodeB.Position.X + nodeB.Sprite.Width < 800 && nodeA.Position.X < 800) ||
                (nodeA.Position.X + nodeA.Sprite.Width >= 800 && nodeB.Position.X >= 800
                || nodeB.Position.X + nodeB.Sprite.Width >= 800 && nodeA.Position.X >= 800))
            {
                if ((nodeA.Position.Y + nodeA.Sprite.Height < 480 && nodeB.Position.Y < 480
                || nodeB.Position.Y + nodeB.Sprite.Height < 480 && nodeA.Position.Y < 480) ||
                (nodeA.Position.Y + nodeA.Sprite.Height >= 480 && nodeB.Position.Y >= 480
                || nodeB.Position.Y + nodeB.Sprite.Height >= 480 && nodeA.Position.Y >= 480))
                {

                    if (Vector2.Distance(nodeA.Position, nodeB.Position) < (nodeA.Sprite.Width / 2 + nodeB.Sprite.Width / 2))
                        return true;
                    else
                        return false;
                }
                else
                    return false;
            }
            else
                return false;
        }
예제 #3
0
        public static bool CheckCollision(Node nodeA, Node nodeB)
        {
            line = nodeB.Position - nodeA.Position;

            // we use LengthSquared to avoid a costly square-root call
            return (line.LengthSquared() <= (nodeA.Sprite.Width / 2 + nodeB.Sprite.Width / 2) * (nodeA.Sprite.Width / 2 + nodeB.Sprite.Width / 2));


            //if(CheckQuad(nodeA, nodeB))
            //{

             //if (Vector2.Distance(nodeA.Position, nodeB.Position) < (nodeA.Sprite.Width / 2 + nodeB.Sprite.Width / 2))
             //    return true;
             // else
             //    return false;

            //}
            //else
            //    return false;

            //if ((nodeA.position.X + nodeA.Sprite.Width < 800 && nodeB.position.X < 800
            //    || nodeB.position.X + nodeB.Sprite.Width < 800 && nodeA.position.X < 800) ||
            //    (nodeA.position.X + nodeA.Sprite.Width >= 800 && nodeB.position.X >= 800
            //    || nodeB.position.X + nodeB.Sprite.Width >= 800 && nodeA.position.X >= 800))
            //{
            //    if ((nodeA.position.Y + nodeA.Sprite.Height < 480 && nodeB.position.Y < 480
            //    || nodeB.position.Y + nodeB.Sprite.Height < 480 && nodeA.position.Y < 480) ||
            //    (nodeA.position.Y + nodeA.Sprite.Height >= 480 && nodeB.position.Y >= 480
            //    || nodeB.position.Y + nodeB.Sprite.Height >= 480 && nodeA.position.Y >= 480))
            //    {

            //        if (Vector2.Distance(nodeA.position, nodeB.position) < (nodeA.Sprite.Width / 2 + nodeB.Sprite.Width / 2))
            //            return true;
            //        else
            //            return false;
            //    }
            //    else
            //        return false;
            //}
            //else
            //    return false;




            //if (CheckQuad(nodeA, nodeB))
            //{

            //    Rectangle collisionRect = Intersect(nodeA.Bounds, nodeB.Bounds);

            //    if (collisionRect == Rectangle.Empty)
            //        return false;

            //    int pixelCount = collisionRect.Width * collisionRect.Height;

            //    Color[] pixelsA = new Color[pixelCount];
            //    Color[] pixelsB = new Color[pixelCount];

            //    //GraphicsDevice.Textures[0] = null;
            //    nodeA.Sprite.Texture.GetData<Color>(0, nodeA.NormalizeBounds(collisionRect), pixelsA, 0, pixelCount);
            //    nodeB.Sprite.Texture.GetData<Color>(0, nodeB.NormalizeBounds(collisionRect), pixelsB, 0, pixelCount);

            //    for (int i = 0; i < pixelCount; i++)
            //    {
            //        if (pixelsA[i].A > ALPHA_THRESHOLD && pixelsB[i].A > ALPHA_THRESHOLD)
            //            return true;
            //    }
            //    return false;
            //}
            //else
            //    return false;
        }
예제 #4
0
        public void Bounce(Node node1, Node node2)
        {

            Vector2 cOfMass = (node1.Velocity + node2.Velocity) / 2;

            Vector2 normal1 = node2.Position - node1.Position;
            normal1.Normalize();
            Vector2 normal2 = node1.Position - node2.Position;
            normal2.Normalize();

            node1.Velocity -= cOfMass;
            node1.Velocity = Vector2.Reflect(node1.Velocity, normal1);
            node1.Velocity += cOfMass;

            node2.Velocity -= cOfMass;
            node2.Velocity = Vector2.Reflect(node2.Velocity, normal2);

            node2.Velocity += cOfMass;
        }