Jump to content
  • Advertisement
Sign in to follow this  
P0jahn

Move towards a point algorithm

This topic is 2030 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

Share this post


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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!