Move towards a point algorithm

Started by
8 comments, last by Trienco 11 years, 3 months ago
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;
	}
Advertisement
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.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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?

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.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
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.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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.

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
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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

Wait, what?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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?

f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement