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.
Quote this message in a reply
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
Quote this message in a reply
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:

|
| \
| |
| |
| /
|
Quote this message in a reply
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:

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.
}
Quote this message in a reply
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?
Quote this message in a reply
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 );
Quote this message in a reply
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?
Quote this message in a reply
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
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #9
so that's what everyone means by man pages. Now I see.
Quote this message in a reply
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
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  [Request] Pong Source &amp; One Question McSpider 6 7,320 Sep 18, 2011 07:21 AM
Last Post: fudog
  Bullet Physics Library / COLLADA physics viewer erwincoumans 14 12,608 Aug 12, 2006 12:59 AM
Last Post: Fenris
  Pong AI trouble saxplayer13 15 7,661 Dec 16, 2005 10:40 AM
Last Post: jamil5454
  My first Cocoa Game that works! (Pong) blobbo 16 9,782 Aug 4, 2005 03:15 PM
Last Post: Andrew
  Pong Questions: blobbo 8 4,134 Jan 21, 2004 08:16 PM
Last Post: blobbo