0

I've been trying to get collision detection working in a Frogger type game I'm doing. Can't seem to get the hang of it though, would appreciate some help on getting it working, my code is below:

I was trying to do the collision detection with the following snippet (also seen below) but it doesn't quite do what I want. It just makes the test text appear on the screen when one of the moving circle reaches the x value of the 'frog'.

    if ( xCoords[0] + radius == movx)
        collision = true;

import java.applet.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;


public class AppletMove extends Applet implements Runnable, KeyListener
{
int movx = 400;
int movy = 300;
int i = 400;
int y = 20;
int dx = 2;
int dy = 2;
int radius = 10;
int[] coOrds = {20,550};
private Image img;
private Graphics grph;
boolean collision = false;

@Override   
public void init()
{
    setSize(800,600);
    addKeyListener(this);       
}

@Override   
public void start()
{
    Thread t = new Thread(this);

    t.start();
}

@Override
public void update(Graphics g) {
    if ( img == null)
    {
        img = createImage(this.getSize().width, this.getSize().height);
        grph = img.getGraphics();

    }

    grph.setColor(getBackground());
    grph.fillRect(0, 0, this.getSize().width, this.getSize().height);

    grph.setColor(getForeground());

    paint(grph);

    g.drawImage(img, 0, 0, this);
}

@Override
public void keyPressed(KeyEvent e) {

    switch(e.getKeyCode())
    {
        case KeyEvent.VK_LEFT:
            movx -=20;
            break;
        case KeyEvent.VK_RIGHT:
            movx +=20;
            break;
        case KeyEvent.VK_UP:
            movy -=20;
            break;
        case KeyEvent.VK_DOWN:
            movy +=20;
            break;              
    }

}

@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

int[] xCoords = {20,280,50,250,80,220,110,190};
int[] yCoords = {20,50,80,110,140,170,200,230};

int[] xCoords2 = {140,150,170,110,200,80,230,50};

@Override
public void run() {
    // TODO Auto-generated method stub
    while(true)
    {

        if ( xCoords[0] + radius == movx)
            collision = true;

        if ( xCoords[0] + dx > this.getWidth())
            xCoords[0]  = 10;
        xCoords[0] += dx;

        if ( xCoords[1] + dx < 0)
            xCoords[1]  = 290;
        xCoords[1] -= dx;

        if ( xCoords[2] + dx > this.getWidth())
            xCoords[2] = 10;
        xCoords[2] += dx;

        if ( xCoords[3] + dx < 0)
            xCoords[3] = 290;
        xCoords[3] -= dx;

        if ( xCoords[4] + dx > this.getWidth())
            xCoords[4] = 10;
        xCoords[4] += dx;

        if ( xCoords[5] + dx < 0)
            xCoords[5] = 290;
        xCoords[5] -= dx;

        if ( xCoords[6] + dx > this.getWidth())
            xCoords[6] = 10;
        xCoords[6] += dx;

        if ( xCoords[7] + dx < 0)
            xCoords[7] = 290;
        xCoords[7] -= dx;

        if ( xCoords2[0] + dx > this.getWidth())
            xCoords2[0]  = 10;
        xCoords2[0] += dx;

        if ( xCoords2[1] + dx < 0)
            xCoords2[1] = 290;
        xCoords2[1] -= dx;

        if ( xCoords2[2] + dx > this.getWidth())
            xCoords2[2] = 10;
        xCoords2[2] += dx;

        if ( xCoords2[3] + dx < 0)
            xCoords2[3] = 290;
        xCoords2[3] -= dx;

        if ( xCoords2[4] + dx > this.getWidth())
            xCoords2[4] = 10;
        xCoords2[4] += dx;

        if ( xCoords2[5] + dx < 0)
            xCoords2[5] = 290;
        xCoords2[5] -= dx;

        if ( xCoords2[6] + dx > this.getWidth())
            xCoords2[6] = 10;
        xCoords2[6] += dx;

        if ( xCoords2[7] + dx < 0)
            xCoords2[7] = 290;
        xCoords2[7] -= dx;

        //y += dy;

        repaint();          
        try {
            Thread.sleep(17);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public void paint(Graphics g)
{

    if(collision == true){
        g.setColor(Color.BLACK);
        g.drawString("Test", 50, 50);
    }
    g.setColor(Color.GREEN);
    g.fillOval(xCoords[0] - radius, yCoords[0] - radius, radius * 2, radius * 2);

    g.setColor(Color.RED);
    g.fillOval(xCoords[1] - radius, yCoords[1] - radius, radius * 2, radius * 2);

    g.setColor(Color.YELLOW);
    g.fillOval(xCoords[2]- radius, yCoords[2] - radius, radius * 2, radius * 2);

    g.setColor(Color.BLUE);
    g.fillOval(xCoords[3] - radius, yCoords[3] - radius, radius * 2, radius * 2);

    g.setColor(Color.PINK);
    g.fillOval(xCoords[4] - radius, yCoords[4] - radius, radius * 2, radius * 2);

    g.setColor(Color.BLACK);
    g.fillOval(xCoords[5] - radius, yCoords[5] - radius, radius * 2, radius * 2);

    g.setColor(Color.MAGENTA);
    g.fillOval(xCoords[6] - radius, yCoords[6] - radius, radius * 2, radius * 2);

    g.setColor(Color.GRAY);
    g.fillOval(xCoords[7] - radius, yCoords[7] - radius, radius * 2, radius * 2);

    g.setColor(Color.GREEN);
    g.fillOval(xCoords2[0] - radius, yCoords[0] - radius, radius * 2, radius * 2);

    g.setColor(Color.RED);
    g.fillOval(xCoords2[1] - radius, yCoords[1] - radius, radius * 2, radius * 2);

    g.setColor(Color.YELLOW);
    g.fillOval(xCoords2[2] - radius, yCoords[2] - radius, radius * 2, radius * 2);

    g.setColor(Color.BLUE);
    g.fillOval(xCoords2[3] - radius, yCoords[3] - radius, radius * 2, radius * 2);

    g.setColor(Color.PINK);
    g.fillOval(xCoords2[4] - radius, yCoords[4] - radius, radius * 2, radius * 2);

    g.setColor(Color.BLACK);
    g.fillOval(xCoords2[5] - radius, yCoords[5] - radius, radius * 2, radius * 2);

    g.setColor(Color.MAGENTA);
    g.fillOval(xCoords2[6] - radius, yCoords[6] - radius, radius * 2, radius * 2);

    g.setColor(Color.GRAY);
    g.fillOval(xCoords2[7] - radius, yCoords[7] - radius, radius * 2, radius * 2);

    g.setColor(Color.BLUE);
    Graphics2D g2 = (Graphics2D) g;
    g2.fill(new Ellipse2D.Double(movx, movy, 20, 20));
}

}

mKorbel
  • 109,107
  • 18
  • 130
  • 305
Him_Jalpert
  • 2,406
  • 9
  • 28
  • 55
  • 2
    Hints: Avoid applets, get it working using a `JPanel` and `JFrame`. Use [key bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) over `KeyListener`. Make use of the geometry classing in the [2D Graphics API](http://docs.oracle.com/javase/tutorial/2d/geometry/index.html), they already handle all this... – MadProgrammer Jan 22 '14 at 00:24
  • You seem to be only checking the first "obstacle" with your "frog". You may wish to consider using a loop, it will reduce the complexity of your code. You may also wish to check for collisions after you have update their positions... – MadProgrammer Jan 22 '14 at 00:28
  • I was only checking the first one for testing purposes, once I got it working with that one I was going to do it for the rest. And Mad, can't avoid using applets in this one, I'm helping out someone I tutor with this, it's for a school project he's doing. – Him_Jalpert Jan 22 '14 at 00:51
  • Is it a collision between circle and rectangle? Then have a look at this: http://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection Or is it a collision between 2 circles? Then you have to check, if the squared distance between them is small then the squared sum of the radiuses (dx² + dy² <= (r1+r2)²). If it is so there is a collision. – Robert P Jan 24 '14 at 13:09

0 Answers0