• Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at \$59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!

Pong

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.

3 replies to this topic

#1Crusable  Members   -  Reputation: 594

Like
0Likes
Like

Posted 20 August 2012 - 05:13 AM

Hello,

I am wanting to make a pong clone to test my new knowledge with Java and slick. i haven't started anything yet but im planning it out. I am just a it confused on how you get the direction the ball goes after you hit it with the paddle. I don't know anything about physics (im taking it in school though). do i just randomly generate a direction or is there some stupid long mathematical equation thanks for any help.

#2Tudor Nita  Members   -  Reputation: 121

Like
0Likes
Like

Posted 20 August 2012 - 05:28 AM

"stupid long mathematical equation"

That doesn't sound good regardless of how much I spin it around in my head.

Anyway, you could just go with some simple conditional statements and inverse the direction of the ball. If you want something more complex you'd have to do some vector maths with at least the ball's direction vector, the paddle's forward vector and the position the ball hit the paddle.

[source lang="cpp"] // edge of playing field if (pong.ball.centre_y > 0.6f) { pong.ball_velocity_y = -ball_speed; } else if (pong.ball.centre_y < -0.6f) { pong.ball_velocity_y = ball_speed; } // ball hit a paddle on the right side if (pong.ball_velocity_x > 0 && collides(&pong.ball, &pong.bats[1]) ) { pong.ball_velocity_x = -pong.ball_velocity_x; } // ball hit a paddle on the left side if (pong.ball_velocity_x < 0 && collides(&pong.ball, &pong.bats[0]) ) { pong.ball_velocity_x = -pong.ball_velocity_x; }[/source]

Edited by Tudor Nita, 20 August 2012 - 06:09 AM.

#3Crusable  Members   -  Reputation: 594

Like
0Likes
Like

Posted 20 August 2012 - 05:41 AM

Thanks, and about the stupid long mathematical equation thing, well its about 3 in the morning here and i really don't feel like doing long complex math.

#4Bacterius  Crossbones+   -  Reputation: 9289

Like
0Likes
Like

Posted 20 August 2012 - 06:07 AM

A simple model for a ball bouncing off a paddle (ignoring edge effects) is perfect reflection. If you use vectors, it has an elegant expression not at all complicated once you know some vector algebra. Note that if your paddle is perfectly vertical, this is equivalent to negating the x-component of the ball's velocity, so you could start by doing that instead and improve later on (perhaps by adding round edges).

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

- Pessimal Algorithms and Simplexity Analysis

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