Jump to content

  • Log In with Google      Sign In   
  • Create Account


Yet another pong question, this time about ball physics.


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.

  • You cannot reply to this topic
8 replies to this topic

#1 wtfmates   Members   -  Reputation: 109

Like
0Likes
Like

Posted 24 October 2012 - 07:01 AM

Alright, so I've got some nice collision detection going on and the ball's Y position is multiplied by -1 every time it hits a wall or a paddle, like so:

if (ballPosition.Y < 0 || ballPosition.Y > maxY)
ballSpeed.Y *= -1;

but i'm finding that it isn't enough. My ball keeps getting stuck going through the same movements. I don't even have to move my paddle when this happens and can just leave it hanging out in the corner while I go grab a beer.

Could someone help me out with this?

Edited by wtfmates, 24 October 2012 - 07:23 AM.


Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8152

Like
1Likes
Like

Posted 24 October 2012 - 07:08 AM

You need to push the ball out of the wall/paddle, otherwise the ball's speed will repeatedly get multiplied by -1 and will jitter with zero net displacement. You can do this:


if (ballPosition.Y < 0 || ballPosition.Y > maxY)
{
ballSpeed.Y *= -1;
if (ballPosition.Y < 0) ballPosition = 0;
if (ballPosition.Y > max) ballPosition.Y = max;
}

Or something like that.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 wtfmates   Members   -  Reputation: 109

Like
0Likes
Like

Posted 24 October 2012 - 07:21 AM

You need to push the ball out of the wall/paddle, otherwise the ball's speed will repeatedly get multiplied by -1 and will jitter with zero net displacement. You can do this:


if (ballPosition.Y < 0 || ballPosition.Y > maxY)
{
ballSpeed.Y *= -1;
if (ballPosition.Y < 0) ballPosition = 0;
if (ballPosition.Y > max) ballPosition.Y = max;
}

Or something like that.


Advice taken and I added it to the code. It doesn't seem to have helped the predictability of my ball physics though.

#4 BeerNutts   Crossbones+   -  Reputation: 2555

Like
4Likes
Like

Posted 24 October 2012 - 07:32 AM


You need to push the ball out of the wall/paddle, otherwise the ball's speed will repeatedly get multiplied by -1 and will jitter with zero net displacement. You can do this:


if (ballPosition.Y < 0 || ballPosition.Y > maxY)
{
ballSpeed.Y *= -1;
if (ballPosition.Y < 0) ballPosition = 0;
if (ballPosition.Y > max) ballPosition.Y = max;
}

Or something like that.


Advice taken and I added it to the code. It doesn't seem to have helped the predictability of my ball physics though.


Oh wait, you don't mean it's getting stuck in a corner, or at the top of the screen, you mean it keeps following the same pattern when bouncing around the screen.

Why would the ball bounce different, if it hits the same angles every time, with no other forces on it? I think it's doing what it thinks it should. Now, you can make the ball have different angles coming off the padddle, based on where it hits on the paddle to provide some variation. That's what I'd do.

Edited by BeerNutts, 24 October 2012 - 07:35 AM.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#5 wtfmates   Members   -  Reputation: 109

Like
0Likes
Like

Posted 24 October 2012 - 07:46 AM



You need to push the ball out of the wall/paddle, otherwise the ball's speed will repeatedly get multiplied by -1 and will jitter with zero net displacement. You can do this:


if (ballPosition.Y < 0 || ballPosition.Y > maxY)
{
ballSpeed.Y *= -1;
if (ballPosition.Y < 0) ballPosition = 0;
if (ballPosition.Y > max) ballPosition.Y = max;
}

Or something like that.


Advice taken and I added it to the code. It doesn't seem to have helped the predictability of my ball physics though.


Oh wait, you don't mean it's getting stuck in a corner, or at the top of the screen, you mean it keeps following the same pattern when bouncing around the screen.

Why would the ball bounce different, if it hits the same angles every time, with no other forces on it? I think it's doing what it thinks it should. Now, you can make the ball have different angles coming off the padddle, based on where it hits on the paddle to provide some variation. That's what I'd do.


Yeah, that's what I meant. I'm sorry if I wasn't clear. That sounds like a good idea, but I'm unsure where to start. Will I need to create a separate bounding box for each 1/3rd of the paddle, or is there a simpler way to do it?

Feel free to tell me to stop pestering you guys and go google it if you get annoyed with these questions.

Found this on a website but I'm not clear on exactly what it's doing:

public void BatHit(int block)
{
if (direction > Math.PI * 1.5f || direction < Math.PI * 0.5f)
{
switch (block)
{
  case 1:
   direction = MathHelper.ToRadians(220);
   break;
  case 2:
   direction = MathHelper.ToRadians(215);
   break;
  case 3:
   direction = MathHelper.ToRadians(200);
   break;
  case 4:
   direction = MathHelper.ToRadians(195);
   break;
  case 5:
   direction = MathHelper.ToRadians(180);
   break;
  case 6:
   direction = MathHelper.ToRadians(180);
   break;
  case 7:
   direction = MathHelper.ToRadians(165);
   break;
  case 8:
   direction = MathHelper.ToRadians(130);
   break;
  case 9:
   direction = MathHelper.ToRadians(115);
   break;
  case 10:
   direction = MathHelper.ToRadians(110);
   break;
  }
}
else
{
  switch (block)
  {
   case 1:
    direction = MathHelper.ToRadians(290);
    break;
   case 2:
    direction = MathHelper.ToRadians(295);
    break;
   case 3:
    direction = MathHelper.ToRadians(310);
    break;
   case 4:
    direction = MathHelper.ToRadians(345);
    break;
   case 5:
    direction = MathHelper.ToRadians(0);
    break;
   case 6:
    direction = MathHelper.ToRadians(0);
    break;
   case 7:
    direction = MathHelper.ToRadians(15);
    break;
   case 8:
    direction = MathHelper.ToRadians(50);
    break;
   case 9:
    direction = MathHelper.ToRadians(65);
    break;
   case 10:
    direction = MathHelper.ToRadians(70);
    break;
  }
}
}

Edited by wtfmates, 24 October 2012 - 07:49 AM.


#6 BeerNutts   Crossbones+   -  Reputation: 2555

Like
1Likes
Like

Posted 24 October 2012 - 08:29 AM

There are multiple ways you could do this. I think the simplest would be to have it check each 1/3 of the paddle (as you suggest), and, do something like this:
if (PaddleHit()) {
  // This covers the middle part of paddle hit
  ballSpeed.Y *= -1;

  if (LeftSidePaddleHit()) {
    // increase Y speed, so it goes up at a tighter angle:
    ballSpeed.Y *= 2;
  }
  else if (LeftSidePaddleHit()) {
    // decrease Y speed, so it goes up at a wider angle:
    ballSpeed.Y /= 2;
  }
  // Ensure Y-Speed doesn't go above or below a minimum here
}

That code above is just a guess. You should really be keeping the velocity as a fraction, so you can perform these computations with greater accuracy.

The other methods of doing this would involve some trigonometry, or some vector math. You're welcome to look up some solutions related to them if you would like.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#7 Bacterius   Crossbones+   -  Reputation: 8152

Like
1Likes
Like

Posted 24 October 2012 - 08:58 AM

You could consider your paddle as a rectangle with two half circles at the ends. Then find out what the ball hits and perform collision response accordingly (for the rectangle, reflect the velocity about the surface normal, for the half circles do the same but the normal depends on the location the ball hit - and this'll make the ball bounce off at different angles if it hits the edges of the paddle). Though this kind of design can't really be shoehorned in the typical "if ball position is less than zero do such and such" type of code - you'd need to start from scratch and define actual objects, run correct intersection loops, etc..

Edited by Bacterius, 24 October 2012 - 09:00 AM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#8 wtfmates   Members   -  Reputation: 109

Like
0Likes
Like

Posted 24 October 2012 - 09:58 AM

You could consider your paddle as a rectangle with two half circles at the ends. Then find out what the ball hits and perform collision response accordingly (for the rectangle, reflect the velocity about the surface normal, for the half circles do the same but the normal depends on the location the ball hit - and this'll make the ball bounce off at different angles if it hits the edges of the paddle). Though this kind of design can't really be shoehorned in the typical "if ball position is less than zero do such and such" type of code - you'd need to start from scratch and define actual objects, run correct intersection loops, etc..


I was afraid of that, but I'm still attempting to shoehorn it into the code. I've found a solution that involves taking the location of the center of the ball and figuring out where it collides in relation to the center of the paddle and adjusting it's direction, speed, and position accordingly. If that doesn't work out I'll create three different collision boxes for each paddle. I know I shouldn't be, but I'm very lazy and attempting to shortcut writing completely new code as much as I can.

Thank you for all of your help and suggestions though. When I work something out I'll post it here and hopefully you guys can look it over and let me know where I need improvement.

There are multiple ways you could do this. I think the simplest would be to have it check each 1/3 of the paddle (as you suggest), and, do something like this:

if (PaddleHit()) {
  // This covers the middle part of paddle hit
  ballSpeed.Y *= -1;
  if (LeftSidePaddleHit()) {
    // increase Y speed, so it goes up at a tighter angle:
    ballSpeed.Y *= 2;
  }
  else if (LeftSidePaddleHit()) {
    // decrease Y speed, so it goes up at a wider angle:
    ballSpeed.Y /= 2;
  }
  // Ensure Y-Speed doesn't go above or below a minimum here
}
That code above is just a guess. You should really be keeping the velocity as a fraction, so you can perform these computations with greater accuracy.
The other methods of doing this would involve some trigonometry, or some vector math. You're welcome to look up some solutions related to them if you would like.

I just noticed this reply from you. That's a pretty simple way of doing it. I'll give it a go and let you know how it turns out. Thanks for your help

Edited by wtfmates, 24 October 2012 - 10:03 AM.


#9 Shaquil   Members   -  Reputation: 815

Like
0Likes
Like

Posted 24 October 2012 - 02:53 PM

Feel free to tell me to stop pestering you guys and go google it if you get annoyed with these questions.


This is just a passing comment, but wouldn't you prefer to figure most of this stuff out on your own?




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.



PARTNERS