Followers 0

# Move towards a point algorithm

## 9 posts in this topic

I need a method that moves one point(this-object) to the specified coordinate, that uses integers to calculate this instead of decimals.

I have tested multiple algorithm found on the net. They all work the same and look identical ingame even if their implementation differs.
The problem is, since my game use integers as coordinates and not float, which the method found on the net use, the movespeed is not always the same. I mean, sometimes, the unit "walk" the specified speed, and sometimes it walks that specified movespeed + 1, which is unacceptable(I might sound picky, but it is noticeably ingame and can have major effect on gameplay).

I tried some things like casting and rounding but they are not helping. Here is the method.

public void moveToward(int targetX, int targetY, int steps)	//TODO:
{
if(posX == targetX && posY == targetY)
return;

int fX = targetX - posX;
int fY = targetY - posY;
double dist = Math.sqrt( fX*fX + fY*fY );
double step = (steps / dist);
posX += fX * step;
posY += fY * step;
}
Edited by P0jahn
0

##### Share on other sites
Store position as float coordinates at all times and only cast to an int when rendering. Unless you want to also keep track of remainders from division and accumulate them, incrementing the position integer when it overflows... painful.
1

##### Share on other sites

The latter would be less appealing tbh. I tried to switch to floats earlier today, but there are some many function that must use integers so I ditched the plans. So... would you mind elaborating your suggestion?

0

##### Share on other sites

Use floats to store the positions but cast them to ints for those functions that need ints (and rewrite your own functions that need int coords to use float coords). That's the best way to get additional granularity, even if it's a pain in the butt.

0

##### Share on other sites
Just cast the floats to an int when calling the functions then... unless they modify the position.<br /><br />Otherwise you need to look at fixed point numbers.
0

##### Share on other sites

One option is to use fixed point integers. That is, your integers (presumably) have 32 bits, decide on how many of them should be fractional (I use 12 fractional bits for my fixed point numbers).

It can take a while to get to grips with, you need to be comfortable with bit-shifting, and you'll need to find or write a decent library to manipulate fixed point numbers as there's no built in support, but it's a good solution in certain circumstances. I'm using them at the moment because I have plans to add lock-step multiplayer to my RTS and I don't want to have to worry about floating point inconsistencies across different compilers, processors, optimisation settings, etc.

0

##### Share on other sites
Yeah, that's what we did back in PS1 days. Every time you multiply a 12 fractional bit number by another, you need to shift down 12 bits afterwards to get the right answer. You can keep the accuracy high by storing the result & 4095 (just getting the fractional part) before shifting down, and accumulating it in another int. When it overflows to 4096, subtract 4096 from the remainder accumulator and add 1 << 12 to the fixed point value.

EDIT: This new forum update has knackered the post formatting hasn't it? I meant result logical anded with 4095 Edited by Paradigm Shifter
0

##### Share on other sites

The position are also indexes, so fixed point integers is not an option.

0

Wait, what?

0

##### Share on other sites

I'm confused. Your function is being passed the number of steps it is supposed to take to get from A to B. You also want these steps to be integers AND of equal size?

Tell me, what exactly do you expect to be the result of moving a distance of 3 to the right in 2 steps, using only integers?

2

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account