aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Collision detection help please." Watch "Collision detection help please." New topic
Author

Collision detection help please.

Tom Farrell
Greenhorn

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");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

Box2Go bx2 =new Box2Go();
bx2.start();


}

@Override
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(box1.x,box1.y,box1.width,box1.height);
g.fillRect(box2.x,box2.y,box2.width,box2.height);

}

private class Box2Go extends Thread implements KeyListener
{
public void run()
{
addKeyListener(this);

while(true)
{
try
{

if (box2.intersects(box1))
{
System.out.println("HIT");
speed = -5;
}

repaint();

if (speed <5)
speed++;

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);
Thread.sleep(100);

}

catch (Exception e)
{
break;
}
}
}
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
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"farrell2k",
Please check your private messages regarding an important administrative matter.
-Ben


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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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.
 
Similar Threads
Problem with writing code.
can not move the paddle in this program
How to fix error in Rectangle Class problem in Java?
Sprite / Window Collision
Problem when repainting rectangles