Sign in to follow this  
P0jahn

Move towards a point algorithm

Recommended Posts

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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this