• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collision detection help please.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"farrell2k",
Please check your private messages regarding an important administrative matter.
-Ben
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic