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 9101341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:06:19+00:00 2026-06-17T01:06:19+00:00

I have implemented a simple tile-based collision system in my top-down game, but I

  • 0

I have implemented a simple tile-based collision system in my top-down game, but I am having problems with moving across corners. Currently, my programs takes the player’s center point and works out a tile on a 20px square grid (the alternating grey cbackground). It checks the up, down, left and right tiles for collisions, and if it finds one, it moves onto the next stage.

The next stage is to check where the player is within the tile. For this, I have a function called getCenterDistanceFromCurrentTile() (creative name, I know). This returns a Vector2i (x and y) of the distance from the center point of the tile, to the center point of the player. I then use this in the following ways;

  • If there is a wall tile to the left, and the player’s x distance < 0 (the player is to the left of the current tile’s centerpoint), the player is moved to the x of the tile next to the wall.
  • If there is a wall to the right, and the player’s x distance is > 0 (right of the current tile’s centerpoint), the player is moved to the x of the tile next to the wall.
  • This continues for the Y axis in the same way.

My problem is with diagonal. I do not check diagonal collisions currently, but it appears I will have to. I have already made an attempt to implement it, but the player jumps around when they hit a corner. I was using the above methods of player x < 0 and player y < 0 together, which didn’t work out. I have made some images to better explain my problem.

Problem image 1

This image demonstrates my collision system working fine, with a collision to the left (shown by the blue tile), and the pink tiles being checked, but no collision found. The green tile shows the player’s current tile, and the red square shows the player’s current location.

Problem image 2

This image clearly shows the problem I have. None of the pink tiles have collided with anything, therefore the player’s x and y do not need to be checked.

How should I check for corner collisions in my game?

My current collision checking code;

public boolean isWall(Vector2i loc)
{
    return this.isWall(loc.x, loc.y);
}

public boolean isWall(int x, int y)
{
    if (x < 0) return true;
    if (y < 0) return true;
    if (y > map[0].length-1) return true;
    if (x > map.length-1) return true;
    return map[x][y];
}

public void phys(Player plr) {
    Vector2i playerTile = plr.getTile();
    // Left
    if (isWall(playerTile.x-1, playerTile.y))
    {
        Vector2i distanceFromTile = plr.getCenterDistanceFromCurrentTile(true);
        if (distanceFromTile.x < 0)
        {
            plr.setX(playerTile.x*BLOCK_SIZE);
        }
    }
    // Right
    if (isWall(playerTile.x+1, playerTile.y))
    {
        Vector2i distanceFromTile = plr.getCenterDistanceFromCurrentTile(true);
        if (distanceFromTile.x > 0)
        {
            plr.setX(playerTile.x*BLOCK_SIZE);
        }
    }
    // Up
    if (isWall(playerTile.x, playerTile.y-1))
    {
        Vector2i distanceFromTile = plr.getCenterDistanceFromCurrentTile(true);
        if (distanceFromTile.y < 0)
        {
            plr.setY(playerTile.y*BLOCK_SIZE);
        }
    }
    // Down
    if (isWall(playerTile.x, playerTile.y+1))
    {
        Vector2i distanceFromTile = plr.getCenterDistanceFromCurrentTile(true);
        if (distanceFromTile.y > 0)
        {
            plr.setY(playerTile.y*BLOCK_SIZE);
        }
    }
}
  • 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-17T01:06:21+00:00Added an answer on June 17, 2026 at 1:06 am

    Your phys method could return boolean depending on whether the player is colliding with any of the walls. You could then use the result of this function to decide whether to move back to the previous location in space you were before you moved, or if you did not collide, to continue on.

    This would require you to check all of the adjacent walls around the player. This would introduce a much cleaner way of iterating through the collision checks in the phys method.

    Such an improvement would look like:

    public boolean phys(Player plr)
    {
        java.awt.Rectangle playerBounds = new java.awt.Rectangle(plr.x, plr.y, BLOCK_SIZE, BLOCK_SIZE);
    
        for (int y = 0; y < 3; y++)
        {
            for (int x = 0; x < 3; x++)
            {
                // Skip the tile the player is on assuming that he is not on a wall already
                if (x == 1 && y == 1) continue;
    
                java.awt.Rectangle bounds = new java.awt.Rectangle(/*tile at (x, y) xpos*/, /*tile at (x, y) ypos*/, BLOCK_SIZE, BLOCK_SIZE);
    
                if (bounds.intersects(playerBounds))
                {
                    return true;
                }
            }
        }
    
        // Did not collide
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a simple messaging system in WCF and have implemented a minimal
I am new in codeigniter. I have implemented a simple login system. I want
I've implemented a simple Bayesian classifier, but I'm running into some overflow problems when
I've been working on a simple tile-based terrain generator for fun, and have run
I have implemented simple proxy server using Java NIO channels, but have a problem,
While working with Akka, I have implemented a simple command line app. But Akka
I have implemented a simple state machine for an embedded system using C's switch
I am working on a 3d tile-based strategy game and have read that implementing
We have implemented simple LMS based on Basic Web Player which is part of
I have implemented a simple entity ejb with a @version annotation. I expect that

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.