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

  • Home
  • SEARCH
  • 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 1034959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:28:23+00:00 2026-05-16T14:28:23+00:00

For my tile-based game, I need to calculate direction based on a given point

  • 0

For my tile-based game, I need to calculate direction based on a given point offset (difference between two points). For example, let’s say I’m standing at point (10, 4) and I want to move to point (8, 6). The direction I move at is north-west. What would be the best way to calculate this?

Here’s me basic implementation in Java.

public int direction(int x, int y) {
    if (x > 0) {
        if (y > 0) {
            return 0; // NE
        } else if (y < 0) {
            return 1; // SE
        } else {
            return 2; // E
        }
    } else if (x < 0) {
        if (y > 0) {
            return 3; // NW
        } else if (y < 0) {
            return 4; // SW
        } else {
            return 5; // W
        }
    } else {
        if (y > 0) {
            return 6; // N
        } else if (y < 0) {
            return 7; // S
        } else {
            return -1;
        }
    }
}

Surely it can be optimised or shortened. Any help? Thanks.

  • 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-05-16T14:28:24+00:00Added an answer on May 16, 2026 at 2:28 pm

    I think the easiest to understand way would be making a static array that contains the values for all cases.

    // Won't say anything about how much these values make sense
    static final int[][] directions = {
        {3,  6, 0},
        {5, -1, 2}, // -1 for "no direction", feel free to replace
        {4,  7, 1}
    };
    
    public int direction(int x, int y) {
        x = (x < 0) ? 0 : ((x > 0) ? 2 : 1);
        y = (y < 0) ? 0 : ((y > 0) ? 2 : 1);
    
        return directions[y][x];
    }
    

    Edit: Now it’s correct (why are so many languages missing a proper sgn function?)

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

Sidebar

Related Questions

I'm making a tile based game in AS3. Now I need to check a
I'm making a 2D tile based game and I need to make part of
I have a tile based game and I need to find the closest tile
I'm writing a little tile-based game, for which I'd like to support light sources.
I'm creating a 2d tile based sim game. I have a 2d array of
For some time I've been making a 2d tile based sim game, and it's
Can anyone recommend a good Java game engine for developing simple tile-based games? I'm
I am building a tile based app in Python using pyglet/openGL wherein I'll need
Im currently creating a tile-based game for android. Using java via dalvik JVM. im
How can you map negative map coordinates in a 2D tile based game? ex.

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.