Game Development Community

precise movement

by Joseph Bosch · in Game Design and Creative Issues · 12/30/2013 (2:09 pm) · 6 replies

warning: long post with math
so I have an object at -150, -150 a client right clicks some place lets say -57, 55. The object has a property that lets it move at a speed of exactly 3 per cycle. what is the best way to move the object in code?
well at first I thought the best way would be to do out the math.
IE
currentX = -150
currentY = 100
targetX = -57
targetY = 55
distanceX = 93
distanceY = 45
totalDistance = 138
time = d/s
time = 138/3 = 36
XSpeed = xdistance/time = 93/46 = 2.021739130434783
while mathematically correct this number gets rounded off by the engine at 5 decimal places and positions of objects are only saved to three places means if i do it this way (which I currently am) in this scenario it will be off by about .00091 off of where it should be per every time the game moves the object. The question is though is there a better more precise way to move this object so it ends up where it needs to be?

#1
12/30/2013 (5:04 pm)
So as I understand it, you don't want to move the object at a uniform speed of 3 pixels/sec, but instead you want to interpolate linearly between the starting and target positions?

Really, the approach you have now is kind of okay. When you get to the end point, just set your position exactly to the target position. Nobody will notice a jump of one thousandth of a pixel; it won't even display visibly.
#2
12/31/2013 (8:48 am)
well I want to have it move at 3 pixels and exactly three pixels per cycle. see each object has its own speed value (much like a unit in an RTS) the entire point of this math is to ensure all objects move at the proper speed values and towards the right spot. what I do not want is the object to 1.5x and 1.5y then move left/right/up/down in one direction because its already gotten to its x/y axis, I want the object to travel in a straight line to its target at a specific speed.
also the jump is noticeable on larger levels especially if players go from one edge of the screen to the other.
#3
12/31/2013 (10:22 am)
In which engine? T2D and T3D should not have this warping thing, unless you're setting the destination to the mouse-click location (in Torque this will almost never be an integer value pair) and then at the end of the move you then force it to match some new grid location coordinate. See the T3D RTS Prototype for known "not doing this weird thing you describe" behavior.
#4
12/31/2013 (11:13 am)
T2D, I am trying to get it to the mouse click location represented by worldpos in the right down mouse event function. I though originally part of the problem was getting odd values from that but even using mfloor to get even values I still have this problem.
#5
01/01/2014 (2:44 am)
Ah, I see. So what you want is a vector that points in the direction you want to end up at, with length 3, and add that vector to your position each frame (i.e. set your velocity to that vector). See if this makes sense:

%current = "-150 100";
%target = "-57 55";

%toTarget = VectorSub(%target, %current);
%toTargetUnit = VectorNormalize(%toTarget);
%velocity = VectorScale(%toTargetUnit, 3);

%obj.setVelocity(%velocity);

Or something similar to that.
#6
01/03/2014 (8:40 am)
that looks like it should work thanks