943,617 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
May 4th, 2008
0

Breakout Ball bouncing physics

Expand Post »
Hey, so I'm making a Breakout game in C++ w/ DirectX, and I'm having some trouble thinking of the ball bouncing physics. The way I've made breakout games before was to have the ball only move at 45 degree angles. This was fairly simple. Just reverse the x or y movement everytime you hit something. But now I want to make a game where the ball moves not just at 45 degree angles. The way I see it, bouncing against non-moving blocks will not change the direction that the ball is moving (that is angle-wise, obviously it will bounce off, but the angle that the ball is moving won't ever change to anything other than 45, 135, 225, or 315 degrees. So I came to the conclusion that the only way to change the angle is when the ball collides with a moving object, i.e., the paddle. But I'm not sure how this works exactly. How do I calculate the change in angle based on the movement of the paddle, the place the ball hits the paddle, and the angle the ball is currently traveling at. I look online for some tips but I couldn't find much that gave an algorithm and explained it. So anyone have what I'm looking for. How do I use the factors I just mentioned along with any others I might need to take into consideration to write the code for this?

Any and all help is appreciated.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
May 5th, 2008
1

Re: Breakout Ball bouncing physics

You use simple vectors. You can rotate vectors with minimal calculations.

So when the ball hits the paddle, based on the distance from the center, rotate the directional vector of the ball by a certain amount of degrees.

http://chortle.ccsu.edu/VectorLessons/vectorIndex.html

The above link contains a pretty good coverage of vector math. Vectors are very simple to use and make thing very easy once you get the hang of them.

Try them out. , come back if your run into trouble or if you figure it out post your discoveries here
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
May 5th, 2008
0

Re: Breakout Ball bouncing physics

I didn't have time to read through that yet, but I've heard that distance from center thing before, and I don't necessarily get it. If the paddle isn't moving, the distance from the center of the paddle shouldn't affect the angle of the ball's path of motion (correct me if I'm wrong). And if the paddle is moving, I don't see the distance from the center playing as big a role in the change of direction as I do the direction the paddle is moving relative to the horizontal direction of the ball.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
May 6th, 2008
0

Re: Breakout Ball bouncing physics

The position of the paddle when the ball hits it is wholely irrelevant - the paddle is the same all over, flat, and moves in a constrained direction ( in its own plane ), so any moving point on the paddle is 'identical' to any other point in terms of any calculation you perform. The only areas of the paddle that ARE different are the corners, do you model that accurately?

The quick trick you describe ( inverting one of the components of the moving object's velocity on a bounce with a plane ) works irrespective of the initial angle that the ball is travelling at; if you start the ball moving at any angle other than 45 deg, then bounce angles will be a little more 'varied', because only 45 deg will invert as itself. Any other angle will have slightly more possible results.

But.. I get what you're saying. Given any initial starting angle, and assuming that the player is 'perfect', and assuming that you don't handle corners of the paddle in any special way; the movement of the ball will always be exactly the same; the game is deterministic with a perfect player.

I am not sure if that's how breakout always is.. Alot of basic collision detection code assumes that a collision happens instantanously, and I believe ( although I may be completely wrong on this! ) that if a perfect sphere bounces off a perfect plane, that the velocity of the plane that is in the plane doesn't affect the bounce angle of the sphere. Like I say, I may be wrong

For a quick solution, instead of doing this with the ball velocity after a bounce:
velocity2[x] = velocity1[x];
velocity2[y] = velocity1[y] * -1;
try this:
velocity2[x] = velocity1[x] + ( paddle_velocity[x] * some_constant );
velocity2[y] = velocity1[y] * -1;
[[ some_constant should be between 0 = original behaviour and 1 = lots of sideways effect ]]

the effect here, is that if the paddle moves in the horizontal direction that the ball is moving, the ball will bounce 'flatter', if the paddle moves in the opposite horizontal direction, the ball bounces upwards more sharply. this is sorta like the paddle being rough, the collision being non-instanteanous, and 'pushing' the ball sideways slightly if the paddle moves fast enough.

bear in mind, that this will add energy to your system ( or remove it ) over time. normalize the post-collision velocity and multiply it by the length of the pre-collision velocity to prevent that.. [[ in a system that doesnt have a player adding forces, you'd normally conserve the energy by pushing the paddle backwards ( or starting it spinning ) after the bounce.. but.. that just wouldn't be breakout would it. ]]
Last edited by MattEvans; May 6th, 2008 at 12:41 am.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
May 6th, 2008
0

Re: Breakout Ball bouncing physics

Yes, I like this:
velocity2[x] = velocity1[x] + ( paddle_velocity[x] * some_constant );
velocity2[y] = velocity1[y] * -1;
solution a lot. I had thought of something like that before, but I thought that the code was just affecting the speed of the ball (which I didn't want to worry about yet). I didn't think of that fact that changing the x movement while not changing the y movement will result in a change of angle. I especially like this solution because it avoids trig (which I hear really taxes the proccesor) as well as keeping track of the angle of movement.

Now, the one thing I'm thinking about is that there should be some sort of gradule friction, as well as a maximum ball speed, otherwise the ball speed would get out of hand. I'm thinking that figuring out the maximum ball speed might require some inverse trig functions. Is there another way to figure out the ball's speed w/o trig?

Edit: O, well I just thought of simply putting upper and lower limits (and friction) on the x and y velocities seperately. This would avoid trig, but the friction would be a bit unrealistic. But, hey, games are supposed to be fun, not realistic.
Last edited by CoolGamer48; May 6th, 2008 at 4:56 pm.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
May 6th, 2008
0

Re: Breakout Ball bouncing physics

The magnitude/speed of a velocity is just the length of the vector; in components:

speed = sqrt( ( velocity[x] * velocity[x] ) + ( velocity[y] * velocity[y] ) ); ( good ol' pythagorus theorem:'square of the hypotenuse = the sum of the squares of the other two sides. )

I wouldn't worry too much about the speed of trig / speed of sqrt on processors in this day and age, not unless you're dealing with 1000s of trigs/sqrts/etc every frame.

Edit: And, to 'rescale a vector' to some new length:
vector2[x] = ( vector1[x] / old_length ) * new_length;
vector2[y] = ( vector1[y] / old_length ) * new_length;
It become alot easier to work with math/vector classes than to work entirely in components after a point..
Last edited by MattEvans; May 6th, 2008 at 6:12 pm.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
May 6th, 2008
0

Re: Breakout Ball bouncing physics

So, wait - what exactly is a velocity vector? A point that the object will move to every frame, and then is updated? Or is it always relative to the current position of the object (I'm guessing its this one). So if you were moving to the left at 5 pixels per frame, the velocity would be (-5,0)?
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
May 6th, 2008
0

Re: Breakout Ball bouncing physics

Yes, the second interpretation is correct.

> Velocity is change in position w/ respect to time ( a vector )
> Acceleration is change in velocity w/ respect to time ( a vector )
> Speed is distance travelled w/ respect to time ( a scalar, equal to length of velocity )

Erm, the same exists for rotation, except velocity = angular momentum, and acceleration = torque. But err.. don't worry about rotation, it's evil.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
May 6th, 2008
0

Re: Breakout Ball bouncing physics

Click to Expand / Collapse  Quote originally posted by MattEvans ...
Yes, the second interpretation is correct.
the same exists for rotation, except velocity = angular momentum, and acceleration = torque. But err.. don't worry about rotation, it's evil.
Uhh... I think I'll stick to degrees for rotation.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
May 11th, 2008
0

Re: Breakout Ball bouncing physics

Hey,

I have some code for bouncing. It works well most of the time but there are some issues at times. Does this look good?

C++ Syntax (Toggle Plain Text)
  1. if(m_position.y+m_height >= paddle->GetY() && m_position.y <= paddle->GetY() + paddle->GetHeight() && m_position.x+m_width/2 > paddle->GetX() && m_position.x+m_width/2 < paddle->GetX() + paddle->GetWidth())
  2. {
  3. m_velocity.x += paddle->GetMoveX()*(0.25);
  4. m_velocity.y *= (-1);
  5. }
  6. else if(m_position.x+m_width >= paddle->GetX() && m_position.x <= paddle->GetX() + paddle->GetWidth() && m_position.y+m_height/2 > paddle->GetY() && m_position.y+m_height/2 < paddle->GetY() + paddle->GetWidth())
  7. {
  8. m_velocity.x *= (-1);
  9. m_velocity.y -= paddle->GetSpeed()*(0.15);
  10. }

The code's in C++, and the scope is within the ball class. So things like m_position and m_width refer to the ball, and paddle->GetX() and paddle->GetWidth() refer to the paddle. if you'd like me to give it in more basic pseudo code let me know.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Game Development Forum Timeline: looking for software and computer advise for gaming
Next Thread in Game Development Forum Timeline: Is this software better for game developement.?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC