Results 1 to 3 of 3
- 07-24-2013, 06:56 PM #1
Member
- Join Date
- Jul 2013
- Posts
- 6
- Rep Power
- 0
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(); } } }
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); } }
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); } }
- 07-24-2013, 07:01 PM #2
Member
- Join Date
- Jul 2013
- Posts
- 5
- Rep Power
- 0
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?
- 07-24-2013, 07:01 PM #3
Re: Need Help With Collision Detection
scotthdez, please go through the Forum Rules, with special attention to the second paragraph.
db
THREAD CLOSEDIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
Collision detection
By mwr1976 in forum Java 2DReplies: 2Last Post: 04-14-2012, 01:45 PM -
Collision Detection
By Äppelpaj in forum Java 2DReplies: 1Last Post: 10-13-2011, 04:29 PM -
Collision Detection
By sunde887 in forum Java 2DReplies: 2Last Post: 10-08-2011, 12:40 AM -
Collision Detection?
By Alerhau in forum New To JavaReplies: 39Last Post: 09-07-2011, 05:55 PM -
Really Need help with some collision detection
By Harwad in forum New To JavaReplies: 1Last Post: 01-23-2011, 01:38 AM
Bookmarks