Pong "Physics"

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
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.
Member
Posts: 233
Joined: 2003.05
Post: #2
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.

"Pay no attention to that man behind the curtain." - Wizard of Oz
Sage
Posts: 1,066
Joined: 2004.07
Post: #3
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:

|
| \
| |
| |
| /
|
Member
Posts: 156
Joined: 2002.11
Post: #4
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:

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.
}
Sage
Posts: 1,066
Joined: 2004.07
Post: #5
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?
Member
Posts: 156
Joined: 2002.11
Post: #6
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 );
Sage
Posts: 1,066
Joined: 2004.07
Post: #7
Didn't see this the first time, but what is fabsf? Is that an absolute value function?
Moderator
Posts: 522
Joined: 2002.04
Post: #8
SimReality/Nick Wrote: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
Sage
Posts: 1,066
Joined: 2004.07
Post: #9
so that's what everyone means by man pages. Now I see.
Moderator
Posts: 522
Joined: 2002.04
Post: #10
FCCovett Wrote: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