1
\$\begingroup\$

I' have some code to move a unit between two points in a series of points, when it reaches the next point it pops that point off the front of a stack, and then heads towards the next in the stack.

Issue: When the movement between two points occur, the object will slow down towards the point, I want a constant rate of movement between the two points, I'm thinking of implementing something like a physics engine would, there is some groundwork there already, however it would not ideally work with this code the way I think I should do it, I'd have to rewrite a fair deal where you create a movement vector in the direction of the point?

void EntityUnit::unitgmovement(CWaypoint* &TargetVec, float updatetime, CWaypoint* &Sourcewaypoint)
{
    // BUG why use this twice ? what happening ? probably resolve later tonight. 
    // a moveto function without path smoothing used for infantry and tank movement
    pf_Dest = TargetVec;
    pf_Source = Sourcewaypoint;
    double xDistance = pf_Dest->GetX() - unit_x;
    double yDistance = pf_Dest->GetY() - unit_y;
    double zDistance = pf_Dest->GetZ() - unit_z;
    double distance = sqrt(xDistance * xDistance + yDistance * yDistance + zDistance * zDistance);
    util_timer_movedelay += updatetime;
    if (util_timer_movedelay > speed / 1000)
    {

    //  cout << "Distance:" << distance << endl;
        if (distance > 0.05)
        {
            unit_x += (0.5 * xDistance * speed * updatetime);
            unit_y += (0.5 * yDistance * speed * updatetime);
            unit_z += (0.5 * zDistance * speed * updatetime);
            util_timer_movedelay = 0;

        }
        else
        {
            pf_Source = pf_Dest;
            dest_reached = true;
            pf_waypoints.pop_front();

        }
    }



}

If I have an objects X,Y,Z global location, how do I create a movement vector for them ? that would work with this so I have a constant rate of movement? Or is there another easier work around ?

\$\endgroup\$
  • \$\begingroup\$ @tkausl sorry fixed now. \$\endgroup\$ – RNewell122 Aug 24 '17 at 18:34
3
\$\begingroup\$

The issue is in the following lines:

unit_x += (0.5 * xDistance * speed * updatetime);
unit_y += (0.5 * yDistance * speed * updatetime);
unit_z += (0.5 * zDistance * speed * updatetime);

The first half of each of those is saying to move 1/2 the distance to the goal, which will by nature shrink as you get closer, giving you the slowdown effect. This may be desirable sometimes, but not in your case.

To fix this, simply divide each component by the total distance before adding it, like so:

unit_x += (0.5 * xDistance * updatetime * speed / distance);
unit_y += (0.5 * yDistance * updatetime * speed / distance);
unit_z += (0.5 * zDistance * updatetime * speed / distance);

How does this work? The important bit is speed / distance. That gives you a fraction like, "I go 10ft per second, it's 40ft away, so I'll go 1/4 of the distance this second." If you take that times your component distances (xDistance, yDistance, and zDistance) that will give you the amount to move on each axis.

As a side note, the 0.5 * ... will mean you only move at half of your speed. Alternatively, your speed variable may mean something completely different to you and 0.5 is the true 'speed' of the object. The math remains the same in that case, but it's something to watch out for.

\$\endgroup\$
  • \$\begingroup\$ It worked man! thanks so much, and yes the 0.5 is what im considering a base movement unit, and the * speed is a modifier for the different unit types speed. \$\endgroup\$ – RNewell122 Aug 24 '17 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.