Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a set of layers (each of them being a list of items) moving with different speeds as I mentioned here: http://stackoverflow.com/a/18962019/270197. Layers back in the scene move with slower speed than those in the front. The position of items in the front layer is calculated from VelocityTracker.getYVelocity() (we are not doing infinite scrolling but responding to the user's gestures) by using standard Euler integration with friction applied to decrease the velocity by a fixed number of percent each frame -- see code below.

I would need a similar dynamics class for the background layers that act as a satellite, that is, movement should gracefully finish at the same time so instead of an abrupt pause, the velocity of the slower layers should dynamically adjust to the top layer's velocity on each frame (reaching 0 together) to create a realistic but synchronized fling movement on each layer. Any ideas about the algorithm to achieve that?

class SimpleDynamics extends Dynamics {
    private float mFrictionFactor;
    private float mSnapToFactor;

    /**
     * @param frictionFactor The friction factor. Should be between 0 and 1.
     *            A higher number means a slower dissipating speed.
     * @param snapToFactor The snap to factor. Should be between 0 and 1. A
     *            higher number means a stronger snap.
     */
    public SimpleDynamics(final float frictionFactor, final float snapToFactor) {
        mFrictionFactor = frictionFactor;
        mSnapToFactor = snapToFactor;
    }

    @Override
    protected void onUpdate(final int dt) {
        // update the velocity based on how far we are from the snap point
        mVelocity += getDistanceToLimit() * mSnapToFactor;

        // then update the position based on the current velocity
        // velocity is pixels per second, that's the reason for the divide by 1000
        mPosition += mVelocity * dt / 1000;

        // and finally, apply some friction to slow it down
        mVelocity *= mFrictionFactor;
    }
}
share|improve this question
    
So what we would need: while keeping a higher mFrictionFactor and the same gesture-based mVelocity in the bottom layer, to extend its movement (without jumps) so that the fling animation would take the same amount of time as on the top layer. –  ber4444 Sep 27 '13 at 10:21
add comment

1 Answer 1

up vote 0 down vote accepted

To keep the scroll velocity of each layer related to each other layer, I'd calculate the velocity of the top layer and then modify it by the relative depth of the current layer you're evaluating. Something like:

layer_velocity = mVelocity * (1.0 - layer_depth)

where layer_depth is ranged 0.0 (top or surface level) to 1.0 (bottom or deepest level)

Notice that you won't get any movement at 1.0 depth, possible floating point errors aside, so you would either want to set your actual deepest layer to something less than 1.0, or modify the calculation to accommodate a depth of 1.0 while still providing a non-zero velocity result.

share|improve this answer
    
thanks @lll79 but it doesn't work: your calculation only adds a little delay plus it does not take into account the different friction applied to the different layers (and the whole point is that they should scroll at different speeds, due to different friction applied, but reach their end-point simultaneously -- wherever that happens to be within the list -- while maintaining a realistic fling movement). –  ber4444 Sep 27 '13 at 8:58
    
Instead of applying a different friction to each layer to achieve different scroll rates, it assumes you apply a single friction value to a velocity that is shared between all layers, and find the scroll rate of each layer by interpolation, using their relative depth. –  LLL79 Sep 27 '13 at 21:59
    
Thanks, works like a charm! My code for reference: gist.github.com/ber4444/6763465#file-parallaxlist-java –  ber4444 Sep 30 '13 at 13:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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