Jump to content
  • Advertisement
Sign in to follow this  
Juvelbagarn

Rectangle

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

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
Advertisement
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

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. Edited by Freemountain

Share this post


Link to post
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: 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);

Share this post


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


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


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!