Page 1 of 1

Best way to do RPS type movement with floats?

Posted: Sun Mar 02, 2008 8:18 pm
by Ion Dune
I'm building a 2d game with RTS-type movement (right click to set a location and unit moves to that location), and I'm using floating points which are converted to ints for drawing. The best way, I thought, to do it was to to have units have a location and a target location, and have them move towards the target location every iteration. In order to stop jittering around the target location, I implemented this:

Code: Select all

if (units[t].loc.X<(units[t].tloc.X-(elapsed*units[t].spd*uspdmod))) {units[t].loc.X +=(elapsed*units[t].spd*uspdmod);}
else if(units[t].loc.X<units[t].tloc.X) {units[t].loc.X +=(units[t].tloc.X-units[t].loc.X);}
if (units[t].loc.X>(units[t].tloc.X+(elapsed*units[t].spd*uspdmod))) {units[t].loc.X -=(elapsed*units[t].spd*uspdmod);}
else if(units[t].loc.X>units[t].tloc.X) {units[t].loc.X -=(units[t].tloc.X-units[t].loc.X);}

if (units[t].loc.Y<(units[t].tloc.Y-(elapsed*units[t].spd*uspdmod))) {units[t].loc.Y +=(elapsed*units[t].spd*uspdmod);}
else if(units[t].loc.Y<units[t].tloc.Y) {units[t].loc.Y +=(units[t].tloc.Y-units[t].loc.Y);}
if (units[t].loc.Y>(units[t].tloc.Y+(elapsed*units[t].spd*uspdmod))) {units[t].loc.Y -=(elapsed*units[t].spd*uspdmod);}
else if(units[t].loc.Y>units[t].tloc.Y) {units[t].loc.Y -=(units[t].tloc.Y-units[t].loc.Y);}
With .loc being unit location, .tloc being target, .spd being unit speed, elapsed being the elapsed time for frame-independent movement, and uspdmod being a constant float.

I thought this would prevent jittering, but I still get it at some positions, but not others.

I guess what I'm asking is, what would be the best way to stop units from jumping around their target location?

Thanks for your time.

Posted: Sun Mar 02, 2008 9:10 pm
by JP
Just check for if the unit has got 'close enough' to the target location, if so then stop updating their position:

Code: Select all

if (unit position not 'close enough' to target position) {
  update unit position
}
where 'close enough' is a pre-defined distance which you'll probably have to find by trial and error, could be just a few units.

Re: Best way to do RPS type movement with floats?

Posted: Mon Mar 03, 2008 4:35 am
by Ion Dune
Aha! I got what I was originally trying to do to work.

Where it said:

Code: Select all

else if(units[t].loc.X>units[t].tloc.X) {units[t].loc.X -=(units[t].tloc.X-units[t].loc.X);}
It needed to say:

Code: Select all

else if(units[t].loc.X>units[t].tloc.X) {units[t].loc.X -=(units[t].loc.X-units[t].tloc.X);}
And vise-versa for Y.

Now it works just fine.

Re: Best way to do RPS type movement with floats?

Posted: Mon Mar 03, 2008 5:24 am
by TheBeef
Ion Dune wrote:

Code: Select all

else if(units[t].loc.X>units[t].tloc.X) {units[t].loc.X -=(units[t].tloc.X-units[t].loc.X);}

Code: Select all

else if(units[t].loc.X>units[t].tloc.X) {units[t].loc.X -=(units[t].loc.X-units[t].tloc.X);}
Maybe I'm just crazy, but that looks exactly the same?

Posted: Mon Mar 03, 2008 5:29 am
by Ion Dune
Yes, it's almost, tloc-loc is changed to loc-tloc at the end.