Beispiel #1
0
        /// <summary>
        /// Places the player randomly on the battlefield.
        /// If the player is inside the terrain, it is moved up until it is clear.
        /// If no clear spot can be found, try a new random spot
        /// </summary>
        /// <param name="p">The player.</param>
        public void PlacePlayer(Player p)
        {
            p.Velocity     = Vector2.Zero;
            p.Acceleration = Vector2.Zero;
            Vector2 pos = new Vector2(rand.Next(Terrain.Width), rand.Next(Terrain.Height));

            if (Terrain.IsPositionClear(pos + p.Origin))
            {
                p.Position     = pos;
                p.IsStandingOn = GroundTypes.Air;
            }
            else
            {
                int counter = 0;
                do
                {
                    pos.Y = (pos.Y - 1) % Terrain.Height;
                    counter++;
                    if (counter >= Terrain.Height)
                    {
                        pos     = new Vector2(rand.Next(Terrain.Width), rand.Next(Terrain.Height));
                        counter = 0;
                    }
                } while (!Terrain.IsPositionClear(pos + p.Origin));
                p.Position     = pos;
                p.IsStandingOn = GroundTypes.Ground;
            }
        }
Beispiel #2
0
        public static bool IntersectPixels(Matrix transformA, int widthA, int heightA, Color[] dataA, Terrain terrain, out Vector2 collisionPoint)
        {
            // Calculate a matrix which transforms from A's local space into
            // world space and then into B's local space
            // When a point moves in A's local space, it moves in B's local space with a
            // fixed direction and distance proportional to the movement in A.
            // This algorithm steps through A one pixel at a time along A's X and Y axes
            // Calculate the analogous steps in B:
            Vector2 stepX = Vector2.TransformNormal(Vector2.UnitX, transformA);
            Vector2 stepY = Vector2.TransformNormal(Vector2.UnitY, transformA);

            // Calculate the top left corner of A in B's local space
            // This variable will be reused to keep track of the start of each row
            Vector2 yPosInB = Vector2.Transform(Vector2.Zero, transformA);

            // For each row of pixels in A
            for (int yA = 0; yA < heightA; yA++)
            {
                // Start at the beginning of the row
                Vector2 posInB = yPosInB;

                // For each pixel in this row
                for (int xA = 0; xA < widthA; xA++)
                {
                    // Round to the nearest pixel
                    int xB = (int)Math.Round(posInB.X);
                    int yB = (int)Math.Round(posInB.Y);

                    // If the pixel lies within the bounds of B
                    if (0 <= xB && xB < terrain.Width &&
                        0 <= yB && yB < terrain.Height)
                    {
                        // Get the colors of the overlapping pixels
                        Color colorA = dataA[xA + yA * widthA];

                        // If both pixels are not completely transparent,
                        if (colorA.A != 0 && !terrain.IsPositionClear(new Vector2(xB, yB)))
                        {
                            // then an intersection has been found
                            collisionPoint = Vector2.Transform(new Vector2(xA, yA), transformA);
                            return(true);
                        }
                    }

                    // Move to the next pixel in the row
                    posInB += stepX;
                }

                // Move to the next row
                yPosInB += stepY;
            }

            // No intersection found
            collisionPoint = new Vector2(-1, -1);
            return(false);
        }