Search code examples
javacollision-detection

java bouncing ball collision not working


i have been trying to make this bouncing ball game in java and here's my code. I am not able to understand the velocity changes when the ball is made to hit the wall. when i tried to change the velocity the ball just doesn't moves but stays in one position and vibrates.i don't know where the problem is but the ball is not bouncing off the wall..can anybody help me out??

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics2D;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.ArrayList;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ballGame.SimpleSprite;


public class mainGame extends JPanel
{


    public static void main(String args[])
    {
        JFrame frame = new JFrame("hello");

        mainGame mainPanel = new mainGame(600, 400);

        SimpleSprite st = new SimpleSprite(mainPanel);

        frame.add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }


    private ArrayList<SimpleSprite> sprites = new ArrayList<SimpleSprite>();

    final static int timer_msec=30;

mainGame(int width, int height)
{
    setPreferredSize(new Dimension(width, height));
    sprites.add(new SimpleSprite(Color.blue, 0, 35, 2, 1));

    startTimer();

}

public void startTimer()
{
    java.util.Timer timer = new java.util.Timer();
    TimerTask timerTask = new TimerTask()
    {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            updateSimulation();
        }

    };
    timer.scheduleAtFixedRate(timerTask, 0, timer_msec);

}

public void updateSimulation()
{
    for(SimpleSprite s: sprites)
    {
        s.update();
    }
    repaint();
}

public void paintComponent(Graphics g)
{
    Graphics2D g2d = (Graphics2D) g;
    paintBackground(g2d);
    for (SimpleSprite s : sprites) {
       s.draw(g2d);
}
}

public void paintBackground(Graphics2D g2d)
{
    g2d.setColor(Color.LIGHT_GRAY);
    g2d.fillRect(0, 0, getWidth(), getHeight());
}

}

class SimpleSprite extends JPanel
{
       // basic x,y movement at constant velocity
       float x, y, dx, dy; // position and velocity (pixels/TIMER_MSEC)
       Color color;
      private static float width=600;;
       SimpleSprite(mainGame g)
       {
           width=g.getWidth();
       }

       public SimpleSprite(Color color, float x, float y, float dx, float dy) {
          // initial position and velocity
          this.color = color;
          this.x = x;
          this.y = y;
          this.dx = dx;
          this.dy = dy;
       }
       public void update() { // update position and velocity every n milliSec

           x += dx; // velocity in x direction
           y += dy; // velocity in y direction

            if(x<0)
           {
             dx=+dx;
           }
           if(x>width)
           {
               dx=-dx;
           }





           if(y<0)
           {
               dy=+dy;
           }
           if(y>width)
           {
               dy=-dy;
           }


       }
       public void draw(Graphics2D g2d) {
          g2d.setColor(color);
          g2d.fillOval((int) x, (int) y, 100, 100); // draw this at latest position.
       }
    }

Solution

  • dx=+dx and dy=+dy do nothing.

    If you want to reverse the direction do

    dx=-dx;

    That remains true regardless of whether dx was positive or negative prior to the change.