• Create Account

## Rectangle

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1Freemountain  Members

108
Like
0Likes
Like

Posted 30 August 2012 - 12:40 PM

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

385
Like
0Likes
Like

Posted 30 August 2012 - 12:59 PM

I read the question wrong.. I agree with WarAmp.

Edited by ShadowValence, 30 August 2012 - 01:02 PM.

### #3WarAmp  Members

750
Like
1Likes
Like

Posted 30 August 2012 - 01:00 PM

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
Waramp.Before you insult a man, walk a mile in his shoes.That way, when you do insult him, you'll be a mile away, and you'll have his shoes.

### #4Freemountain  Members

108
Like
0Likes
Like

Posted 30 August 2012 - 01:05 PM

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, 30 August 2012 - 01:12 PM.

### #5fdn2012  Members

126
Like
0Likes
Like

Posted 30 August 2012 - 01:13 PM

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

385
Like
3Likes
Like

Posted 30 August 2012 - 01:25 PM

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, 30 August 2012 - 01:26 PM.

### #7Freemountain  Members

108
Like
1Likes
Like

Posted 30 August 2012 - 01:30 PM

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.

385
Like
0Likes
Like

Posted 30 August 2012 - 01:59 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.