Marko
Marko

Reputation: 39

tween movement speed control

I'm using tween engine for smoothing paths of moving entities. To make interpolation you feed function like this:

Tween.to(myObject, POSITION, 1.0f) .target(50, 70) .ease(Quad.INOUT) .start(myManager);

Last argument of to() function is duration. What i learned, if path is longer, the entities move quicker to the target. Shorter the path is, entities move slower. I have float variable called movementSpeed, in every entity, that should move entities 7 pixels per seconds. What is the way using my variable for tween's movement speed instead of having it specified once at factory constructor?

My implementation:

Stack<Vector2i> stack = new Stack<Vector2i>();
/* ...pushing path points from last to first to the stack. */


Tween t = Tween.to(this, EntityAccessor.POS, 4.0f);
for (int i = stack.size()-1; i >= 0; i--) {
    Vector2i cur = stack.get(i);
    if (i == 0) {    // if point is last then
        t.target(cur.getX(), cur.getY());
    } else {
        t.waypoint(cur.getX(), cur.getY());
    }           
}

t.ease(Quad.INOUT);
t.path(TweenPaths.catmullRom);
t.delay(0.5f);
t.start(game.tweenManager);

Upvotes: 0

Views: 246

Answers (2)

Barodapride
Barodapride

Reputation: 3723

Once the tween is created and its duration set with the factory method Tween.to(...) I don't think there is a way to alter its duration. I would suggest using a velocity and updating it every frame to have the effect you want. Having a set velocity or calculating it every frame defeats the purpose of a tween anyway.

Upvotes: 1

Tanmay Patil
Tanmay Patil

Reputation: 7057

I stead of hard-coding the duration, you could calculate it.

i.e. you can always calculate distance to be traveled by using

distance = sqrt(dx*dx + dy*dy)

Now having both distance and speed, you can set the duration as distance/speed.

Hope this helps.

Upvotes: 0

Related Questions