I'm using a single sprite sheet image as the main texture for my breakout game. The image is this:


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:

    ballVelocity.Y = -ballVelocity.Y;

What am I doing wrong?

  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Dec 17 '12 at 22:57
  • \$\begingroup\$ In what line should I add a breakpoint? The one with the action inside the IF or the actual check used by the IF? \$\endgroup\$ – Daniel Ribeiro Dec 17 '12 at 23:05
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Dec 17 '12 at 23:10
  • \$\begingroup\$ 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? \$\endgroup\$ – Daniel Ribeiro Dec 17 '12 at 23:13
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Dec 17 '12 at 23:20

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.

  • \$\begingroup\$ "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! \$\endgroup\$ – Daniel Ribeiro Dec 17 '12 at 23:51

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