Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  code to bounce a ball  (Read 2305 times)
0 Members and 1 Guest are viewing this topic.
Offline jparril1

Junior Member





« Posted 2010-12-16 21:08:30 »

Hey guys. Im trying to get a ball to bounce off of the walls of my applet. The X direction works fine but I just cant get the Y
direction to work. Any idea what Im doing wrong? By the way, "this" is referring to the applet

//loop to constantly repaint the screen
      while(true) {
         
         /* Collision detections between the ball and all four walls*/
         if(ballXPosition > this.getWidth() - ballRadius) {
            
            //change the direction
            ballXSpeed = -1;
         }
         else if(ballXPosition < ballRadius) {
            
            //change the direction
            ballXSpeed = 1;
         }
         if(ballYPostion > this.getHeight() - ballRadius) {
            
            ballYSpeed = -1;
         }
         else if(ballYPostion < ballRadius) {
            
            ballYSpeed = 1;
         }
         
         ballXPosition += ballXSpeed;
         ballYPostion += ballYSpeed;
         
         
         //repaint
         repaint();
Offline dishmoth
« Reply #1 - Posted 2010-12-16 21:56:43 »

I can't see anything wrong with your code (apart from the unconventional spelling of ballYPostion Wink ).

Try printing out the ball's position and velocity each step (System.out.println("x=" + ballXPosition + " y=" + ballYPostion + ...)) and also printing a message whenever the direction changes (or set break points if you prefer).

Simon

Offline DzzD
« Reply #2 - Posted 2010-12-16 22:07:15 »

what exacly is not working,  could you explain a little more the symptomes ?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jparril1

Junior Member





« Reply #3 - Posted 2010-12-16 22:25:14 »

well 2 things are happening.. With the code like this.. the ball just goes straight up through the top of the frame, if I switch the < > signs, it just goes to the bottom and looks like its trying to go through the bottom, but cant. Maybe Im thinking of it the wrong way since the Y coordnates arent like we normally have... (up is negative and Down is positive) Basically, Y direction is never changed
Offline Mike

JGO Wizard


Medals: 69
Projects: 2
Exp: 5 years


Java guru wanabee


« Reply #4 - Posted 2010-12-16 23:16:48 »

Could you post the complete code (use the
1  
 tag)? That'll help us with figuring out what's going wrong.

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline jparril1

Junior Member





« Reply #5 - Posted 2010-12-16 23:20:07 »

sure, I probably should have done this from the start

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

public class BallApplet extends Applet implements Runnable {
   
   private static final long serialVersionUID = -7570390841627016661L;
   private int ballXPosition = 10;
   private int ballYPosition = 100;
   private int ballRadius = 20;
   private int ballXSpeed;
   private int ballYSpeed;
   private Image dbImage; //an image to use for double buffering
  private Graphics dbGraphics;
   
   
   
   public void init() {
     
   }
   
   public void start() {
     
      Thread myThread = new Thread(this);
      myThread.start();
     
   }

   public void stop() {
     
   }
   
   public void destory() {
     
   }
   
   public void run() {
     
      //lowering the threads priority
     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
     
      //loop to constantly repaint the screen
     while(true) {
         
         /* Collision detections between the ball and all four walls*/
         if(ballXPosition > this.getWidth() - ballRadius) {
           
            //change the direction
           ballXSpeed = -1;
         }
         else if(ballXPosition < ballRadius) {
           
            //change the direction
           ballXSpeed = 1;
         }
         if(ballYPostion > this.getHeight() - ballRadius) {
           
            ballYSpeed = -1;
         }
         else if(ballYPostion < ballRadius) {
           
            ballYSpeed = 1;
         }
         
         ballXPosition += ballXSpeed;
         ballYPostion += ballYSpeed;
         
         
         //repaint
        repaint();
         
         try {
            //try to stop the thread for 20 ms
           Thread.sleep(20);
         } catch (InterruptedException ex) {
            //do nothing here if we cant
        }
         
         //making the priority max again
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
      }
     
   }
   
   public void paint(Graphics g) {
      //set the paint color
     g.setColor(Color.BLUE);
     
      //draw a circle
     g.fillOval(ballXPosition - ballRadius, ballYPosition - ballRadius,
            ballRadius * 2, ballRadius * 2);
   }
   
   public void update(Graphics g) {
     
      //create the buffered image
     if (dbImage == null) {
         dbImage = createImage(this.getSize().width, this.getSize().height);
         dbGraphics = dbImage.getGraphics();
      }
     
      //clear background
     dbGraphics.setColor(getBackground());
      dbGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
     
      //draw background
     dbGraphics.setColor(getForeground());
      paint(dbGraphics);
     
      //finally draw this buffered image to the actual screen
     g.drawImage(dbImage, 0, 0, this);
   }
}
Offline DzzD
« Reply #6 - Posted 2010-12-16 23:40:29 »

I guess you dont see it was not compiling and then you use an older version of your compiled class file

you just mispelled ballYPosition => you use severals time ballYPostion

also you mispelled destroy method of the Applet

corrected code here :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

public class BallApplet extends Applet implements Runnable {
   
    private static final long serialVersionUID = -7570390841627016661L;
    private int ballXPosition = 10;
    private int ballYPosition = 100;
    private int ballRadius = 20;
    private int ballXSpeed;
    private int ballYSpeed;
    private Image dbImage; //an image to use for double buffering
   private Graphics dbGraphics;
   
   
   
    public void init() {
     
    }
   
    public void start() {
     
       Thread myThread = new Thread(this);
       myThread.start();
     
    }

    public void stop() {
     
    }
   
    public void destory() {
     
    }
   
    public void run() {
     
       //lowering the threads priority
      Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
     
       //loop to constantly repaint the screen
      while(true) {
         
          /* Collision detections between the ball and all four walls*/
 if(ballXPosition > this.getWidth() - ballRadius) {
 
//change the direction
ballXSpeed = -1;
 }
 else if(ballXPosition < ballRadius) {
 
//change the direction
ballXSpeed = 1;
 }
 if(ballYPosition > this.getHeight() - ballRadius) {
 
ballYSpeed = -1;
 }
 else if(ballYPosition < ballRadius) {
 
ballYSpeed = 1;
 }
 
ballXPosition += ballXSpeed;
 ballYPosition += ballYSpeed;
         
         
          //repaint
         repaint();
         
          try {
             //try to stop the thread for 20 ms
            Thread.sleep(20);
          } catch (InterruptedException ex) {
             //do nothing here if we cant
         }
         
          //making the priority max again
         Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
       }
     
    }
   
    public void paint(Graphics g) {
       //set the paint color
      g.setColor(Color.BLUE);
     
       //draw a circle
      g.fillOval(ballXPosition - ballRadius, ballYPosition - ballRadius,
             ballRadius * 2, ballRadius * 2);
    }
   
    public void update(Graphics g) {
     
       //create the buffered image
      if (dbImage == null) {
          dbImage = createImage(this.getSize().width, this.getSize().height);
          dbGraphics = dbImage.getGraphics();
       }
     
       //clear background
      dbGraphics.setColor(getBackground());
       dbGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
     
       //draw background
      dbGraphics.setColor(getForeground());
       paint(dbGraphics);
     
       //finally draw this buffered image to the actual screen
      g.drawImage(dbImage, 0, 0, this);
    }
}

Offline dishmoth
« Reply #7 - Posted 2010-12-16 23:45:44 »

In case DzzD's answer doesn't do the trick...

Initial values for ballXSpeed and ballYSpeed?  It looks to me like they're both initially zero, but the start x-position is sufficiently close to a wall that the ball is immediately given a positive x-speed.  I don't see anything that stops the y-speed from remaining zero (but that may depend on the height of the applet).

Simon

Offline jparril1

Junior Member





« Reply #8 - Posted 2010-12-16 23:48:05 »

Thanks for the corrections, but thats not whats causing the issue. I am using eclipse anyway so its being recompiled everytime I run an updated version. Its something in my logic thats messed up... something with the collision with the Y direction. Ignore the misspellings, Im 100% sure that its not the problem
Offline jparril1

Junior Member





« Reply #9 - Posted 2010-12-16 23:48:45 »

ahhh dishmoth, I need to check that out.. could be the problem.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jparril1

Junior Member





« Reply #10 - Posted 2010-12-16 23:51:23 »

THAT WAS IT! Thank you so much... I really appreciate the help
Offline DzzD
« Reply #11 - Posted 2010-12-17 00:16:53 »

Thanks for the corrections, but thats not whats causing the issue. I am using eclipse anyway so its being recompiled everytime I run an updated version. Its something in my logic thats messed up... something with the collision with the Y direction. Ignore the misspellings, Im 100% sure that its not the problem
your code was not compiling, so if it execute it can only be an ancient version

Offline jparril1

Junior Member





« Reply #12 - Posted 2010-12-17 02:45:15 »

My version of the code was fine with spelling. The version I posted was some copying and pasting from my code so theres were pieces of it that were stilll old. The problem was that I never gave Yspeed an initial value so it never actually worked. Thanks for that dishmoth
Offline DzzD
« Reply #13 - Posted 2010-12-17 10:25:49 »

 persecutioncomplex persecutioncomplex persecutioncomplex persecutioncomplex

Offline ReBirth
« Reply #14 - Posted 2011-01-25 04:18:18 »

1  
2  
3  
4  
5  
else if(ballXPosition < ballRadius) {
           
            //change the direction
           ballXSpeed = 1;
         }

ballRadius is radius of the ball right? if yes it should be (ballXPosition < 0) which bounce on left side, in contradiction to your (ballXPosition > this.getWidth() - ballRadius) which bounce on right side.

Offline dishmoth
« Reply #15 - Posted 2011-01-26 11:11:56 »

ballRadius is radius of the ball right? if yes it should be (ballXPosition < 0) which bounce on left side, in contradiction to your (ballXPosition > this.getWidth() - ballRadius) which bounce on right side.
ballXPosition and ballYPosition are the coordinates of the ball's centre (as can be seen from the paint() function), so the original code is correct.  The ball bounces when its centre is distance ballRadius from a wall.
Simon

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (29 views)
2014-04-15 18:08:23

BurntPizza (26 views)
2014-04-15 03:46:01

UprightPath (41 views)
2014-04-14 17:39:50

UprightPath (23 views)
2014-04-14 17:35:47

Porlus (40 views)
2014-04-14 15:48:38

tom_mai78101 (62 views)
2014-04-10 04:04:31

BurntPizza (122 views)
2014-04-08 23:06:04

tom_mai78101 (221 views)
2014-04-05 13:34:39

trollwarrior1 (188 views)
2014-04-04 12:06:45

CJLetsGame (197 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!