Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9204589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:44:44+00:00 2026-06-17T23:44:44+00:00

Possible Duplicate: Java rectangle collision detection confusion I have run into a problem with

  • 0

Possible Duplicate:
Java rectangle collision detection confusion

I have run into a problem with collision detection, I have asked around other forums and been searching Google for a month now (No one had an answer). My problem with collision detection is that it’s delayed or inaccurate, sometimes it detects it perfectly, sometimes halfway through and sometimes not at all. Some people say it’s a synchronicity problem between my update loop (using swing timer) and KeyInput. What do you think?

Code! 🙂

KeyInput:

  public void keyPressed(KeyEvent e)
{

    int key = e.getKeyCode();

        if (key == KeyEvent.VK_D && walkRight == true)
        {

                Screen.movementX=-1;


        }
        else if (key == KeyEvent.VK_A && walkLeft == true)
        {

                Screen.movementX=1;



        }
        else if (key == KeyEvent.VK_S && walkDown == true)
        {

                Screen.movementY=-1;



        }
        else if (key == KeyEvent.VK_W && walkUp == true)
        {

                Screen.movementY=1;



        }





        if (key == KeyEvent.VK_D && walkRight == false)
        {

                Screen.movementX =0;


        }
        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;



        }






}
public void keyReleased(KeyEvent e)
{
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_D)
    {
        Screen.movementX=0;
    }
    else if (key == KeyEvent.VK_A)
    {
        Screen.movementX=0;
    }
    else if (key == KeyEvent.VK_S)
    {
        Screen.movementY=0;
    }
    else if (key == KeyEvent.VK_W)
    {
        Screen.movementY=0;
    }
}

Collision:

public static void collision()
{
    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);
    }
}

And:

public void collision(Player p)
{
    if (p.downGetBounds().intersects(getBounds()))
    {
        p.walkDown = false;
    }

    else if (p.upGetBounds().intersects(getBounds()))
    {
        p.walkUp = false;
    }
    else if (p.leftGetBounds().intersects(getBounds()))
    {
        p.walkLeft = false;
    }
    else if (p.rightGetBounds().intersects(getBounds()))
    {
        p.walkRight = false;
    }

}

Switching JPanels (Using this for Main Menu etc)

public static void changePanelTo(Component add)
{
        Main.f.getContentPane().invalidate();
        Main.f.getContentPane().removeAll();
        Main.f.add(add);
        Main.f.validate();
        add.requestFocusInWindow();





}

Feel free to ask questions regarding the code!

Download: Click Here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:44:45+00:00Added an answer on June 17, 2026 at 11:44 pm

    If You are using java.util.Timer class then watch here .
    It Clearly stays that:

    Corresponding to each Timer object is a single background thread that
    is used to execute all of the timer’s tasks, sequentially. 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
    .

    So sometimes the TimerTask associated with your Timer class is fired on exact collision time but sometimes it takes a while because by that time your previous task might be completing..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
Possible Duplicate: Java string comparison? I have encounter the following problem, I have an
Possible Duplicate: Java GUI repaint() problem? I write a Java code, but I have
Possible Duplicate: Java: recommended solution for deep cloning/copying an instance I have an object
Possible Duplicate: How to draw a rectangle on a java applet using mouse drag
Possible Duplicate: Java Hashmap: How to get key from value? I have a hashmap
Possible Duplicate: Java char array to int I have the following code, where stackCells
Possible Duplicate: Java Byte Array to String to Byte Array I have a method
Possible Duplicate: java get file size efficiently I have a File called filename which
Possible Duplicate: Java: how to clone ArrayList but also clone its items? I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.