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 have the following code I'm working with in XNA from the book Professional XNA Programming.

It's just pong on a 2d screen plane. We use bounding boxes for collision detection. I'm just having trouble understanding why we would use a negative paddle size for the RedPaddleBox. I read bounding boxes define the minimum and maximum corners of the box, but I'm not sure what that means--minimum in terms of coordinate values? If so, why use a negative value when the paddle isn't partly off screen?

This is the code line that gave me trouble:

            new Vector3(-paddleSize.X/2,

in context of:

        BoundingBox leftPaddleBox = new BoundingBox(
            new Vector3(-paddleSize.X/2,
                leftPaddlePosition-paddleSize.Y/2, 0),
            new Vector3(+paddleSize.X/2,
                leftPaddlePosition+paddleSize.Y/2, 0));

in the following code:

        // Check for collisions with the paddles.
        // Construct bounding boxes to use the intersection helper method.
        Vector2 ballSize = new Vector2(
            GameBallRect.Width / 1024.0f, GameBallRect.Height / 768.0f);
        BoundingBox ballBox = new BoundingBox(
            new Vector3(ballPosition.X - ballSize.X / 2,
                ballPosition.Y - ballSize.Y / 2, 0),
            new Vector3(ballPosition.X + ballSize.X / 2,
                ballPosition.Y + ballSize.Y / 2, 0));
        Vector2 paddleSize = new Vector2(
            GameRedPaddleRect.Width / 1024.0f,
            GameRedPaddleRect.Height / 768.0f);
        BoundingBox leftPaddleBox = new BoundingBox(
            new Vector3(-paddleSize.X/2,
                leftPaddlePosition-paddleSize.Y/2, 0),
            new Vector3(+paddleSize.X/2,
                leftPaddlePosition+paddleSize.Y/2, 0));
        BoundingBox rightPaddleBox = new BoundingBox(
            new Vector3(1-paddleSize.X/2,
                rightPaddlePosition+paddleSize.Y/2, 0),
            new Vector3(1+paddleSize.X/2,
                rightPaddlePosition+ paddleSize.Y/2,0));
share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

Mathematically, it's super-convenient when ranges go from -1 to +1 ([-1,+1]). All kinds of formulas and such are just way more powerful if you make that assumption, one reason being that when scaled it has further mathemetical meaning than other arbitrary coordinate systems (like using a corner). It can hence be useful for a bounding box to be represented as <-1,-1> to <+1,+1> scaled by the half-sizes of the box with (0,0) being the center of the box.

That is, for a box with dimensions <2,4>, you'd give it the min/max vectors <-2/2,-4/2>=<-1,-2> to <+2/2,+4/2>=<+1,+2>, dividing by 2 because you want the half-sizes here. You use the half-sizes because that represents a width of +1-(-1)=2 and a height +2-(-2)=4, which are the original dimensions, and they nicely represent that [-1,+1] scaled up to represent those dimensions.

Now you might want to also encode the object's position with its bounding box in some cases. Or you might want to store it separately. Depends on what specifically you use the bounding box for. It's certainly more common to add the object's position to its extents to change from offsets from the object's center into points representing the outer vertices of the object in the world. Sometimes this is not done, though.

Part of understanding this is understanding coordinate system. Positions are given relative to some other position. If I tell you I'm 5 miles East of a particular landmark then you need to know where that landmark is in order to know where I'm at. Hence you can give coordinates in a game relative to an object, relative to some arbitrary "center" of the level, or whatever else you want. A simple bounding box's extents are relative to the center of the object. In order to know where those corners actually are, however, you must know where the box is. That's why you calculate the extents of the box as if the center is at (0,0) (object-space or model-space) and can translate them to world-space.

share|improve this answer
    
"Positions are given relative to some other position" That alone made everything make so much more sense, thank you. –  Andy Isbell Jun 16 '13 at 9:13
add comment

If we take the paddle position as defining the center of the paddle:

To find the top edge we need to find the value that is half of the paddle height less than that center point.

This code achieves that goal:

leftPaddlePosition-paddleSize.Y/2

Note: I think there is a typo in the code for the Y-axis component of the first Vector3 used to define the rightPaddleBox, as I read it the '+' should be a '-'.

Edit:

The left paddle's horizontal range is 0-paddleSize.X/2 to 0+paddleSize.X/2

The right paddle's horizontal range is 1-paddleSize.X/2 to 1+paddleSize.X/2

They will both extend beyond the screen but will appear symmetrical to the player. The author may have done this so the code is repetitive/formulaic and may be easier to understand.

In my opinion the (implied) 0 for the left paddle and the 1 for the right should be variables rather than magic numbers, this way it would be easier to shift them if you wanted.

share|improve this answer
    
Yeah, that makes sens. I just have trouble understanding this line: ` BoundingBox leftPaddleBox = new BoundingBox( new Vector3(-paddleSize.X/2,` Specifically the first argument to Vector3() –  Andy Isbell Jun 16 '13 at 7:40
add comment

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.