vote up 1 vote down
star

Playing Starcraft, I was just wondering how to achieve the same effect as they do when bringing their buttons on screen. (0:20 into this video - http://www.youtube.com/watch?v=r4ijwtGCaRg).

I tried doing position.x += 5, but how do I get it to move less towards the end like how the buttons slows down?

flag

4 Answers

vote up 0 vote down

All you need to do is make 5 smaller the closer you get to your final position. This can be done several ways. I'm not sure on what may be the most efficient though. It also depends on what the rest of the code is doing.One way is this.

if( position.x < finalPos.x * 0.6f)

   position.x += 5;     // Move normal speed until 60% of way to final position

else if( position.x < finalPos.x)

   position.x += 2;     // Move slower to final position

This only works when moving in the positive direction and you start at 0, though. You could also try making the 5 a number that gets smaller the closer the position gets to the end.

position.x = (finalPos.x - position.x) * 0.6f; 
//Move 60% of the way to final position.

As long as you get the idea you can make it fit what you need. Hope I helped.

link|flag
That could definitely work, but I think there is a smoother approach. – resolveaswontfix Oct 27 at 13:28
vote up 0 vote down

In pseudo code:

// Make the object fly out to the right
speed = 0;   
while(position.x < screen.width)
   position.x += speed;
   speed += 0.1;


// Or, make the object fly in from the right
speed = 5;
while(position.x > target.x)
    position.x -= speed;
    speed -= 0.1;

Something along those lines should give you the smoothness you're after.

link|flag
couldn't that potentially reverse direction? – resolveaswontfix Oct 27 at 13:27
Potentially, yes. You can add an extra check to make sure it doesn't, but I tried to keep the snippet concise. – Martin Oct 27 at 13:56
vote up 0 vote down

This sounds like a perfect use for a sine wave to me!

link|flag
please explain this answer. – resolveaswontfix Oct 28 at 12:42
vote up 0 vote down

What you have is called dampening. You stop giving a constant velocity to the object, let its current velocity take over, and apply friction.

Or just use flash.

link|flag

Your Answer

Get an OpenID
or

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