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'm using a single sprite sheet image as the main texture for my breakout game. The image is this:

breakout_sprite_sheet

My code is a little confusing, since I'm creating two elements from the same Texture using a Point, to represent the element size and its position on the sheet, a Vector, to represent its position on the viewport and a Rectangle that represents the element itself.

Texture2D sheet;

Point paddleSize = new Point(112, 24);
Point paddleSheetPosition = new Point(0, 240);
Vector2 paddleViewportPosition;
Rectangle paddleRectangle;

Point ballSize = new Point(24, 24);
Point ballSheetPosition = new Point(160, 240);
Vector2 ballViewportPosition;
Rectangle ballRectangle;
Vector2 ballVelocity;

My initialization is a little confusing as well, but it works as expected:

paddleViewportPosition = new Vector2((GraphicsDevice.Viewport.Bounds.Width - paddleSize.X) / 2, GraphicsDevice.Viewport.Bounds.Height - (paddleSize.Y * 2));
            paddleRectangle = new Rectangle(paddleSheetPosition.X, paddleSheetPosition.Y, paddleSize.X, paddleSize.Y);

Random random = new Random();
ballViewportPosition = new Vector2(random.Next(GraphicsDevice.Viewport.Bounds.Width), random.Next(GraphicsDevice.Viewport.Bounds.Top, GraphicsDevice.Viewport.Bounds.Height / 2));
ballRectangle = new Rectangle(ballSheetPosition.X, ballSheetPosition.Y, ballSize.X, ballSize.Y);
ballVelocity = new Vector2(3f, 3f);

The problem is I can't detect the collision properly, using this code:

if(ballRectangle.Intersects(paddleRectangle))
{
    ballVelocity.Y = -ballVelocity.Y;
}

What am I doing wrong?

share|improve this question

closed as too localized by Byte56, Trevor Powell, bummzack, Josh Petrie, Nick Wiggill Dec 30 '12 at 12:21

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
You need to use the debugger and break on the line where you're trying to detect collision. Ensure your values are properly initialized. The debugger is very powerful and allow you to easily solve problems like this. You'll feel silly for asking questions like this once you learn to use it. –  Byte56 Dec 17 '12 at 22:57
    
In what line should I add a breakpoint? The one with the action inside the IF or the actual check used by the IF? –  Daniel Ribeiro Dec 17 '12 at 23:05
    
The one with the if statement first. Then you can check the values. You can also place one inside the statement to see when/if your code is getting called and why it might not be working the way you expect. Check this out. –  Byte56 Dec 17 '12 at 23:10
    
I know what breakpoints are and I'm used to use them on different platforms and environments. The thing is, I need to simulate the collision by moving the objects, since if I don't they won't collide at all. When I place a breakpoint on the checking of the IF statement, it breaks right on the first execution, and I can't access the game to control de paddle so I can touch the ball. Did you understand? –  Daniel Ribeiro Dec 17 '12 at 23:13
    
Yes, I'm familiar with this. You're first just trying to see if the rectangles are properly set up? Are they the right size? Next you want to see if they're moving with your objects. You can set conditions on your break point. Set it to trigger after a number of executions and make sure the rectangles are moving and in the correct positions. Additionally, you can add some visual debugging to your project. Set up some code that will draw the bounding rectangles on screen. –  Byte56 Dec 17 '12 at 23:20
show 1 more comment

1 Answer 1

up vote 3 down vote accepted

You don't include any code to indicate that you're moving the rectangles with your objects. Additionally, you're creating the rectangles at the position they are in the sprite sheet. You should be creating them at the position their parent objects are. In your case you should be creating them like:

ballRectangle = 
  new Rectangle(ballViewportPosition.X + ballSize.x/2f, ballViewportPosition.Y+ ballSize.y/2f, ballSize.X, ballSize.Y);

Then when moving the ball around, you should also be moving the rectangle around:

public void setBallPosition(int x, int y) {
    ballViewPortPosition.x = x;
    ballViewPortPosition.y = y;
    ballRectangle.x = ballViewportPosition.X + ballSize.x/2f;
    ballRectangle.y = ballViewportPosition.y + ballSize.y/2f;
}

The ballRectangle is used for collision. You should use something else for drawing the ball.

share|improve this answer
    
"The ballRectangle is used for collision. You should use something else for drawing the ball." Perfect comment! I completely understood that thing about source rectangles... Thank you very much for the help, and the precious debugging tips! –  Daniel Ribeiro Dec 17 '12 at 23:51
add comment

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