LiveWire Network Peer Answers Peer Support Teen Forums Tech Forums College Forums 825 users online 223512 members 1436 active today Advertise Here Sign In
TeenCollegeTechPhotos | Quizzes | LiveSecret | Memberlist | Dictionary | News | FAQ
Member Spotlight
HatesYou
Ohhh, my, my. Oh, hell, yes.
Mood: Euphoric
You have 1 new message.
Emergency Help
Until you sign up you can't do much. Yes, it's free.

Sign Up Now
Membername:
Password:
Already have an account?
Invite Friends
Active Members
Groups
Contests
Moderators
4 online / 54 MPM
World AIDS Day
Fresh Topics
  LiveWire / Technical Forums / Programming & Application Development / Adding Reply

Adding Reply
Archived Topic: It will not be bumped to the top of the forum.
Topic Java Rush Hour Game - Collision Detection
Membername   Not a member? Sign Up Free (takes 20 seconds)
Password   Forgotten your password?
Post

Font:   Size:   Color:

FAQ Keyword Search:
Post Options
Favorites Manager
Notify me of new replies to this topic by email
Notify me of new replies to this topic by private message
Original Post
geebs2006 Posted at 7:45 am on Dec. 2, 2007
Hi there folks - first post here.

I am making the Rush Hour Game. I have all my rectangles (cars) set up and they can be dragged about on the board horizontally or vertically on the board depending on their orientation. My problem which I'm having now is setting up collision detection.

Am I right in thinking that because i'm using x and y coordinates to define where each car is placed on the board that i can use the intersects method ie.

public boolean intersects(Rectangle rect)

or am I going down the wrong path here?

If its the right path then I have 8 cars so I guess I would use a for loop to make sure that car selected to be moved does not collide with any other car on the board - is this correct?

Any tips/pointers you can give me would be much appreciated. Below is my three classes that i have just now to get the game to stage of setting up the collision detection.

Thanks

Code:

/*
* RushHour.java
*
* Created on 26 November 2007, 18:50
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package rushhourgamefinal;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class RushHour {
   public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               createAndShowGUI();
           }
       });
   }

   private static void createAndShowGUI() {
       JFrame f = new JFrame("The Rush Hour Game");
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f.add(new RushHourPanel());
       f.setSize(317,337);
       f.setVisible(true);
   }
}

Code:

/*
* RushHourPanel.java
*
* Created on 26 November 2007, 18:52
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package rushhourgamefinal;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.BorderFactory;
import javax.swing.JPanel;


public class RushHourPanel extends JPanel {
   // Very easy to add othe cars to the board, just specify position, size and colour
   // Double array to enter x position, y position, width and height
   int[][] dims = {
       // x    y    w    h
       {  50, 100, 100,  50 }, {   0,   0, 100,  50 }, { 250,   0, 50, 150 },
       { 150,  50,  50, 150 }, {   0,  50,  50, 150 }, {   0, 200, 50, 100 },
       { 200, 200, 100,  50 }, { 100, 250, 150,  50 }
   };
   
   // Colours of the cars are entered here
   Color[] colors = {
       Color.red, Color.green, Color.yellow, Color.blue,
       Color.pink, Color.orange, Color.cyan, Color.black
   };
   Car[] cars;
   int selectedIndex;
   boolean drag = false;
   // added here
   boolean mouseInside, collide;
   // end of add
   final int OFFSET = 1;

   public RushHourPanel() {
       initCars();
       setBorder(BorderFactory.createLineBorder(Color.black));
       addMouseListener(new MouseAdapter(){
           public void mousePressed(MouseEvent e){
               Point p = e.getPoint();
               // Check to see if user has clicked on a car
               for(int j = 0; j < cars.length; j++) {
                   if(cars[j].contains(p)) {
                       selectedIndex = j;
                       drag = true;
                       break;
                   }
               }
           }

           public void mouseReleased(MouseEvent e) {
               drag = false;
           }
       });

       addMouseMotionListener(new MouseMotionAdapter(){
           public void mouseDragged(MouseEvent e){
               if(drag) {
                   moveCar(e.getX(),e.getY());
               }
            }
       });
   }

   private void moveCar(int x, int y){
       final int CURR_X = cars[selectedIndex].getX();
       final int CURR_Y = cars[selectedIndex].getY();
       final int CURR_W = cars[selectedIndex].getWidth();
       final int CURR_H = cars[selectedIndex].getHeight();
       final int OFFSET = 1;

       if ((CURR_X!=x) || (CURR_Y!=y)) {
           // The car is moving, repaint background
           // over the old car location.
           repaint(CURR_X,CURR_Y,CURR_W+OFFSET,CURR_H+OFFSET);
           // Update coordinates.
           if (CURR_W > CURR_H)
           {
                cars[selectedIndex].setX(x);
           }
           if (CURR_H > CURR_W)
           {
                cars[selectedIndex].setY(y);
           }
                             
           // Repaint the car at the new location.
           repaint(cars[selectedIndex].getX(), cars[selectedIndex].getY(),
           cars[selectedIndex].getWidth()+OFFSET,
           cars[selectedIndex].getHeight()+OFFSET);
       }
   }

   private void initCars() {
       cars = new Car[colors.length];
       for(int j = 0; j < cars.length; j++) {
           int x = dims[j][0];
           int y = dims[j][1];
           int w = dims[j][2];
           int h = dims[j][3];
           cars[j] = new Car(x, y, w, h, colors[j]);
       }
   }

   public Dimension getPreferredSize() {
       return new Dimension(300,300);
   }

   public void paintComponent(Graphics g) {
       super.paintComponent(g);
       for(int j = 0; j < cars.length; j++)
           cars[j].paintCar(g);
       // added here
       if(collide)
            g.drawString("Collision!", 10, 20);
       //end of add

   }
}

Code:

/*
* Car.java
*
* Created on 26 November 2007, 18:55
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package rushhourgamefinal;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;


class Car {
   private int xPos;
   private int yPos;
   private int width;
   private int height;
   Color color;

   Car(int x, int y, int w, int h, Color color) {
       this.xPos = x;
       this.yPos = y;
       this.width = w;
       this.height = h;
       this.color = color;
   }

   public void setX(int xPos){
       this.xPos = xPos;
   }

   public int getX(){
       return xPos;
   }

   public void setY(int yPos){
       this.yPos = yPos;
   }

   public int getY(){
       return yPos;
   }

   public int getWidth(){
       return width;
   }

   public int getHeight(){
       return height;
   }

   public boolean contains(Point p) {
       return new Rectangle(xPos, yPos, width, height).contains(p);
   }

   public void paintCar(Graphics g){
       g.setColor(color);
       g.fillRect(xPos,yPos,width,height);
       g.setColor(Color.BLACK);
       g.drawRect(xPos,yPos,width,height);
   }
}

Replies
Jof Posted at 7:45 am on Dec. 2, 2007
Post from this position was omitted due to content violations