Pong Game Development

Started by
8 comments, last by Khatharr 11 years, 11 months ago
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.

Please help me in this

Mahesh
Advertisement

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 ?

[quote name='maheshbinny' timestamp='1337582011' post='4941842']
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 ?
[/quote]


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
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).

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!

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).


Thanks for your reply

You said multiply by 180 degrees

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

Mahesh
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.
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/
The link is at the end of the thread.

Hope that helps!
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.

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.
Aye. Since my 0 degrees is up I use something like

xstep = sin(deg2rad(angle))
ystep = -(cos(deg2rad(angle)))

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.
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.

This topic is closed to new replies.

Advertisement