Wednesday, March 23, 2016

xna - Why would you use a negative value for Bounding Box?


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));


Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...