Beispiel #1
0
        public CollisionDirection CheckCollision(PhysicsAble entity)
        {
            Entity e = this;

            Vector2 position = entity.GetPhysicsPosition() + entity.GetPhysicsVelocity();
            Vector2 entitySize = entity.GetPhysicsSize();

            if (entitySize == Vector2.Zero)
            {
                return CollisionDirection.NONE;
            }

            int half_x_bounds = (int)entitySize.X / 2;
            int half_y_bounds = (int)entitySize.Y / 2;

            Rectangle checkingBounds = new Rectangle((int)position.X - half_x_bounds, (int)position.Y - half_y_bounds, (int)entitySize.X, (int)entitySize.Y);

            bool left = leftRect.Intersects(checkingBounds);
            bool right = rightRect.Intersects(checkingBounds);
            bool top = topRect.Intersects(checkingBounds);
            bool bottom = bottomRect.Intersects(checkingBounds);

            if (left && top)
            {
                return CollisionDirection.TOPLEFT;
            }
            else if (right && top)
            {
                return CollisionDirection.BOTTOMRIGHT;
            }
            else if (bottom && right)
            {
                return CollisionDirection.BOTTOMRIGHT;
            }
            else if (bottom && left)
            {
                return CollisionDirection.BOTTOMLEFT;
            }
            else if (top)
            {
                return CollisionDirection.TOP;
            }
            else if (bottom)
            {
                return CollisionDirection.BOTTOM;
            }
            else if (left)
            {
                return CollisionDirection.LEFT;
            }
            else if (right)
            {
                return CollisionDirection.RIGHT;
            }
            else
            {
                return CollisionDirection.NONE;
            }
        }
Beispiel #2
0
        public CollisionDirection CheckCollision(PhysicsAble entity)
        {
            // Assuming for now position and bounds defined in pixel space, this should be easy to switch out if needed
            Vector2 position = entity.GetPhysicsPosition() + entity.GetPhysicsVelocity();
            Vector2 size = entity.GetPhysicsSize();

            float half_scaled_bg_w = levelSize.X / 2;
            float half_scaled_bg_h = levelSize.Y / 2;
            Vector2 levelPosition = this.worldPosition - new Vector2(half_scaled_bg_w, half_scaled_bg_h);
            Vector2 characterInLevel = position - levelPosition;

            // Define region of pixels under the bounds
            int left = Math.Max(0, (int)Math.Floor((characterInLevel.X - size.X / 2) / levelSize.X * bitmapWidth));
            int width = (int)Math.Ceiling(size.X / levelSize.X * bitmapWidth);
            int top = Math.Max(0, (int)Math.Floor((characterInLevel.Y - size.Y / 2) / levelSize.Y * bitmapHeight));
            int height = (int)Math.Ceiling(size.Y / levelSize.Y * bitmapHeight);

            bool hitTop = false;
            bool hitLeft = false;
            bool hitRight = false;
            bool hitBottom = false;

            for (int x = left; x < left + width; x++)
            {
                for (int y = top; y < top + height; y++)
                {

                    int index = y * bitmapWidth + x;

                    // TODO: shouldn't get too far ahead of myself
                    if (index < bitmap.Length && bitmap[index])
                    {
                        if (x - left <= width / 2) hitLeft = true;
                        if (x - left >= width / 2) hitRight = true;
                        if (y - top <= height / 2) hitTop = true;
                        if (y - top >= height / 2) hitBottom = true;
                    }
                    // Possible: delete if out of bounds

                }
            }

            if (hitLeft && hitTop)
            {
                return CollisionDirection.TOPLEFT;
            }
            else if (hitRight && hitTop)
            {
                return CollisionDirection.BOTTOMRIGHT;
            }
            else if (hitBottom && hitRight)
            {
                return CollisionDirection.BOTTOMRIGHT;
            }
            else if (hitBottom && hitLeft)
            {
                return CollisionDirection.BOTTOMLEFT;
            }
            else if (hitTop)
            {
                return CollisionDirection.TOP;
            }
            else if (hitBottom)
            {
                return CollisionDirection.BOTTOM;
            }
            else if (hitLeft)
            {
                return CollisionDirection.LEFT;
            }
            else if (hitRight)
            {
                return CollisionDirection.RIGHT;
            }
            else
            {
                return CollisionDirection.NONE;
            }
        }