Connecting Tech Pros Worldwide Forums | Help | Site Map

Wall ball bounce

Newbie
 
Join Date: May 2007
Posts: 8
#1: Jun 1 '07
Source:
http://fileho.com/download/120846695042/bounce2.zip.html

How would I go about making a wall for my bouncing ball?
For example if the ball hit the center box it would bounce.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Jun 1 '07

re: Wall ball bounce


Quote:

Originally Posted by Bigs

Source:
http://fileho.com/download/120846695042/bounce2.zip.html

How would I go about making a wall for my bouncing ball?
For example if the ball hit the center box it would bounce.

Well, I'm not going to download those files and I know a few others who wont as well. It's better if you tell us what characteristics you want your wall to have. The bouncing can be controlled by tracking the position of the ball on the canvas (or whatever it is you are using) and changing the direction of the ball as soon as it enters certain coordinates. You can even make the wall invisible.
Newbie
 
Join Date: May 2007
Posts: 8
#3: Jun 1 '07

re: Wall ball bounce


Quote:

Originally Posted by r035198x

Well, I'm not going to download those files and I know a few others who wont as well. It's better if you tell us what characteristics you want your wall to have. The bouncing can be controlled by tracking the position of the ball on the canvas (or whatever it is you are using) and changing the direction of the ball as soon as it enters certain coordinates. You can even make the wall invisible.

It is a little hard to describe without actually seeing it. Well, basically I have two boxes:
Expand|Select|Wrap|Line Numbers
  1.        g.drawRect(10,0,950,500);
  2.        g.drawRect(350,170,200,150);
One box inside the other, right now the ball will bounce inside the first box, but the part I am having trouble with is making the inside box also make the ball bounce when hit.

For the first box I just took the x or y and then changed direction example:
Expand|Select|Wrap|Line Numbers
  1.          if  (x > 910)                 /** bounces back left when x = 910 */   
  2.          {dir = dir - 2;}
But, because the other box is on the inside it will not work. I need to find out how to add walls that the ball will bounce off of. Preferably something that will not require at lot of check for the ball going into a certain point. But, anything that works would be great.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Jun 1 '07

re: Wall ball bounce


Quote:

Originally Posted by Bigs

It is a little hard to describe without actually seeing it. Well, basically I have two boxes:

Expand|Select|Wrap|Line Numbers
  1.      g.drawRect(10,0,950,500);
  2.      g.drawRect(350,170,200,150);
One box inside the other, right now the ball will bounce inside the first box, but the part I am having trouble with is making the inside box also make the ball bounce when hit.

For the first box I just took the x or y and then changed direction example:
Expand|Select|Wrap|Line Numbers
  1. if (x > 910) /** bounces back left when x = 910 */ 
  2. {dir = dir - 2;}
But, because the other box is on the inside it will not work. I need to find out how to add walls that the ball will bounce off of. Preferably something that will not require at lot of check for the ball going into a certain point. But, anything that works would be great.

I still don't get the problem. What's different for the second box except for the co-ordinates? Can't you do it the same way you did with the first box?
Newbie
 
Join Date: May 2007
Posts: 8
#5: Jun 1 '07

re: Wall ball bounce


Quote:

Originally Posted by r035198x

I still don't get the problem. What's different for the second box except for the co-ordinates? Can't you do it the same way you did with the first box?

The box is inside the other box. The reason I cant do it the same way is because it would be blocking the ball from moving freely in the first box because it would section off the entire area from that point. Think of it this way I have a pool of water and there is an island inside the pool.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#6: Jun 1 '07

re: Wall ball bounce


Quote:

Originally Posted by Bigs

The box is inside the other box. The reason I cant do it the same way is because it would be blocking the ball from moving freely in the first box because it would section off the entire area from that point. Think of it this way I have a pool of water and there is an island inside the pool.

Ok. What's wrong with using :
The leftmost side of the box is at 350 + 200 = 550.
Expand|Select|Wrap|Line Numbers
  1.  if (x > 550) /** bounces back left when x = 550 */  { 
  2.  
  3. dir = dir - 2;
  4.  
  5. }
  6.  
  7.  
Newbie
 
Join Date: May 2007
Posts: 8
#7: Jun 1 '07

re: Wall ball bounce


It is because it is still counting for both inside and outside, box.
For example it may hit the inside box point and bounce but the program is also considering the entire point from there on. Making a line across causing the ball to bounce before it touches an outside corner as well. Bouncing off nothing.


Could you please look at the full program code, it is kind of hard to explain in words.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#8: Jun 1 '07

re: Wall ball bounce


Quote:

Originally Posted by Bigs

It is because it is still counting for both inside and outside, box.
For example it may hit the inside box point and bounce but the program is also considering the entire point from there on. Making a line across causing the ball to bounce before it touches an outside corner as well. Bouncing off nothing.

Could you please look at the full program code, it is kind of hard to explain in words.

So you don't want the ball to get into the inner box?
You need to test both the x position and the y position deepending on the direction of the ball. e.g if the ball is travelling from left to right, do

if x >= 550 and if y is between 20 and 170 then move the ball back to the left
Reply