Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / simple ball("pong") phyisics question...

Author
Message
gpex
19
Years of Service
User Offline
Joined: 29th May 2005
Location:
Posted: 22nd Jun 2005 13:19 Edited at: 23rd Jun 2005 04:36
ok so i've got everything in here working, you move the paddle around with the mouse and if it hits the paddle it goes the opposite diretion and if you left click it speeds the ball up and if you right click it slows the ball down.

i have to problems...

1. i would like to be able to get the ball to go in a different direction sometimes, not just back the exact same way that it came. I cant figure out how to do this.

2. sometimes(many times) the ball gets stuck in the paddle, all you do is shake the mouse to get it unstuck but i would like to keep that
from happening all together.

any help would be great...
(it's kind of sloppy, sorry.)




-gpex-

Your signature has been erased by a mod because it is larger than 600x120...
GICO
20
Years of Service
User Offline
Joined: 22nd Jun 2004
Location: Probably watching The Monkey Show
Posted: 25th Jun 2005 06:44 Edited at: 25th Jun 2005 06:49
do like this for the bouncing:
when it hits the opponent then ball_angle# = 360 - ball_angle#

This actually has been answered in a turorial by Chris K (its great btw)
and as a matter of fact its the second Topic from the top.. so a bit forum search before posting wouldnt harm anyone

GICO

Give food, give give!!!
NanoBrain
19
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Portland, OR
Posted: 25th Jun 2005 08:44 Edited at: 25th Jun 2005 09:19
gpex,

If I am correct, the ball is getting stuck onto the paddle because, when the collision takes place, the direction of the ball is the only calculation being made. However, a second calculation is needed. This is, to reposition the ball in front of the paddle, because otherwise the ball continues to touch the paddle, therefore the collision code(ball changing direction) is happening over and over, rapdily, 'without moving'. Once repositioned, it is then free of the collision of the paddle.

The reason the ball stays on the paddle until the mouse is shaken, is because of the above, and that once the paddle, in one sync, has a chance to leave the area of the ball(the mouse being shaken heavily), the ball is then not touching the paddle, having then a chance to move.

About a changing direction of the ball. A 'direction' method is quite a simple way to go about it. Think about this, an x coordinate direction would be left or right. A y coordinate direction would be up or down. Knowing this, let's make two direction variables, to use for boolean operations, 1 or 0. Let's say that a value of 1, on the x coordinate, means "right", as to add to the ball's x coordinate. A value of 0 should then equal to "left", as to subtract from the ball's x coordinate. Also, a value of 1, on the y coordinate should equal to "down", as adding, and 0 equal "up", as subtracting.

If the ball hits any vertical surface(player paddle, side wall), then the x coordinate direction should be swapped. If it was 1, it should now be 0. Therefore, if the ball traveling right(1), it will now be going left(0).

If the ball hits any horizontal surface(ceiling, floor), then the y coordinate direction should be swapped. If it was 1, it should now be 0. Therefore, if the ball was traveing down(1), it will now be traveling up(0).

We then use these boolean values to either increase or decrease the value of the specified coordinate. To make the ball go right on the x coordinate, it's value must be added to. A value of 0 would mean to subtract from the position value. Same goes for the y coordinate, except up and down.



Try this program:



+NanoBrain+
gpex
19
Years of Service
User Offline
Joined: 29th May 2005
Location:
Posted: 25th Jun 2005 12:14
@nanobrain: That actually helped a lot, it is a much simpley process than i had pictured it in my mind, thanks.

Your signature has been erased by a mod because it is larger than 600x120...

Login to post a reply

Server time is: 2024-11-27 22:48:06
Your offset time is: 2024-11-27 22:48:06