Yann
Yann

Reputation: 988

Create walls in XNA

So I'm creating a game in XNA using C#, and I have walls that are created in random positions, and I'm trying to stop things walking through them/be teleported to points on the screen when they hit them. (Note, Left and right now works, it's just the top and bottom)

if (collision(wallRect[k], wallColours, pacmanRect, pacmanColor, 0))
//Collision works, not an issue here
{
    if (pacmanRect.Bottom > wallRect[k].Top && pacmanRect.Top < wallRect[k].Bottom)
    {
        if (pacmanRect.Right >= wallRect[k].Left  
            && pacmanRect.Right < wallRect[k].Right)
        {
            pacmanPos.X = wallRect[k].X - frameSize.X; 
            //frameSize is the size of the pacman sprite
        }                                
        else if (pacmanRect.Left <= wallRect[k].Right 
                  && pacmanRect.Left > wallRect[k].Left)
        {
            pacmanPos.X = wallRect[k].X + frameSize.X / 8;
        }
    }
    else if (pacmanRect.Right > wallRect[k].Left && pacmanRect.Left < wallRect[k].Right)
    {
        if (pacmanRect.Bottom >= wallRect[k].Top)
        {
            pacmanPos.Y = wallRect[k].Y - frameSize.Y / 8;
        }                  
        else if (pacmanRect.Top <= wallRect[k].Bottom)
        {
             pacmanPos.Y = wallRect[k].Y + frameSize.Y / 8;
        }
   }
   playSound(collisionSoundInstance);
}

That is the last point in the game loop where pacmanPos is updated. So how would I make it so that the walls are actual walls, and you can't walk through them?

Upvotes: 0

Views: 1391

Answers (2)

Mark Hildreth
Mark Hildreth

Reputation: 43061

if (pacmanRect.Bottom > wallRect[k].Top && pacmanRect.Top < wallRect[k].Bottom)

Does this make sense? I don't think it's possible for Pacman to be above the top of the wall, but simultaneously below the bottom of it (unless maybe your y-coordinate system is positive in the down direction, and even then the logic still looks fishy)

I would recommend drawing one of the states out on paper, then going through your code in your mind line-by-line given what you see on your paper. For example, draw the state of Pacman's bottom-most edge overlapping the wall's top-most edge. Then walk through your code and see what happens.

Upvotes: 2

Felix K.
Felix K.

Reputation: 6281

If you have a game which is restricted to two dimensions you should use Box2D which makes your life much easier.

See comment if you want to stay to your own code

Upvotes: 0

Related Questions