Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I seem to be having this problem a lot, I'm still learning XNA / C# and well, trying to make a classic paddle and ball game. The problem I run into (and after debugging have no answer) is that everytime I run my game and press either of the movement keys, the Paddle won't move. Debugging shows that it never gets to the movement part, but I can't understand why not?

Here's my code:

    // This is the If statement for checking Left movement.
    if (keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.A))
        {
            if (!CheckCollision(walls[0]))
            {
                Location.X -= Velocity;
            }
        }

    //This is the CheckCollision(Wall wall) boolean
    public bool CheckCollision(Wall wall)
    {
        if (this.BoundingBox.Intersects(wall.BoundingBox))
        {
            return true;
        }
        return false;
    }

As far as I can tell there should be absolutely no problem with this, I initialize the bounding box in the constructor whenever a new instance of Walls and Paddle is created.

   this.BoundingBox = new Rectangle(0, 0, Sprite.Width, Sprite.Height);

Any idea as to why this isn't working?

I have previously succeeded with using the whole Location.X < Wall.Location.X + Wall.Texture.Width code... But to me that seems like too much coding if a simple boolean check could be done.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I'm not an XNA user, so this is based off of some very quick MSDN reading and general knowledge.

That constructor indicates that both bounding boxes are at the origin (0,0), which means they'll always be intersecting.

Generally you need to recalculate bounding boxes whenever an object's position changes, and of course correctly initialize the bounding boxes when the object is created.

Bounding boxes are usually stored as two vectors, a minimum position and a maximum position. XNA has a BoundingBox structure that does this. Using that, if you have the a position and a width/height, you can calculate a half vector as:

half = [width/2, height/2]

and then the min and max positions as:

min = position - half
max = position + half

If you want to keep using Rectangle, then you need to use the position instead of 0,0. Knowing that the upper-left corner of the rectangle is it's position (I think; again, not an XNA user), the "min" position is the rectangle's position:

half = [width/2, height/2]
min = position - half
rect = new Rectangle(min.x, min.y, width, height)

Of course you can just store your actual object positions as the upper-left corner and avoid the need to calculate a half-vector, but this is not ideal. A great deal of other things in game math are going to be a lot easier if your positions are centers rather than corners of bounding boxes.

share|improve this answer
    
Unless all your objects are the same uniform shape (either a square or a circle), of course. –  Raven Dreamer Nov 18 '12 at 6:19
    
So you're saying that I should rather use middle-point origin and the BoundingBox function within XNA as opposed to Rectangle? Thanks for the solution to the Rectangle problem though. –  Pieter Nov 18 '12 at 7:43
    
@Pieter: that's what I'm saying, yes. As a perfect example of where this helps, think of rotation. You will almost always want to rotate around the center of an object, which is harder if you are basing coordinates on a corner. If you fire a billet or explosion particle, you usually want it from the center. If you need a ray for line of sight tests, you want the center. If you're moving for path finding, you want the center. And so on. –  Sean Middleditch Nov 18 '12 at 19:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.