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.
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.
/*
* 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
}
}
/*
* 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);
}
}