Uffe Puffe
Uffe Puffe

Reputation: 105

Animation with Collision?

I have a player class, which is a mario character. When I walk to the left, I call a method that starts the Left animation and sets the speed.

Now here's my problem: How would I go making the collision rectangle for the player? This is my rectangle: rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);

Which uses my currentFrame variable, and the frameWidth and Height.

I also have a RectanlgeHelper class which looks like this:

public static class RectangleHelper
{

    public static bool TouchTopOf(this Rectangle r1, Rectangle r2)
    {
        return (r1.Bottom >= r2.Top - 1 &&
                r1.Bottom <= r2.Top + (r2.Height / 2) &&
                r1.Right >= r2.Left + r2.Width / 5 &&
                r1.Left <= r2.Right - r2.Height / 6);
    }

    public static bool TouchBottomOf(this Rectangle r1, Rectangle r2)
    {
        return (r1.Top <= r2.Bottom + (r2.Height / 5) &&
                r1.Top >= r2.Bottom - 1 &&
                r1.Right >= r2.Left + r2.Width / 5 &&
                r1.Left <= r2.Right - r2.Width / 5);
    }

    public static bool TouchLeftOf(this Rectangle r1, Rectangle r2)
    {
        return (r1.Right <= r2.Right &&
                r1.Right >= r2.Left - 5 &&
                r1.Top <= r2.Bottom - (r2.Width / 4) &&
                r1.Bottom >= r2.Top + (r2.Width / 4));  
    }

    public static bool TouchRightOf(this Rectangle r1, Rectangle r2)
    {
        return (r1.Left >= r2.Right &&
                r1.Left <= r2.Right + 5 &&
                r1.Top <= r1.Bottom - (r2.Width / 4) &&
                r1.Bottom >= r2.Top + (r2.Width / 4));
    }
}

And in my Tile class, which draws the tiles on the map :

class Tiles
{
    protected Texture2D texture;

    private Rectangle rectangle;

    public Rectangle Rectangle
    {
        get { return rectangle; }
        protected set { rectangle = value; }
    }

    private static ContentManager content;
    public static ContentManager Content
    {
        protected get { return content; }
        set { content = value; }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, rectangle, Color.White);
    }

}

class CollisionTiles : Tiles
{
    public CollisionTiles(int i, Rectangle newRectangle)
    {
        texture = Content.Load<Texture2D>("Tiles/Tile" + i);
        this.Rectangle = newRectangle;
    }
}

And if necessary, my Map class, which generates the map/level:

class Map
{
    private List<CollisionTiles> collisionTiles = new List<CollisionTiles>();

    public List<CollisionTiles> CollisionTiles
    {
        get { return collisionTiles; }
    }

    private int width, height;
    public int Width
    {
        get { return width; }
    }
    public int Height
    {
        get { return height; }
    }

    public Map() { }

    public void Generate(int[,] map, int size)
    {
        for (int x = 0; x < map.GetLength(1); x++)
            for (int y = 0; y < map.GetLength(0); y++)
            {
                int number = map[y, x];

                if (number > 0)
                {
                    CollisionTiles.Add(new CollisionTiles(number, new Rectangle(x * size, y * size, size, size)));

                    width = (x + 1) * size;
                    height = (y + 1) * size;
                }
            }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        foreach (CollisionTiles tile in collisionTiles)
            tile.Draw(spriteBatch);
    }
}

So how would I go making another rectangle in my player class, so it can use collisions?

Thanks in advance, if you need to know anything more, tell me.

Upvotes: 0

Views: 165

Answers (1)

joppiesaus
joppiesaus

Reputation: 5760

rectangle = new Rectangle(playerPosition.X, playerPosition.Y, playerTexture.Width, playerTexture.Height);

When you want to detect collisions, do this in the update void:

player.animationRectangle.X = player.rectangle.X;
player.animationRectangle.Y = player.rectangle.Y;
foreach (CollistionTiles tile in map.CollisionTiles)
{
   if (player.rectangle.TouchLeftOf(tile.Rectangle))
   {
      //touched left of tile
   }
}

EDIT: in your player class, create two members:

Rectangle rectangle;
Rectangle animationRectangle;

rectangle gets used for collision, NOT for drawing. animationRectangle gets used for displaying animations, and for drawing. So you draw only the animationRectangle.

Upvotes: 2

Related Questions