Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

Rectangle

Started by Juvelbagarn Aug 30, 2012 at 6:40 PM 6 replies 2.8k views
Original Post
Juvelbagarn
Juvelbagarn
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
WarAmp
WarAmp
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.
Juvelbagarn
Juvelbagarn

I read the question wrong.. My bad.


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.
fdn2012
fdn2012
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: smile.png
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);
ShadowValence
ShadowValence
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).
Juvelbagarn
Juvelbagarn

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.
ShadowValence
ShadowValence
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. biggrin.png

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.