[SOLVED] Explain Update() of JumpBy

Hello there,

I am a bit confused with the update() of JumpBy.
I am trying to implement the same but without the action class, i.e i want to implement it in a update function.

Could anyone please make me understand this block of code

void JumpBy::update(float t)
{
    // parabolic jump (since v0.8.2)
    if (_target)
    {
        float frac = fmodf( t * _jumps, 1.0f );
        float y = _height * 4 * frac * (1 - frac);
        y += _delta.y * t;

        float x = _delta.x * t;
#if CC_ENABLE_STACKABLE_ACTIONS
        Vec2 currentPos = _target->getPosition();

        Vec2 diff = currentPos - _previousPos;
        _startPosition = diff + _startPosition;

        Vec2 newPos = _startPosition + Vec2(x,y);
        _target->setPosition(newPos);

        _previousPos = newPos;
#else
        _target->setPosition(_startPosition + Vec2(x,y));
#endif // !CC_ENABLE_STACKABLE_ACTIONS
    }
}

Like say - I have a start position and an end position.
how do i get the x and y value for each frame and how is time of action playing a role in it. !!

I want to override the same logic with some offset of x value in the game (wind effect offset)

Thanks in advance :smile:
could you help @slackmoehrle !! please…

In the code you posted, this is your custom JumpBy class?

nope… this is the source code of JumpBy in ActionInterval.cpp of cocos2d-x

Actually i dont understand it.
how the action time given is playing a role in it… !!
because when i tried with MoveTo

it just keep on going without stopping.
Vec2 new pos = _startPos + _delta * dt;
_startPos = newPos;

So, want to know, how time plays a role in it…??

Well, when these actions are performed the idea is that you want to make it seem as smooth as possible, (i.e. not all choppy). So one thing cocos2d-x does is take a look at the duration of the action, start position, stop position, etc and sort of creates an idea of what makes this happen smooth. The process is called “easing”.

http://cocos2d-x.org/programmersguide/4/

In the code you posted, the function parameter is t instead of dt. If you look closely, it’s used to calculate x and y. Is this what you’re asking?

no no… ha ha… :smile:

but thanks for replying.
I will subclass an aciton class to do my work.

anyway thanks for your response… :smile:
Happy Coding :wink:

Ok, so maybe what you’re confused about is how does the update(float dt) stop being called?

If so, here’s a basic explanation. An action like MoveTo or JumpBy has ActionInterval as a base class. This base class has a function called step(float dt). The step function keeps track of the total dt values it has seen and calls update(float dt) if the total is less than the initialized duration.

When you subclass ActionInterval you should do your override update and do your calculations there. You normally should leave step alone.

Now you are right… :smile:

thanks for the explanation.
I found that today only.

Yes as you suggest, I am about to change something in the update function only.
Thank you for your reply… :smile:

Lemme take a jab at explaining the code…

First, time in actions is normalized. I.e. t moves from 0 - 1. The reason for this is that you can reverse it, in which case t goes from 1 to 0. Or you can make it non linear, which in the case of easing, you can mess around with t outside of the action, and the action will be none the wiser. It’s a really good design actually, one the coolest parts of Cocos IMO.

That said, what is going on.

    float frac = fmodf( t * _jumps, 1.0f );

So _jumps is the number of jumps the action is going to perform. Probably 1 usually, but perhaps you want more. But basically this code is finding the remainder of t for whatever jump are on. So for _jumps == 1. You get t (which is always 0-1) modulus 1, so essentially just t. For values of _jumps > 1, it is the same as…

    auto foo = t * _jumps;
    while (foo > 1)
        foo -= 1;
    // frac now == foo

That remainder is the t in whichever jump you are in. It’s normalizing t for whatever jump number you happen to be making. Each jump also goes from 0 to 1 in terms of t.

    float y = _height * 4 * frac * (1 - frac);

So we know that _height is the height of the parabola. So for frac == 0, we get 0, for 1 we also get 0. So we know that the end points of each jump, y == 0.

What about in between? for frac == 0.5 we get _height * 4 * .5 * .5 = _hieght * 1, so when t is halfway, you get _height. So that’s the parabola part.

The next line _delta is the difference between the target position and the start position. So calculates using the normalized t how far along that path it is. If _delta.y = 0, then the jump start and end are the same height. But you can jump onto platforms or off cliffs, and so the start and endpoint can be different. This calculation interpolates between the two using t.

Finally, _startPosition is the position of the target node, so the overall jump is relative to this, so we add the x and y to this and we have the final jump position for a given t.

Hope this helps :slight_smile:

4 Likes

Thank you very much @mannewalis :smile:
That excellently explained me very much.

Really helped by your response. :smile:
Thank You.