Results 1 to 3 of 3
  1. #1
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default CollisionHandler does the wrong thing

    So I got myself a pong applet, but there are still quite a few problems with collision on which I don't know the answer. (or not a proper one)

    1. The ball increments its position with 4 (negative or positive) in x and y directions. And the player works with a Keylistener that increments the position of the paddle with 10 if a keyevent occurs. Now this means that the chance may occur that the paddle is faster than the ball and that ball get stuck inside the paddle. What would be a way to fix this easily? (this actually occurs quite often)

    2. In the same way it might be possible to "push" the ball offscreen. I fixed this already for the AI by saying that he should stop moving when the ball is beyond his x-value, but I can't fix this in the same way for the player paddle.

    3. As I have also programmed the "sides" of the paddle to be able to bounce of the ball ive ot myself a lil bit in trouble. Seeing that it happens quite often that the ball is reflected 180 degrees while it should just be 90C (its hard to explain this thing, best is just to play around with the applet)

    Source code and applet are enclosed for your convenience. (I didn't use any other classes then the applet one)

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*; 
    import java.util.Random;
    
    public class main extends Applet implements Runnable,KeyListener {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 2305235281454935668L;
    
    	/**
    	 * @param args
    	 */
    	int y1,y2,xball,yball,radius,vx,vy;
    	int FontWidth;
    	int paddley=60;
    	int paddlex=20;
    	boolean pause,first;
    	Graphics offscr;
    	Thread runner;
    	int[] Score = new int[] {0,0};
    	static int height,width;
    	Image offscreenImage,PauseImg,StartImg,Win,Lose,Title;
    	Random random = new Random();
    	Font font2 = new Font("Verdana",Font.PLAIN, 11);
    	Font font1 = new Font("Verdana",Font.BOLD, 25);
    	
    	public void init()
    		{
    		this.setSize(650,400);
    		runner = new Thread(this);
    		width = getWidth();
    		height = getHeight();
    		reset();
    		radius=10;
    		vx=-4;
    		vy=4;
    		offscreenImage = createImage(width, height);
    		offscr = offscreenImage.getGraphics();
    		addKeyListener(this);
    		first=true;
    		PauseImg = getImage(getCodeBase(), "pause.png");
    		StartImg = getImage(getCodeBase(), "start.png");
    		Win = getImage(getCodeBase(), "Win.png");
    		Lose = getImage(getCodeBase(), "Lose.png");
    		Title = getImage(getCodeBase(), "title.png");
    		setFocusable(true);
    		requestFocus();
    		runner.start();
    		} 
    	
    	public void paint(Graphics g)
    	{
    		offscr.setFont(font2);
    		offscr.setColor(Color.black);
    		offscr.fillRect(0,0,width,height);
    		offscr.setColor(Color.white);
    		offscr.fillRect(20,y1-30,paddlex,paddley);
    		offscr.fillRect(width-40,y2-30,paddlex,paddley);
    		drawDashedLine(offscr,width/2,0,height,15);
    		FontMetrics fm = g.getFontMetrics();
    		FontWidth = fm.charWidth(Character.forDigit(Score[0], 10));
    		offscr.drawString(String.valueOf(Score[0]), width/2-10-(FontWidth*String.valueOf(Score[0]).length()), 20);
    		offscr.drawString(String.valueOf(Score[1]),width/2+10,20);
    		if(pause)
    		{
    			offscr.drawImage(PauseImg,width-30,height-30,25,25,this);
    		}
    		else
    		{
    			offscr.drawImage(StartImg, width-30, height-30, 25, 25,this);
    		}
    		offscr.setColor(Color.yellow);
    		offscr.fillOval(xball-radius, yball-radius, 2*radius, 2*radius);
    		if ((Score[0]==5) | (Score[1]==5)) 
    		{
    			if (Score[0]==5)
    			{
    				offscr.drawImage(Win,width/2-162,height/2-100,325,200,this);
    			}
    			else
    			{
    				offscr.drawImage(Lose,width/2-162,height/2-100,325,200,this);
    			}
    			offscr.setColor(Color.black);
    			offscr.setFont(font1);
    			offscr.drawString(String.valueOf(Score[0])+"/"+String.valueOf(Score[1]),width/2+10,250);
    		}
    		if (first)
    		{
    			offscr.drawImage(Title,width/2-162,height/2-100,325,200,this);
    		}
    		g.drawImage(offscreenImage, 0, 0, this);
    	}
    	
    	public void update(Graphics g) 
    	{
    	paint(g);
    	}
    	
    	public void drawDashedLine(Graphics g,int x,int y1,int y2,int dashlength) {
    		int length=y2-y1;
    		for (int i=0;i<length ;i+=2*dashlength)
    		{
    			g.drawLine(x, y1+i, x, y1+i+dashlength);
    		}
    	}
    	
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
    	    int keyCode = e.getKeyCode();
    	    if (!pause)
    	    {
    	    switch( keyCode ) { 
    	        case KeyEvent.VK_UP:
    	        	if (y1>0)
    	        	{
    		            y1=y1-10;
    	        	}
    	            break;
    	        case KeyEvent.VK_DOWN:
    	        	if (y1<height)
    	        	{
    		            y1=y1+10;
    	        	}
    	            break;
    	     }
    	    }
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		int keyCode = e.getKeyCode();
    		if ((keyCode==KeyEvent.VK_SPACE)&&(Score[0]!=5)&&(Score[1]!=5)&&(!first))
    		{
    			pause=!pause;
    		}
    		else if(keyCode==KeyEvent.VK_ESCAPE)
    		{
    			reset();
    			Score[0]=0;
    			Score[1]=0;
    			if (first)
    			{
    				first=!first;
    			}
    		}
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	
    	public void reset()
    	{
    		y1 = height/2;
    		y2 = y1;
    		yball=height/2;
    		xball=width/2;
    		pause=true;
    	}
    	
    	public void AI()
    	{
    		if (xball<width-40){
    		if (y2-yball<-20)
    		{
    			if (xball<width/2)
    			{
    				y2=y2+random.nextInt(4);
    			}
    			else
    			{
    				y2=y2+random.nextInt(9);
    			}
    		}
    		else if (y2-yball>20)
    		{
    			if (xball<width/2)
    			{
    				y2=y2-random.nextInt(4);
    			}
    			else
    			{
    				y2=y2-random.nextInt(9);
    			}
    		}
    		}
    	}
    	
    	@Override
    	public void run() 
    	{
    		while (true)
    		{
    		if (!pause)
    		{
    		xball=xball+vx;
    		yball=yball+vy;
    		CollisionHandler();
    		AI();
    		if ((Score[0]==5) | (Score[1]==5))
    		{
    			
    		}
    		}
    		repaint();
    		try
    		{	
    			Thread.sleep (20);
    		}
    		catch (InterruptedException ex){}
    		}
    	}
    	
    	public void CollisionHandler()
    	{
    		if ((xball-radius<0)|(xball+radius>width))
    		{
    			vx=-vx;
    			if (xball-radius<0)
    			{
    				Score[1]++;
    				reset();
    			}
    			else 
    			{
    				Score[0]++;
    				reset();
    			}
    		}
    		if ((yball-radius<0)|(yball+radius>height))
    		{
    			vy=-vy;
    		}
    		//CollisionHandler of left paddle and right paddle (front and sides)
    		if (((xball-radius<20+paddlex)&&(xball+radius>20)&&(yball+radius>y1-30)&&(yball+radius<y1-30+10))
    				|((xball-radius<20+paddlex)&&(xball+radius>20)&&(yball-radius<y1+30)&&(yball-radius>y1+30-10))
    				|((xball-radius<width-20)&&(xball+radius>width-20-paddlex)&&(yball+radius>y2-30)&&(yball+radius<y2-30+10))
    				|((xball-radius<width-20)&&(xball+radius>width-20-paddlex)&&(yball-radius<y2+30)&&(yball-radius>y2+30-10)))
    		{
    			vy=-vy;
    		}
    		if (((yball-radius<y1+30)&&(yball+radius>y1-30)&&(xball-radius<20+paddlex)&&(xball-radius>20+paddlex-10))
    				|((yball-radius<y1+30)&&(yball+radius>y1-30)&&(xball+radius>paddlex)&&(xball+radius<paddlex+10))
    				|((yball-radius<y2+30)&&(yball+radius>y2-30)&&(xball+radius>width-40)&&(xball+radius<width-40+10))
    				|((yball-radius<y2+30)&&(yball+radius>y2-30)&&(xball-radius<width-40+paddlex)&&(xball-radius>width-40+paddlex-10)))
    		{
    			vx=-vx;
    		}
    		
    		
    	}
    }
    The applet is "here"

    Thnx in advance

    Reskaillev

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I believe in the original pong game they made only the front portion of the paddle able to reflect the ball. Not the sides.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default

    Quote Originally Posted by Dark View Post
    I believe in the original pong game they made only the front portion of the paddle able to reflect the ball. Not the sides.
    Indeed, but I thought it would be nice if the sides reflected as well. Also I'm not making the original pong game XD

    It doesn't solve me other questions though... :s

Similar Threads

  1. JTable whole thing changes
    By MarkusHendersonicus in forum New To Java
    Replies: 3
    Last Post: 01-12-2011, 04:09 PM
  2. please i need help with a simple thing
    By zohdi in forum New To Java
    Replies: 28
    Last Post: 05-30-2010, 01:41 AM
  3. What did i do wrong on thing method?
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 08:37 AM
  4. PLz i really need help on this final thing
    By jason27131 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:31 AM

Posting Permissions

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