Best way to do RPS type movement with floats?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Best way to do RPS type movement with floats?

Post 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.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post 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.
Image Image Image
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

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

Post 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.
TheBeef
Posts: 25
Joined: Wed Feb 20, 2008 4:38 am

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

Post 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?
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post by Ion Dune »

Yes, it's almost, tloc-loc is changed to loc-tloc at the end.
Post Reply