Results 1 to 2 of 2
  1. #1
    Jayayoh is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default Collisions are working & not working

    Hello! I checked around the forum and on different sites, but I can not figure out why my collision script is not working. It works when the padde is stationary, but if the paddle is moving when the ball comes into contact with it, it ignores the collision. furthermore, when you release the keys if the ball is inside the paddle, it goes straight up or down as if it is experiencing a collision. While I am at it, the ball also slows down whenever you move the paddle. I personally think they are all connected, But I have looked over my code several times and cant find the problem. Some help would be greatly appreciated. Here is the code :
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    
    class game extends JFrame implements KeyListener
    {
    //innitialize variables
    	static int game_width = 800;
    	static int game_height = 500;
    	static boolean playable = true;
    //main method
    	public static void main(String[] args)
    	{
    		game window = new game();
    	}
    //class constructor
    	public game()
    	{
    		super("a ball");
    		setSize(game_width,game_height);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setVisible(true);
    		setResizable(false);
    		add(new pnl());
    		this.addKeyListener(this);
    	}	
    //KeyListener
    	public void keyPressed(KeyEvent event)
    	{
    		if(event.getKeyCode() == 38)
    		{	
    			paddle.dy = -2;
    			paddle.motion = true;
    		}
    		else if(event.getKeyCode() == 40)
    		{
    			paddle.dy = 2;
    			paddle.motion = true;
    		}
    	}
    	public void keyTyped(KeyEvent event)
    	{
    	}
    	public void keyReleased(KeyEvent event)
    	{
    		if(event.getKeyCode() == 38)
    		{
    			paddle.motion = false;
    		}
    		else if(event.getKeyCode() == 40)
    		{
    			paddle.motion = false;
    		}
    	}
    //start the JPanel
    	class pnl extends JPanel 
    	{
    //JPanel constructor
    		public pnl()
    		{
    			setVisible(true);
    			setBorder(BorderFactory.createLineBorder(Color.black));
    			setBackground(Color.red);
    			setLayout(null);
    			t.start();
    			//ball Ball = new ball();
    		}
    
    
    //paint the ballw
    		public void paintComponent(Graphics g)
    		{
    			super.paintComponent(g);
    			g.fillOval(ball.x,ball.y,ball.width,ball.height);
    			g.fillRect(paddle.x,paddle.y,paddle.width,paddle.height);
    		}
    //timers
    	Timer t = new Timer(10,new ActionListener(){public void actionPerformed(ActionEvent event){
    		checkmove();
    		bound();
    		ball.x += ball.dx;
    		ball.y += ball.dy;
    		repaint();
    	}});
    //player paddle movement
    	Timer p = new Timer(10,new ActionListener(){public void actionPerformed(ActionEvent event){
    		checkmove();
    		bound();
    		paddle.y += paddle.dy;
    		repaint();
    	}});
    //move
    		public void checkmove()
    		{
    			if(ball.motion == true)
    			{
    				t.start();
    			}
    			else if(ball.motion == false)
    			{
    				t.stop();
    			}
    			if(paddle.motion == true && game.playable == true)
    			{
    				p.start();
    			}
    			else if(paddle.motion == false && game.playable == true)
    			{
    				p.stop();
    			}
    		}
    //set boundaries
    		public void bound()
    		{
    //ball boundaries		
    			if(ball.x > ball.ballboundsw || ball.x == ball.ballboundsw)
    			{
    				ball.x = ball.ballboundsw;
    				ball.dx = -ball.dx;
    			}
    			if(ball.x < 0 || ball.x == 0)
    			{
    				ball.x = 0;
    				ball.dx = -ball.dx;
    			}
    			if(ball.y > ball.ballboundsh || ball.y == ball.ballboundsh)
    			{
    				ball.y = ball.ballboundsh;
    				ball.dy = -ball.dy;
    			}
    			if(ball.y < 0 || ball.y == 0)
    			{
    				ball.y = 0;
    				ball.dy = -ball.dy;
    			}
    //collision between A) ball and B) paddle
    			if(collision.checkcollide(ball.x,ball.y,ball.width,ball.height,paddle.x,paddle.y,paddle.width,paddle.height) == true)
    			{
    				ball.dx = -ball.dx;
    			}
    //paddle boundaries
    			if(paddle.y <= 0)
    			{
    				paddle.y = 0;
    			}
    			if(paddle.y >= paddle.paddleboundsh)
    			{
    				paddle.y = paddle.paddleboundsh;
    			}
    		}
    //end of JPanel
    	}
    }
    class ball
    {
    	 static int x = 250;
    	 static int y = 100;
    	 static int height = 50;
    	 static int width = 50;
    	 static int dx = 2;
    	 static int dy = 2;
    	 static int ballboundsh = game.game_height - height;
    	 static int ballboundsw = game.game_width - width;
    	 static boolean motion = true;
    }
    
    class paddle
    {
    	static int x =50;
    	static int y = 250;
    	static int width = 50;
    	static int height = 200;
    	static int dy = 2;
    	static int paddleboundsh = game.game_height - height;
    	static boolean motion = false;
    }
    //called to check and define collision events
    class collision
    {
    	public static boolean checkcollide(int x,int y,int width, int height, int x2, int y2, int width2, int height2)
    	{
    		boolean hit = false;
    	//top left corner
    		if(x < x2 + width2 && x > x2)
    		{
    			if(y < y2 + height2 && y > y2)
    			{
    				hit = true;
    			}
    		}
    	//top right corner
    		else if( x + width > x2 && x + width < x2 + width2)
    		{
    			if( y < y2 && y > y2 + height2)
    			{
    				hit = true;
    			}
    		}
    	//bottom left corner
    		else if( x > x2 && x < x2 + width2)
    		{
    			if( y + height > y2 && y + height < y2 + height2)
    			{
    				hit = true;
    			}
    		}
    	//bottom right corner
    		else if( x + width > x2 && x + width < x2 + width2)
    		{
    			if( y + height > y2 && y + height < y2 + height2)
    			{
    				hit = true;
    			}
    		}
    //otherwise
    		else
    		{
    			hit = false;
    		}
    		return hit;
    	}
    }
    //end of program

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,277
    Rep Power
    25

    Default

    I have looked over my code several times and cant find the problem.
    In addition to looking at it, I'd suggest that you try debugging it by adding printlns to show the values of the variables as they are changing and each time they are tested. If you understand what the conditions are for a collision then you should be able to look at the values of the variables as the code exeuctes and see where the error is.

Similar Threads

  1. if else not working
    By silverglade in forum New To Java
    Replies: 5
    Last Post: 05-12-2011, 07:29 PM
  2. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  3. Why is my Do/While Not working?
    By Meta in forum New To Java
    Replies: 1
    Last Post: 05-11-2010, 06:05 PM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •