davivid
davivid

Reputation: 5960

Exponential Formula for negative direction?

Ive got a bit stuck figuring it out for the negative direction? it must be really simple, but just cant seem to get it!

x = current x position

dir = direction of motion on x axis

if (tween == 'linear'){

    if (dir == 1) {

        x += (x / 5);
    }

    else if (dir == -1){

        //what here??
    }
}

Upvotes: 0

Views: 550

Answers (5)

davivid
davivid

Reputation: 5960

In the end I added a frame counter (t) and went with:

x = -(change*dir) * (t /= 10) * (t - 2) + x;

from my fav as3 tweener lib: http://code.google.com/p/tweener/source/browse/trunk/as3/caurina/transitions/Equations.as

Upvotes: 0

tom10
tom10

Reputation: 69182

What's missing here is that you need to consider deviations from the starting point, not x=0 (and also consider the sign of the direction as well, which others are stating correctly). That is, if your starting point is x0, your equation should be more like:

x += (x-x0)/5

Here's the figure for motion in the positive and negative directions (note that position is on the vertical axis and time on the horizontal)

alt text

And here's the Python code. (Note that I've added in a dt term, since it's too weird to do dynamic simulation without an explicit time.)

from pylab import *

x0, b, dt = 11.5, 5, .1

xmotion, times = [], []

for direction in (+1, -1):
    x, t = x0+direction*dt/b, 0  # give system an initial kick in the direction it should move
    for i in range(360):
        x += dt*(x-x0)/b
        t += dt
        xmotion.append(x)
        times.append(t)

plot(times, xmotion, '.')
xlabel('time (seconds)')
ylabel('x-position')
show()

Upvotes: 2

mtrw
mtrw

Reputation: 35088

If you do something like x -= (x/5), it's going to be impossible to cross x = 0 - as x gets close to 0, it starts changing less and less. Try using a minimum increment

v = abs(x) / 5;
x += ((v > MINVEL) ? v : MINVEL) * dir;

Upvotes: 1

Sheldon L. Cooper
Sheldon L. Cooper

Reputation: 3260

x += (abs(x) / 5) * dir;

Upvotes: 1

Chris Dennett
Chris Dennett

Reputation: 22721

if (tween == 'linear') {
   x += (x / 5) * dir;
}

Upvotes: 0

Related Questions