wood burning stoves
The moose likes Game Development and the fly likes Collision detection help please. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Collision detection help please." Watch "Collision detection help please." New topic

Collision detection help please.

Tom Farrell

Joined: Jun 18, 2008
Posts: 3
I have been racking my brain trying to figure out what is wrong with my collision detection, and I know it is something extremely simple that I am just not seeing, so maybe someone here can shed some light on it for me.

I have created this app that generates two rectangles, then checks for collision. When a collision occurs, the moving rectangle bounces off the other. Everything works great if I am moving around by only pressing one key at a time, but if I am holding down two opposite movement keys (w and s, or a and d) at the point where the two rectangle collide, the rectangles pass through one another.

I hope I've made myself clear. Thanks for any help

* To change this template, choose Tools | Templates
* and open the template in the editor.

package collide;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

* @author tj
public class Collide extends JFrame
final int WIDTH=800, HEIGHT=600, UP =0, DOWN =1, LEFT =2, RIGHT =3, STOP = 4;
int speed = 5;
int p1Direction = STOP;

Rectangle box1 = new Rectangle(350,250,100,100);
Rectangle box2 = new Rectangle(400,400,20,20);

public Collide()
super("Collision Detection Test");

Box2Go bx2 =new Box2Go();


public void paint(Graphics g)


private class Box2Go extends Thread implements KeyListener
public void run()


if (box2.intersects(box1))
speed = -5;


if (speed <5)

if (p1Direction == STOP)
box2.x = 400;
box2.y = 400;
box2.width = 20;
box2.height = 20;

if (p1Direction == UP)
box2.y -= speed;

if (p1Direction == DOWN)
box2.y += speed;

if (p1Direction == LEFT)
box2.x -= speed;

if (p1Direction == RIGHT)
box2.x += speed;

System.out.println(box2.y + " " + speed);


catch (Exception e)
public void keyTyped(KeyEvent event)
if (event.getKeyChar() == 'a')
p1Direction = LEFT;
if (event.getKeyChar() == 's')
p1Direction = DOWN;
if (event.getKeyChar() == 'd')
p1Direction = RIGHT;
if (event.getKeyChar() == 'w')
p1Direction = UP;


public void keyPressed(KeyEvent event)


public void keyReleased(KeyEvent event)


public static void main(String[] args)
Collide collide1 = new Collide();


[ June 18, 2008: Message edited by: farrell2k ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Please check your private messages regarding an important administrative matter.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Welcome to JavaRanch, and welcome to the wonderful world of multithreading!

So the basic idea is this: when the little box hits the big box, you switch the speed to -5. Then below that, you adjust the position based on the speed and on the current direction. This is the right thing to do if the little box is still moving in the direction is was when it hit. But key events can come in at any time. If you carefully time it so that, for example, you hit while going up, but then immediately press "down", then that negative speed is going to make the box go in the direction is was already going -- up into the big box!

I will sit and wait until you get that straight in your head.

OK, all clear?

The best way to fix this would be have an x velocity and a y velocity, and have the keyboard events and the hit detector both change those as needed, instead of having the UP, DOWN, etc constants. That would get rid of the "double negative" problem you're having here.

[Jess in Action][AskingGoodQuestions]
It is sorta covered in the JavaRanch Style Guide.
subject: Collision detection help please.
It's not a secret anymore!