Tom Farrell

Greenhorn
+ Follow
since Jun 18, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Tom Farrell

Synchronized methods for reading and writing the data.
Repaint not thread safe anymore.

I was always under the impression that JComponent.repaint() was always executed on the edt. Is this not the case anymore?
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 ]
15 years ago