Results 1 to 3 of 3
  1. #1
    scotthdez is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Need Help With Collision Detection

    Hi im new to java and i have been making a ping pong game, to start off java, and i have run into a problem that when i run it the collision detection for rectangles doesnt always work only at certain points of it being run. Please tell me what i have done wrong with the collision detection so i can move on with java.(p.s the collision method is in the Ball.java class)

    Main.java

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JFrame;
    
    public class Main extends JFrame implements Runnable {
    	private static final long serialVersionUID = 1L;
    	
    	public static String title = "Ping Pong v1.0";
    	public static Dimension size = new Dimension(600, 500);
    	
    	public static boolean running = false;
    	public static boolean isOp = false;
    	
    	Ball ball = new Ball(startY, startY);
    	Paddle p = new Paddle();
    	MouseHandler mh = new MouseHandler();	
    	
    	Thread thread;
    	
    	private Image dbImage;
    	private Graphics dbg;
    		
    	public static int startY = size.width / 2, startX = size.height / 2;
    			
    	public Main() {
    		thread = new Thread(this, "Display");
    		setPreferredSize(size);
    		setMaximumSize(size);
    		setMinimumSize(size);
    
    		setTitle(title);
    		pack();
    		setResizable(false);
    		setLocationRelativeTo(null);
    		setFocusable(true);		
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBackground(Color.BLACK);
    		setVisible(true);
    		addKeyListener(new AL());
    		addMouseListener(new MouseHandler());
    		addMouseMotionListener(new MouseHandler());
    		
    		start();
    		
    	}
    	
    	public synchronized void start() {
    		running = true;
    		thread.start();
    		
    	}
    	
    	public synchronized void stop() {
    		running = false;
    		try {
    			thread.join();
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	@Override
    	public void run() {
    		try {
    			while(running) {
    				tick();				
    				Thread.sleep(9);
    			}			
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public void tick() {
    		ball.tick();
    		p.tick();
    	}
    	
    	public void paint(Graphics g) {
    		dbImage = createImage(getWidth(), getHeight());
    		dbg = dbImage.getGraphics();
    		render(dbg);
    		g.drawImage(dbImage, 0, 0, this);
    	}
    
    	public void render(Graphics g) {
    		p.render(g);
    		ball.render(g);
    		
    		repaint();
    	}
    	
    	public static void main(String[] args) {
    		Main main = new Main();
    		
    	}
    	
    	public class AL extends KeyAdapter {
    		@Override
    		public void keyPressed(KeyEvent e) {
    			if (e.getKeyCode() == KeyEvent.VK_W) {
    				p.setP1YDirection(-1);			
    			} else if (e.getKeyCode() == KeyEvent.VK_S) {
    				p.setP1YDirection(+1);
    			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
    				p.setP2YDirection(-1);
    			} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    				p.setP2YDirection(+1);
    			}
    			
    		}
    		@Override
    		public void keyReleased(KeyEvent e) {
    			if (e.getKeyCode() == KeyEvent.VK_W) {
    				p.setP1YDirection(0);
    			} else if (e.getKeyCode() == KeyEvent.VK_S) {
    				p.setP1YDirection(0);
    			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
    				p.setP2YDirection(0);
    			} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    				p.setP2YDirection(0);
    			}
    			
    		}
    		
    	}
    	public class MouseHandler extends MouseAdapter {
    				
    		@Override
    		public void mouseMoved(MouseEvent e) {
    			int mx = e.getX();
    			int my = e.getY();
    			
    			if (Main.isOp) {
    				System.out.println("(X: " + mx + ", Y: " + my + ")");
    			}
    		}
    		
    		@Override
    		public void mousePressed(MouseEvent e) {
    			int mx = e.getX();
    			int my = e.getY();
    			
    			
    		}
    		
    	}
    	
    }
    Ball.java

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.util.Random;
    
    public class Ball {
    		
    	Rectangle Ball;
    	
    	int x, y;
    	int XDirection, YDirection;
    	
    	Paddle p = new Paddle();
    	
    	public Ball(int x, int y) {
    		this.x = x;
    		this.x = x;
    		
    		Ball = new Rectangle(x, y, 5, 5);
    		Random r = new Random();
    		int rDir = r.nextInt(1);
    		if (rDir == 0) {
    			rDir--;
    			setXDirection(rDir);
    		}
    		int yDir = r.nextInt(1);
    		if (yDir == 0) {
    			yDir++;
    			setYDirection(yDir);
    		}
    		
    	}
    	
    	public void tick() {
    		physics();
    	}
    	
    	public void setXDirection(int xdir) {
    		XDirection = xdir;
    	}
    	
    	public void setYDirection(int ydir) {
    		YDirection = ydir;
    	}
    		
    	public void physics() {
    		collision();
    		Ball.x += XDirection;
    		Ball.y += YDirection;
    		
    		if (Ball.x <= 0) {
    			setXDirection(+1);
    		}
    		if (Ball.x >= 592) {
    			setXDirection(-1);
    		}
    		if (Ball.y >= 492) {
    			setYDirection(-1);
    		}
    		if (Ball.y <= 25) {
    			setYDirection(+1);
    		}
    	}
    
    	public void collision() {
    		if (Ball.intersects(p.p1)) {
    			setXDirection(+1);
    			System.out.println("fd");
    		}
    		if (Ball.intersects(p.p2)) {
    			setXDirection(-1);
    			System.out.println("df");
    		}
    	}
    	
    	public void render(Graphics g) {
    		g.setColor(Color.WHITE);
    		g.fillRect(Ball.x, Ball.y, Ball.width, Ball.height);
    		
    	}
    		
    }
    Paddle.java

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    
    public class Paddle {
    	
    	Rectangle p1 = new Rectangle(40, Main.size.width / 2 - 70, 20, 54);
    	Rectangle p2 = new Rectangle(539, Main.size.width / 2 - 70, 20, 54);
    	
    	int P1YDirection;
    	int P2YDirection;
    		
    	public Paddle() {
    		
    	}
    	
    	public void tick() {
    		move();
    	}
    	
    	public void move() {
    		p1.y += P1YDirection;
    		p2.y += P2YDirection;
    		if (p1.y >= 442) {
    			p1.y = 442;
    		} else if (p1.y <= 25) {
    			p1.y = 25;
    		}
    		if (p2.y >= 442) {
    			p2.y = 442;
    		} else if (p2.y <= 25) {
    			p2.y = 25;
    		}
    		
    	}
    	
    	public void setP1YDirection(int ydir) {
    		P1YDirection = ydir;
    	}
    	
    	public void setP2YDirection(int ydir) {
    		P2YDirection = ydir;
    	}
    	
    	public void render(Graphics g) {
    		g.setColor(Color.BLUE);
    		g.fillRect(p1.x, p1.y, p1.width, p1.height);
    		g.fillRect(p2.x, p2.y, p2.width, p2.height);
    	}
    	
    }

  2. #2
    Di Immortalis is offline Member
    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Default Re: Need Help With Collision Detection

    What do you mean "doesnt always work only at certain points of it being run." You make it sound like every x seconds it works?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Need Help With Collision Detection

    scotthdez, please go through the Forum Rules, with special attention to the second paragraph.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Collision detection
    By mwr1976 in forum Java 2D
    Replies: 2
    Last Post: 04-14-2012, 01:45 PM
  2. Collision Detection
    By Äppelpaj in forum Java 2D
    Replies: 1
    Last Post: 10-13-2011, 04:29 PM
  3. Collision Detection
    By sunde887 in forum Java 2D
    Replies: 2
    Last Post: 10-08-2011, 12:40 AM
  4. Collision Detection?
    By Alerhau in forum New To Java
    Replies: 39
    Last Post: 09-07-2011, 05:55 PM
  5. Really Need help with some collision detection
    By Harwad in forum New To Java
    Replies: 1
    Last Post: 01-23-2011, 01:38 AM

Tags for this Thread

Posting Permissions

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