Jump to content

  • Log In with Google      Sign In   
  • Create Account


Move towards a point algorithm


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 P0jahn   Members   -  Reputation: 260

Like
0Likes
Like

Posted 27 December 2012 - 11:39 AM

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, 27 December 2012 - 11:45 AM.


Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5250

Like
1Likes
Like

Posted 27 December 2012 - 12:05 PM

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

#3 P0jahn   Members   -  Reputation: 260

Like
0Likes
Like

Posted 27 December 2012 - 12:22 PM

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?



#4 Khatharr   Crossbones+   -  Reputation: 2957

Like
0Likes
Like

Posted 27 December 2012 - 12:31 PM

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.

#5 Paradigm Shifter   Crossbones+   -  Reputation: 5250

Like
0Likes
Like

Posted 27 December 2012 - 12:33 PM

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

#6 C0lumbo   Crossbones+   -  Reputation: 2195

Like
0Likes
Like

Posted 27 December 2012 - 12:47 PM

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.



#7 Paradigm Shifter   Crossbones+   -  Reputation: 5250

Like
0Likes
Like

Posted 27 December 2012 - 12:53 PM

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, 27 December 2012 - 12:54 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#8 P0jahn   Members   -  Reputation: 260

Like
0Likes
Like

Posted 27 December 2012 - 01:12 PM

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



#9 Khatharr   Crossbones+   -  Reputation: 2957

Like
0Likes
Like

Posted 27 December 2012 - 02:20 PM

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.

#10 Trienco   Crossbones+   -  Reputation: 2115

Like
2Likes
Like

Posted 27 December 2012 - 11:35 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS