• Advertisement
Sign in to follow this  

simple pong game

This topic is 4172 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to make a simple pong game from scratch. I don't know anything about physics or elastic collisions. Is there anyone who can give me some pointers as to handle the physics in pong. I know, it sounds pretty easy to handle, but for some reason I can't seem to comprehend simple phyiscs like this. I did look into some of the tutorials, but I couldn't find anything that would help me with balls bouncing off walls (probably because it's too simple?). I think I understand how velocity can be intrepreted into a vector, something that has magnitude and direction... So let's say we have a coordinate plane with the positive y axis going up and the positive x axis going to the right. There is a ball with velocity of (8, 2) (... and I think that means it's going to the right 2 spaces and up 8 spaces). How would you get the angle for this? Let's say there is a wall at x = 100. I guess you can tell when the collision occurs by calculating the distance between the ball and the wall every fixed period of time. And I know pythagoras so distance calculation shouldn't be a problem. But how do you calculate the angle at which it hits the wall, and the angle from which it bounces off? You could say I'm confused about the angles and when to use cosine and sine and whatever... One tutorial looked like it was talking about balls bouncing off balls, but I think that's different then balls bouncing off walls. This question was probably answered somewhere else, and I just failed to find it.... whatever... Sorry, I'm a noob. 'Nuff said.

Share this post


Link to post
Share on other sites
Advertisement
You need to bounce the ball off at the opposite direction it came from at the same angle.... so if it is going from the left to the right it is going along the x-axis.

Lets pick an example speed... 3 pixels per second.... You need to turn that into -3 pixels per second.

And do the same for the y-axis apart from you modify the y value instead. You don't really need to mess around with angles.

Good luck with the game
David

Share this post


Link to post
Share on other sites
I had to do something that involved bouncing balls once, and here's the way that I did it:


float ballXPosition = 0.0;
float ballYPosition = 0.0;
float ballXVelocity = 1.0;
float ballYVelocity = 1.0;
// initialization, etc. code here
while(1) {
if(ballXPosition >= 100 || ballXPosition <= -100) {
// flip x velocity direction
ballXVelocity*=-1;
}
if(ballYPosition >= 100 || ballYPosition <= - 100) {
// flip y velocity direction
ballYVelocity*=-1;
}
ballXPosition += ballXVelocity;
ballYPosition += ballYVelocity;
// redraw ball
}


...This way, whenever the ball hits our bounding area, the velocity flips. At the start, it might move like this:

0,0
1,1
2,2

But eventually, it'll get to something like 100, 100. Once it hits that, both values will be swapped, and the movement will become something like:

100,100
99,99

And so on, until it hits -100,-100.

Share this post


Link to post
Share on other sites
Well, I am making a pong game as well

I have this:

int ballSpriteX = 300;
int ballSpriteY = 300;

// Ball speed
int ballSpeedY = 2;
int ballSpeedX = 5;

void UpdateBallSprite()
{
ballSpriteX += ballSpeedX;
ballSpriteY += ballSpeedY;

int MinY = 0;
int MaxY = Window.ClientHeight - ballTexture.Height;
int MinX = 0;
int MaxX = Window.ClientWidth - ballTexture.Width;

if (Collision.detectCollision(player1SpriteY, player1SpriteX, playerTexture.Height, ballSpriteY, ballSpriteX))
{
ballSpeedY *= -1;
ballSpeedX *= -1;
}
if (Collision.detectCollision(player2SpriteY, player2SpriteX, playerTexture.Height, ballSpriteY, ballSpriteX))
{
ballSpeedY *= -1;
ballSpeedX *= -1;
}

if (ballSpriteY > MaxY)
{
ballSpeedY *= -1;
}
if (ballSpriteY < MinY)
{
ballSpeedY *= -1;
}
if (ballSpriteX < MinX)
{
ballSpeedX *= -1;
//ballSpeedX = 0;
}
if (ballSpriteX > MaxX)
{
ballSpeedX *= -1;
//ballSpeedX = 0;
}
}


Hope it helps

Share this post


Link to post
Share on other sites
Cool! I think I understand now. So, I don't need to calculate any angles after all... Interesting. Thanks :)

I actually found a link for anyone who has found this thread and has similar difficulty understanding this stuff. It was very helpful as well.

elastic collisions

Share this post


Link to post
Share on other sites
Well, the physics I used in my game, aren't very advanced, and not really as realistic as in the link you provided.

I wonder if a game like pong could really benefit from realistic physics.

Maybe wait with that until I get to more advanced games.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement