PDA

View Full Version : Pong "Physics"


Nick
2004.09.28, 04:24 PM
I'm working on a small Pong clone just to get away from working on any 3D game right now but I've reached a problem. I have basic wall collisions done and scoring. I have two paddles (both controlled by humans with the keyboard) and the ball bounces off the paddles. My problem is that twist that existed in Pong where you could hit the ball further from the center and have it change the angle of trajectory. I have two float variables (mDX and mDY) to keep track of the movement. Any suggestions on how to get this done? I've tried a couple things and none have worked right yet.

aaronsullivan
2004.09.28, 04:37 PM
It actually works as if you have a curved surface on your paddle.
The way to figure it is as if the closer you are to edge of your paddle, the ball should react as if that edge is angled downward toward the end. Basically, on a flat edge you are probably making the change in y inverse, but towards the edges, the amount of change in y should be less.

Nick
2004.09.28, 05:30 PM
Is the curve surface analogy assuming the curve faces towards the ball or toward the wall behind it? My understanding was always that the curve faced back towards the wall similar to this poorly drawn ASCII image:

|
| \
| |
| |
| /
|

FCCovett
2004.09.28, 05:35 PM
Subtract the position of the ball from the position of the paddle:

float sinHit = ( ball->pos.y - paddle->pos.y );

Check if the ball projects onto the paddle's vertical profile:

sinHit =/ ( ball->radius + paddle->height / 2.0f );

Then:

if ( fabsf( sinHit ) < 1.0f )
{
float ballSpeed = ScalarOfVector( ball->velocity );

ball->velocity.y = ballSpeed * sinHit;

ball->velocity.x = ballSpeed * sqrt( 1.0f - ( sinHit * sinHit ) ); // Get the cosine based on the sine.
}

Nick
2004.09.28, 06:02 PM
Does it matter that I'm just using a square for my "ball?" Is ScalarOfVector() a standard function or one I need to write myself?

FCCovett
2004.09.28, 07:53 PM
Well, no. Just pick a radius that is a little bit larger than the half-side of the square. To get the scalar part of a vector, create a method that returns:

sqrt( x * x + y * y + z * z );

Nick
2004.09.28, 08:24 PM
Didn't see this the first time, but what is fabsf? Is that an absolute value function?

aarku
2004.09.28, 08:32 PM
Didn't see this the first time, but what is fabsf? Is that an absolute value function?

man pages are your friend. :-)

Go into terminal and enter at the prompt %

% man 3 fabsf

-Jon

Nick
2004.09.28, 10:36 PM
so that's what everyone means by man pages. Now I see.

aarku
2004.09.28, 11:36 PM
It's fabs(), not fabsf(). My bad. It returns the absolute value without a sign, always positive. It works like abs(), but fabs() if for float value.

double
fabs(double x);

float
fabsf(float x);

-Jon