# Rectangle

This topic is 1970 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone!

I've just started programming XNA again and did not remember much about it, I didnt really complete any games.. Anyway, I ran in to some problem today, I'm trying to create a pong game and the bounces are kind of wierd. It seems like the rectangle is not a rectangle, more like a 1px dot on the top left corner, The ball is 20x20px size and the rectangle code looks like this:
ballrec = new Rectangle((int)ballvec.X, (int)ballvec.Y, ball.Width, ball.Height);
And this is the Draw section:
spritebatch.Draw(ball,ballvec,ballrec,Color.White,0,new Vector2(0,0),1,SpriteEffects.None,0);

when it bounces the ball only reacts to the top left corner, what am I doing wrong here?

/Freemountain

##### Share on other sites
I read the question wrong.. I agree with WarAmp. Edited by ShadowValence

##### Share on other sites
You will need to give us more than just your drawing code if you want us to help with a collision issue

It's likely that you are only testing ballvec.X/Y in your collision routine rather than the whole Rectangle, show us how you are determining the collision and 'bounce' and we can help you further

##### Share on other sites

Alright, I'm using
spritebatch.Draw(ball, ballvec, Color.White);
right now, and the problem is still there.

here's some more code.
 int maxX = GraphicsDevice.Viewport.Width; int maxY = GraphicsDevice.Viewport.Height; ballrec = new Rectangle((int)ballvec.X, (int)ballvec.Y, ball.Width, ball.Height); //check for bounce if (ballvec.X > maxX || ballvec.X < 0) ballspeed.X *= -1; if (ballvec.Y > maxY || ballvec.Y < 0) ballspeed.Y *= -1; 

As I said before, the ball is 20x20px and i want the recangle to be 20x20 aswell.

EDIT:
changed "ballvec" to "ballrec" in "check for bounce" and the ball did not even bounce, just keept going off screen. Edited by Freemountain

##### Share on other sites
Hello.
ballrec = new Rectangle((int)ballvec.X, (int)ballvec.Y, ball.Width, ball.Height);
ballrec mean you want draw part of ball, In ball's coordinate system.
Hehe. You try this:
1.ballrec = new Rectangle(0, 0, ball.Width, ball.Height);
2.ballrec = new Rectangle(10,10, ball.Width, ball.Height);
3.ballrec = new Rectangle(20,20, ball.Width, ball.Height);

##### Share on other sites
It looks to me like WarAmp was correct in his response. Your logic is only checking a single point of the 20 pixel ball (X, Y). To correct for this you'll have to extend your logic to include the size.

Try something like this:
[source lang="csharp"]if (ballvec.X + ballrec.Width > maxX || ballvec.X < 0)
ballspeed.X *= -1;

if (ballvec.Y + ballrec.Height > maxY || ballvec.Y < 0)
ballspeed.Y *= -1;[/source]

This makes sure that the furthest part of the ball (not just its current position) is taken into consideration (by adding the width and height). Edited by ShadowValence

##### Share on other sites

It looks to me like WarAmp was correct in his response. Your logic is only checking a single point of the 20 pixel ball (X, Y). To correct for this you'll have to extend your logic to include the size.

Try something like this:
[source lang="csharp"]if (ballvec.X + ballrec.Width > maxX || ballvec.X < 0)
ballspeed.X *= -1;

if (ballvec.Y + ballrec.Height > maxY || ballvec.Y < 0)
ballspeed.Y *= -1;[/source]

This makes sure that the furthest part of the ball (not just its current position) is taken into consideration (by adding the width and height).

Thank you, it worked. If I study the code you write it all make sense. i hope i get the collision with ball -> paddle correct aswell now
Thanks once more.

##### Share on other sites
As long as you remember to include the size [width & height] of the paddle and the ball in your collision detection, you'll be fine.