Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Question Collision Detection, a little bit buggy.

    I've decided to try to learn the fundamentals of making a game. So, I started off by making a background and a character that moves. But then I wanted it so that when the character tries to go out of the frame, it's not allowed to. For the movement code, I used a tutorial that I modified to my needs because I didn't even know what classes from the Java library to use. But then I decided to just make my collision detection from scratch. But it's a little bit buggy. It won't detect and stop the character for a couple of milliseconds. Here's my code:
    Java Code:
    if(key == KeyEvent.VK_UP) {
    			if(getUpLocked() == false) {
    				if(getCharY() != 0 && getCharY() > 0) {
    					yChange = -1;
    					setDownLocked(false); //allows you to move if you haven't collided
    				}
    				else {
    					yChange = 0;
    					charY = 0;
    					setUpLocked(true); //doesn't allow you to move if you collide
    				}
    			}
    		}
    I don't know if this is enough of my code to find the source of the problem but that's why I offered the source code.
    Attached Files Attached Files
    Last edited by iamsothecoolest; 08-03-2011 at 10:41 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    It won't detect and stop the character for a couple of milliseconds
    What stops the character?
    Do you mean the x,y position continues to change and the paint method continues to draw the character after the collision was detected. What is the disconnect between the detection and the stopping?
    Is there any relationship between when the user presses a key and when the collision is detected? Can the character continue to move if the key is not pressed?
    Without seeing the code, we can only guess why the motion doesn't stop IMMEDIATELY when the flag is set.
    Last edited by Norm; 08-03-2011 at 11:47 PM.

  3. #3
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    The easiest way to see what I'm talking about would be to compile the code in the attachment

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Have you looked at the points I asked about?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Add some printlns to show the values of x and y as the character moves around. Look at what the values are when it goes out of the frame. Compare those with where the edges of the frame are.

  6. #6
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Oh, I actually already had the showing coordinates idea implemented . But the coordinates where it goes out of the frame varies each time with the furthest being around 100 pixels out of the frame.
    EDIT: Like I said, the easiest way to see would be to compile the program in the .zip file I attached.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    You need to do some more debugging:
    Add some printlns to your code to show the x,y values of the character each time it is drawn. Add printlns where the x,y position of the character changes. Add printlns where the collision is detected. See if it continues making changes to the x,y after the collision is detected.

  8. #8
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Unrelated to the question, but this is redundant:
    Java Code:
    if(getCharY() != 0 && getCharY() > 0) {
    If the second condition is true, the first is going to be true as well, so there's no need to test for both.

  9. #9
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Iron Lion View Post
    Unrelated to the question, but this is redundant:

    If the second condition is true, the first is going to be true as well, so there's no need to test for both.
    That was just me being a stupid experimenter.

    EDIT: Got rid of the first condition of the if statement. That was the one that was unecessary.
    Last edited by iamsothecoolest; 08-03-2011 at 11:57 PM.

  10. #10
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Have you tried compiling it yet?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Yes, that's how I know what to tell you about how to debug it.

Similar Threads

  1. Java3D collision detection
    By abbeywell in forum Java Gaming
    Replies: 5
    Last Post: 04-27-2011, 02:33 PM
  2. Really Need help with some collision detection
    By Harwad in forum New To Java
    Replies: 1
    Last Post: 01-23-2011, 12:38 AM
  3. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 06:13 PM
  4. Collision Detection (Game)
    By mscwd in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 01-28-2008, 08:34 PM
  5. Two Problems Rotating and collision detection help
    By jaferris in forum Java Applets
    Replies: 2
    Last Post: 01-07-2008, 11:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •