Best way to do RPS type movement with floats?
Posted: Sun Mar 02, 2008 8:18 pm
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:
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.
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);}
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.