14 Replies - 6052 Views - Last Post: 03 February 2013 - 06:01 AM Rate Topic: -----

#1 vladim0105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-March 09

Java Delayed Collision Detection

Posted 07 January 2013 - 08:23 AM

Hey!

I have a small problem regarding collision detection, you see... My detection is sometimes slightly delayed (30ms maybe?). Because of this delay the player can sometimes walk trough half of the wall and then gets stopped other times he stops at first contact (which is what I want all the time to happen) and sometimes he just walk right through the wall.

Code samples:

The KeyBoard input for the player (I believe that I have messed up some if/else statements here):

	public void keyPressed(KeyEvent e)
	{
		
		int key = e.getKeyCode();

			if (key == KeyEvent.VK_D && walkRight == true)
			{
				
					Screen.movementX=-2;
				
				
			}
			else if (key == KeyEvent.VK_A && walkLeft == true)
			{
	
					Screen.movementX=2;
				
					
				
			}
			else if (key == KeyEvent.VK_S && walkDown == true)
			{

					Screen.movementY=-2;
				
					
				
			}
			else if (key == KeyEvent.VK_W && walkUp == true)
			{

					Screen.movementY=2;
				
					
				
			}
			
			
			
			
			
			if (key == KeyEvent.VK_D && walkRight == false)
			{
				
					Screen.movementX=2;
				
				
			}
			else if (key == KeyEvent.VK_A && walkLeft == false)
			{
	
					Screen.movementX=0;
				
					
				
			}
			else if (key == KeyEvent.VK_S && walkDown == false)
			{

					Screen.movementY=0;
				
					
				
			}
			else if (key == KeyEvent.VK_W && walkUp == false)
			{

					Screen.movementY=0;
				
					
				
			}


		



	}

Collision loop:
		p.walkUp = true;
		p.walkDown = true;
		p.walkLeft = true;
		p.walkRight = true;
		for (int i = 0; i < wallTileArr.size(); i++)
		{
			wallTile = wallTileArr.get(i);
			wallTile.collision(p);
			//System.out.println(p.walkDown);
		}


Collision code:

	public void collision(Player p)
	{
		if(getBounds().intersects(p.upGetBounds()))
		{
			
			p.walkUp = false;
			
		}
		else if(getBounds().intersects(p.downGetBounds()))
		{
			
			p.walkDown = false;
			
		}
		else if(getBounds().intersects(p.leftGetBounds()))
		{
			
			p.walkLeft = false;
			
		}
		else if(getBounds().intersects(p.rightGetBounds()))
		{
			
			p.walkRight = false;
			
		}
	}



I personally think the problem lies in either the loop or my if/else statements. And sometimes the code requires to be seen through different eyes(which is why I'm posting here) to see the problem. Searching for this on Google(Java Collision Delays) doesn't show up relevant posts (maybe I am using wrong keywords?).

Feel free to ask for more code samples!

Is This A Good Question/Topic? 0
  • +

Replies To: Java Delayed Collision Detection

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: Java Delayed Collision Detection

Posted 07 January 2013 - 09:56 AM

Edit: Removed my initial reply. The more I think about it, the less sense it makes. :)/> I would not do movement this way.

Maybe it's a synchronicity problem. Your KeyListener code executes in the even dispatch thread, and IF you have you collision() executing on your games's main thread, it may not be immediately picking up on the value of the boolean changing.

I don't think we have much to go on.

Or, maybe I am completely off and have no idea what I am talking about. :)/>

This post has been edited by farrell2k: 07 January 2013 - 10:07 AM

Was This Post Helpful? 0
  • +
  • -

#3 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Java Delayed Collision Detection

Posted 07 January 2013 - 10:19 AM

On a simpler note:

Is line 45:

 Screen.movementX=2;


meant to be that? Your other lines suggest that this value should be 0. I'm not entirely sure how your code pieces interact with each other, but if you are walking to the right, I think you will not be stopped.
Was This Post Helpful? 1
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: Java Delayed Collision Detection

Posted 07 January 2013 - 10:24 AM

View PostMylo, on 07 January 2013 - 05:19 PM, said:

On a simpler note:

Is line 45:

 Screen.movementX=2;


meant to be that? Your other lines suggest that this value should be 0. I'm not entirely sure how your code pieces interact with each other, but if you are walking to the right, I think you will not be stopped.


Good catch.
Was This Post Helpful? 0
  • +
  • -

#5 vladim0105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-March 09

Re: Java Delayed Collision Detection

Posted 07 January 2013 - 12:28 PM

View PostMylo, on 07 January 2013 - 10:19 AM, said:

On a simpler note:

Is line 45:

 Screen.movementX=2;


meant to be that? Your other lines suggest that this value should be 0. I'm not entirely sure how your code pieces interact with each other, but if you are walking to the right, I think you will not be stopped.


It's just me testing different stuff. It doesn't matter which direction I collide with, the "delay" still happens :)

View Postfarrell2k, on 07 January 2013 - 09:56 AM, said:

Edit: Removed my initial reply. The more I think about it, the less sense it makes. :)/>/> I would not do movement this way.

Maybe it's a synchronicity problem. Your KeyListener code executes in the even dispatch thread, and IF you have you collision() executing on your games's main thread, it may not be immediately picking up on the value of the boolean changing.

I don't think we have much to go on.

Or, maybe I am completely off and have no idea what I am talking about. :)/>/>



How would you have done the movement?
Was This Post Helpful? 0
  • +
  • -

#6 vladim0105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-March 09

Re: Java Delayed Collision Detection

Posted 07 January 2013 - 12:49 PM

Hmm... after some testing I figured out that if the player moves in one direction for a "long" time (2sec's or so) then it will detect perfectly, but otherwise no. Any clues? Anyone?
Was This Post Helpful? 0
  • +
  • -

#7 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: Java Delayed Collision Detection

Posted 07 January 2013 - 05:12 PM

All my moveable game objects have a dx and dy, denoting speeds for those directions, and would inherit an interface, Moveable, which contains a move() method that is called every iteration through my game loop.

public void move() {
    x += dx;
    y += dy;
}



When dx and dy are 0, my object does not move. Then when I want to move them, I just:

//move an object.
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_LEFT) {
        dx = -1;
    }
}

//stop an object.
public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_LEFT) {
        dx = 0;
    }
}



It eliminates the need for any movement booleans.

View Postvladim0105, on 07 January 2013 - 07:49 PM, said:

Hmm... after some testing I figured out that if the player moves in one direction for a "long" time (2sec's or so) then it will detect perfectly, but otherwise no. Any clues? Anyone?

It sounds like a synchronicity problem. Are you executing your game loop in a Swing Timed on the event dispatch thread, or are you using a seperate Thread with Thread.sleep() on a different thread, such as main()?

This post has been edited by farrell2k: 07 January 2013 - 05:15 PM

Was This Post Helpful? 2
  • +
  • -

#8 vladim0105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-March 09

Re: Java Delayed Collision Detection

Posted 08 January 2013 - 08:05 AM

View Postfarrell2k, on 07 January 2013 - 05:12 PM, said:

It sounds like a synchronicity problem. Are you executing your game loop in a Swing Timed on the event dispatch thread, or are you using a seperate Thread with Thread.sleep() on a different thread, such as main()?


I am using a Swing timer to update game, Is it a bad way to loop a game?
Was This Post Helpful? 0
  • +
  • -

#9 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: Java Delayed Collision Detection

Posted 08 January 2013 - 09:53 AM

View Postvladim0105, on 08 January 2013 - 03:05 PM, said:

View Postfarrell2k, on 07 January 2013 - 05:12 PM, said:

It sounds like a synchronicity problem. Are you executing your game loop in a Swing Timed on the event dispatch thread, or are you using a seperate Thread with Thread.sleep() on a different thread, such as main()?


I am using a Swing timer to update game, Is it a bad way to loop a game?


Nope. It is perfect for 99% of games. Good job.
Was This Post Helpful? 2
  • +
  • -

#10 vladim0105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-March 09

Re: Java Delayed Collision Detection

Posted 11 January 2013 - 10:02 AM

View Postfarrell2k, on 08 January 2013 - 09:53 AM, said:

View Postvladim0105, on 08 January 2013 - 03:05 PM, said:

View Postfarrell2k, on 07 January 2013 - 05:12 PM, said:

It sounds like a synchronicity problem. Are you executing your game loop in a Swing Timed on the event dispatch thread, or are you using a seperate Thread with Thread.sleep() on a different thread, such as main()?


I am using a Swing timer to update game, Is it a bad way to loop a game?


Nope. It is perfect for 99% of games. Good job.



Then I dont get it. What is the problem?
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Delayed Collision Detection

Posted 11 January 2013 - 01:14 PM

At which frequency your Swing.Timer triggers its actionPerformed() ?
Was This Post Helpful? 0
  • +
  • -

#12 vladim0105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-March 09

Re: Java Delayed Collision Detection

Posted 12 January 2013 - 08:28 AM

View Postpbl, on 11 January 2013 - 01:14 PM, said:

At which frequency your Swing.Timer triggers its actionPerformed() ?


t = new Timer(5, this);


5ms? Posted the code above in case its not in "ms"
Was This Post Helpful? 0
  • +
  • -

#13 vladim0105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-March 09

Re: Java Delayed Collision Detection

Posted 01 February 2013 - 10:18 AM

Alright, figured out the problem but haven't been able to fix it. It seems that the java timer is meant for small tasks, or else : Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes. - java docs.

I've tried to change from java timer loop to a thread loop, doesn't fix it :(/>
Altough I believe something called "Swing Worker" may fix this problem.
Was This Post Helpful? 0
  • +
  • -

#14 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: Java Delayed Collision Detection

Posted 01 February 2013 - 01:17 PM

No. That's not it, either. That's a util.Timer, not a Swing timer. The error likely has nothing to do with your Swing timer, and I highly doubt your game logic and even painting would even take 5ms. That's a large amount of time. Just for the heck of it, try pbl's advice and set the timer to execute at 16ms or above. 5ms is kind of pointless because your monitor can't even refresh the screen that fast.

I still think it's probably something with your movement booleans.
Was This Post Helpful? 0
  • +
  • -

#15 vladim0105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 22-March 09

Re: Java Delayed Collision Detection

Posted 03 February 2013 - 06:01 AM

View Postfarrell2k, on 01 February 2013 - 01:17 PM, said:

I still think it's probably something with your movement booleans.



Well, the code is all on the first post, I don't see any problems there...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1