Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

What is the best way performance wise to do this? When I click a tile I want it to get the distance and if I am close enough I can interact with the tile. One way would be to find the tile by doing mouse / tile width when I click correct? But then how would I get that tiles position? I know how to find the distance I just don't know how to get a certain tiles position from the array when I click it

public void checkDistance(GameContainer gc) {
    Input input = gc.getInput();

    float mouseX = input.getMouseX();
    float mouseY = input.getMouseY();

    double mousetileX = Math.floor(mouseX/tiles.tileWidth);
    double mousetileY = Math.floor(mouseY/tiles.tileHeight);

    //Distance code
    double lengthX = Math.abs(playerX - mousetileX); 
    double lengthY = Math.abs(playerY - mousetileY);
    double distance = Math.sqrt((lengthX*lengthX)+(lengthY+lengthY)); 

    System.out.println(distance);
}
share|improve this question
    
How are the tiles and tile-array defined? How do you detect which tile was clicked? –  Markus Jarderot Jul 1 '12 at 8:08

1 Answer 1

up vote 2 down vote accepted

This would be better if there was more information however i going to make the following assumptions:

  • Your tiles are based off a 2d grid (from: slick2d and array)
  • your using doubles to store position (cast if not true)

First of to find the tile you clicked on you would as you say divide your mouse position to your tile position so

double mousetileX = Math.floor(mousex/tileWidth);
double mousetileY = Math.floor(mousey/tileHeight);

NOTE you may need to take into account your view port offset if the map bigger than screen.

Now you have your click co-ordinates and you will already know your position co-ordinates there is a way to get the distance to them namely Pythagoras theorem

Taken from Math Open Reference

Basically states that on a right angle triangle the length of the hypotenuse (the longest side) is the square root of the sum of the squares of the other two sides. Wait a minute we dont have a triangle... sure we do.

If you draw a line between your coords then and then two more lines from that to meet at a 90 degree angle there it is.

To do this programmatically we first find the length of our first side.

//side 1
double lengthx = Math.abs(positionx-mousetileX); //abs to ensure positive.

//side2
double lengthy = Math.abs(positiony-mousetiley);

now we simply follow the formula

double distance = Math.sqrt((lengthx*lengthx)+(lengthy*lengthy));

And distance will be the length of the longest side ie your distance between your tiles.

UPDATE

Almost forgot what i have shown how to calculate is the relative distance between tiles on your grid not taking into account the tileWidth height to allow them to dynamically be resized etc.

UPDATE2

Also when calculating player position relative to your grid it is pretty similar to the mouse calculation.

double playertileX = Math.floor(playerx/tileWidth);
  double playertileY = Math.floor(playery/tileHeight);

Hope this helps

share|improve this answer
    
Looking at this it seems my math is correct, but it is getting the distance from 0,0 and not from my player position. Should I be using doubles instead of floats for all my positions? Will that fix it or is it something else. I will edit my code into main post –  Corey Jul 6 '12 at 16:19
    
I'm a bit confused on what I should do. I can post my whole class if needed –  Corey Jul 6 '12 at 16:27
    
playerX,playery should contain your players position, if they are 0 then it will show u distance from 0 make sure player values are being updated... I only using doubles as the math functions seem to return those so i dont have to cast every time. –  AbstractChaos Jul 6 '12 at 16:28
    
its lengthy*lengthy has to be squared :), you can post the whole class if it still dont work...or step through the code with a debugger and follow the values with a calulator –  AbstractChaos Jul 6 '12 at 16:29
    
oh oops thanks didn't notice that +. My player position is set at the middle of the screen and updates when I move my character. It is never at 0. Unless it's always getting the distance from the very first tile –  Corey Jul 6 '12 at 16:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.