• Create Account

# Pong Game Development

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1maheshbinny  Members   -  Reputation: 118

Like
0Likes
Like

Posted 21 May 2012 - 12:33 AM

Hi,

I am developing Pong game in DirectX9 API using C++ language.

I am facing a problem to change the ball (sphere) direction when it collides with bat (cube).

I am not getting a proper solution to this problem.

Mahesh

### #2Ashaman73  Crossbones+   -  Reputation: 6744

Like
0Likes
Like

Posted 21 May 2012 - 03:12 AM

I am facing a problem to change the ball (sphere) direction when it collides with bat (cube).

What problem ? Any screenshots or a piece of code to look at ?

### #3maheshbinny  Members   -  Reputation: 118

Like
0Likes
Like

Posted 21 May 2012 - 03:37 AM

I am facing a problem to change the ball (sphere) direction when it collides with bat (cube).

What problem ? Any screenshots or a piece of code to look at ?

For an Instance in Pong game, typically these following cases will occur

CASE1 : If ball hits in the middle of the bat
CASE2: If ball hits in top of the bat
CASE3: If ball hits in bottom of the bat

In the above 3 cases ball will move in three different directions.

My question is how to achieve this?

In my game, I have used cube as a bat and sphere as a ball. I can able to compute the collsiion between sphere with cube.

Altering the sphere direction after it collides with cube in the above three cases, I am not able to get it

Mahesh

### #4codeToad  Members   -  Reputation: 142

Like
0Likes
Like

Posted 21 May 2012 - 05:57 PM

It's been a while since I've played any Pong clones, but I had always thought that the angle at which the ball bounced was related to the angle at which the ball impacted the bat (as opposed to where it impacted the bat). I've always assumed that pong obeyed the law of "angle of incidence = angle of reflection" (http://www.physicsclassroom.com/class/refln/u13l1c.cfm).

But let's say you do want the sphere's direction to be based on which part of the bat it hit. When you detect the collision, you need to determine which part of the bat the ball hit. (i.e., a value from 0 - 100, where 0 is the bottom of the bat, and 100 is the top of the bat). Then take that value and multiply it by 180 degrees. This will give you the value of the ball's new direction (where 0 degrees is pointing toward the bottom of the screen, and 180 degrees is pointing toward the top of the screen).

### #5d k h  Members   -  Reputation: 435

Like
1Likes
Like

Posted 21 May 2012 - 06:55 PM

It's been a while since I've played any Pong clones, but I had always thought that the angle at which the ball bounced was related to the angle at which the ball impacted the bat (as opposed to where it impacted the bat). I've always assumed that pong obeyed the law of "angle of incidence = angle of reflection" (http://www.physicscl...efln/u13l1c.cfm).

That wouldn't work as the ball would never stop going straight (as the initial angle the ball hits the paddle at is perpendicular) and you wouldn't be able to change its trajectory. Even if you would have the ball start in a non-standard, diagonal way, you couldn't aim with the paddle whatsoever.

The best PONG clones tend to take that reflection vector AND the 'impact' vector (the one you describe, codeToad) and average the actual reflection vector somewhere in between. That way the angle of incidence still plays a role (ie. the momentum the ball had before it hit the paddle) but you still have control over it (and can aim it) by letting it hit the different parts of the paddle!

Hope this helps, it's essential to a good PONG clone to get this right!

Edited by d k h, 21 May 2012 - 06:56 PM.

### #6maheshbinny  Members   -  Reputation: 118

Like
0Likes
Like

Posted 22 May 2012 - 05:49 AM

It's been a while since I've played any Pong clones, but I had always thought that the angle at which the ball bounced was related to the angle at which the ball impacted the bat (as opposed to where it impacted the bat). I've always assumed that pong obeyed the law of "angle of incidence = angle of reflection" (http://www.physicscl...efln/u13l1c.cfm).

But let's say you do want the sphere's direction to be based on which part of the bat it hit. When you detect the collision, you need to determine which part of the bat the ball hit. (i.e., a value from 0 - 100, where 0 is the bottom of the bat, and 100 is the top of the bat). Then take that value and multiply it by 180 degrees. This will give you the value of the ball's new direction (where 0 degrees is pointing toward the bottom of the screen, and 180 degrees is pointing toward the top of the screen).

You said multiply by 180 degrees

how to do it?
how it will change ball's direction?

Mahesh

### #7Ashaman73  Crossbones+   -  Reputation: 6744

Like
0Likes
Like

Posted 22 May 2012 - 06:21 AM

Assumption, you move your ball in direction x,y, then a very basic way of doing it would look like this:

When you hit an obstancle on the right or left side, just do the following:
x = -x

When you hit an obstancle on the top or bottom side, just do the following
y = -y


As you can guess, it would be somewhat boring, but basically it is just a reflection.

### #8Khatharr  Crossbones+   -  Reputation: 2870

Like
0Likes
Like

Posted 22 May 2012 - 02:51 PM

In my brick game I add ((rand() % 3) - 1) to the angle for every collision to ensure that there's no 'perpendicular lock' problem. Zero degrees is toward the top of the screen for me, so when my ball impacts something I determine whether it was the x motion or y motion that caused the impact and then do either (angle - 180) [for y caused collisions] or (360 - angle) [for x caused collisions].

Of course I use 'dragging' collision testing, so depending on what collision type you use you may need to us a different strategy to determine the collision side.

I posted a rar of my project in another thread earlier. You can look in my Ball.cpp at the do_motion() function to see how I handled it, since it's a similar game type.

http://www.gamedev.net/topic/624433-texture-aliasing-in-win7/

Hope that helps!

Edited by Khatharr, 22 May 2012 - 03:00 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #9codeToad  Members   -  Reputation: 142

Like
0Likes
Like

Posted 22 May 2012 - 05:19 PM

how to do it?
how it will change ball's direction?

You must have some piece of data that is keeping track of the ball's current direction. For example, you could have a float called currentDirection that is a value ranging from 0 - 360. Once you have determined the new direction for the ball, assign that value to currentDirection. (i.e., currentDirection = CalculateNewDirection();) Then on every frame, you need to update the ball's position, so you would do something like:

ball.x = ballSpeed * cos(currentDirection);
ball.y = ballSpeed * sin(currentDirection);

Your code may differ depending on which direction is 0 degrees, and whether degrees increase clockwise or counter-clockwise. Also, it is possible to keep track of the current direction using a vector instead of a float.

### #10Khatharr  Crossbones+   -  Reputation: 2870

Like
0Likes
Like

Posted 22 May 2012 - 09:26 PM

Aye. Since my 0 degrees is up I use something like

Then

for(i = 0; i < distance; ++i) {
//collider's x = x + (xstep * i)
//collider's y = y + (ystep * i)
//check for collision
}

If I collide then I back off one step and check the x and y motions individually to see which one caused the collision. That determines my angle of deflection. Then I just change the angle, set the current position to the collision position and recurse with (distance - i). When the for loop completes with no collisions then the motion is complete.

Edited by Khatharr, 22 May 2012 - 09:34 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS