views:

150

answers:

5

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??
    }
}
A: 
if (tween == 'linear') {
   x += (x / 5) * dir;
}
Chris Dennett
What a nice demonstration of how avoiding blocks in if statements can reduce readability. Avoiding line-breaks is ok, but please put braces so I can upvote.
Šime Vidas
I'm confused as to what the poster wants (based on the other comment in the answer that was deleted) :) If he wants linear motion, that'd just be += 5. I assume he wants something relating to linear acceleration, so keep adding or subtracting something from velocity...
Chris Dennett
im just looking for an exponential increase in x, x += (x / 5); works ok for the positive direction, but not sure what it should be when animating to the left/negative
davivid
+1  A: 
x += (abs(x) / 5) * dir;
Sheldon L. Cooper
nope not the effect im after, x += (x / 5); is fine for the postive direction, just stuck when i want to animate to left/negative. this slows to halt instead of speeding up.
davivid
When `x = -10` this will change `x` to `8` if `dir = +1`, and to `-12` if `dir = -1`. Is that wrong? How should it be then?
Sheldon L. Cooper
for dir = +1, it would then be 6.4, 5.12, 4.09... and for dir = -1, -14.4, -17.28, -20.73.... the ranges increase for one and decrease in the other
davivid
Do you want the magnitude of `x` to increase when `dir = +1` and to decrease when `dir = -1`? In that case, you can try: `x = (x * (5 + dir) / 5);`. If that's not the case, then please provide sample cases, because my crystal ball is running out of batteries. :)
Sheldon L. Cooper
yes x should increase when `dir =+1` and decrease when `dir = -1`. However the range between values should exponentially **increase** for both, so that the animation speeds up in both directions
davivid
Apparently my crystal ball is malfunctioning. Please provide sample cases. Four cases should be enough, with `dir` +1 and -1, and each with positive and negative `x` (e.g. -10 and 10).
Sheldon L. Cooper
+1  A: 

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;
mtrw
thanks, the first option does present a solution - although it does suffer from a warp effect between the slow down and then using MINVEL it seems. the second option doesnt provide equal speeds for both directions, and at the moment cant seem to get a satisfactory result unfortunately.
davivid
I'm sorry, the second option is totally useless - it has the exact same issue about velocity going to `0` as `x` passes through the center. That was really dumb of me.
mtrw
A: 

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

davivid
+2  A: 

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()
tom10
great thank you. I had to modify it to `x += dir*Math.abs(x-startP+(dir*10))/5` by adding `+(dir*10)` since often my start position is the x position.
davivid
In general, which approach would be better? this? or adding a t var and using something like `x = -(change*dir) * (t /= 10) * (t - 2) + x` for JS Interval based animation?
davivid
I don't really understand the motivation for either of your equations. 1) "start position is the x position"... don't you always start somewhere on the x-axis; and I don't like the term -startP+(dir*10) since it's is taking away the symmetry of my approach, which as I wrote it, is independent of the sign of x0, etc? 2) your term (t/=10)*(t-2) doesn't work well for me either since is has units sec^2 which will never be a position. If in a dynamic simulation/animation, you don't use time, you're usually implicitly setting it to 1, but it's less confusing to write it explicitly.
tom10
I took the "dir" and the "abs" out of my answer as that was seeming to add confusion, and it works the same either way. Basically, you just want to system to grow quickly, and this will grow exponentially to the left or right, depending on which direction you give it the initial kick.
tom10
sorry I just meant that with 1) that they cancel each other out, and the system does not grow, and hence the reason to add the extra. I can use `x += dir*Math.abs(x-x0)/5` but the only way do get that to work is to make sure x and x0 are not the same to begin with.
davivid
I see what you intended. The best way to do this is to think of the problem as 1) a dynamic evolution governed by an equation, 2) an initial condition; and treat these somewhat separately. Your extra term sort of combines these two in a way that's confusing. Btw, in physics, the approach I outlined is called a "unstable equilibrium", where the system will stay balanced but only right at the balancing point... once perturbed it's speed increases, like a pencil balanced on it's point. (I added comment in my code to show where I give it the starting condition "kick".)
tom10