Sign in to follow this  
Juvelbagarn

Rectangle

Recommended Posts

Juvelbagarn    108
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:
[CODE]ballrec = new Rectangle((int)ballvec.X, (int)ballvec.Y, ball.Width, ball.Height);[/CODE]
And this is the Draw section:
[CODE]spritebatch.Draw(ball,ballvec,ballrec,Color.White,0,new Vector2(0,0),1,SpriteEffects.None,0);[/CODE]

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

/Freemountain

Share this post


Link to post
Share on other sites
WarAmp    750
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 this post


Link to post
Share on other sites
Juvelbagarn    108
[quote name='ShadowValence' timestamp='1346353189' post='4974878']
I read the question wrong.. My bad.
[/quote]

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



here's some more code.
[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;
[/CODE]



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 this post


Link to post
Share on other sites
fdn2012    126
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: [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
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 this post


Link to post
Share on other sites
ShadowValence    385
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 this post


Link to post
Share on other sites
Juvelbagarn    108
[quote name='ShadowValence' timestamp='1346354708' post='4974887']
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).
[/quote]

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 this post


Link to post
Share on other sites
ShadowValence    385
As long as you remember to include the [b]size[/b] [width & height] of the paddle and the ball in your collision detection, you'll be fine. [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this