Solved Velocity Slide Effect

Hello,

I’m trying to achieve a sliding effect with coins. The goal is to have it so that when I collide with a chest, which is already setup, coins will spew out in alternative directions and slowly slow down, but in a sliding type of way. I know how to achieve the random directions, but the sliding effect? Given my current setup, below, what algorithm would work correctly?

I have everything on a Vector2 velocity plane. I set the coin velocity to something, then update it with velocity.x *= DAMP; (DAMP is equal to about 0.2f). This doesn’t create a sliding effect, but rather an abrupt stop. In particular case, I’m applying 25.0 towards the velocity. Lower did not work. How can I achieve a slide effect?

Thanks!

make DAMP = a higher value?

using 10 as a example, before you apply the DAMP, it would be 10, then as soon as you apply DAMP, it would become 2.

maybe you should use a value that will not reduce it so dramatically. Which is the first thing you should of tried, trying different values, to make it be more to your liking.

at the moment, your reducing it to be 80% of the current value, which seems a little to high for a sliding.

Your original method should work, at least mathematically… Check your code again and step through it. You probably have a bug somewhere. You could also use this interpolation method:


private float Lerp(float start, float stop, float alpha)
{
   return (1 - alpha) * start + alpha * stop;
}

Which works on normalized values. I think. Kinda forget, but looking at it makes me think that.
You could also use other easing methods. I suggest you look at LibGDX’s Interpolation class code (it’s on github).

Yeah just raise the dampening, 0.2 is like a brick wall: with an initial velocity of 25, it will be down to 0.001 in just over a tenth of a second.
https://www.desmos.com/calculator/opxesh6vr5

Use something more like 0.9 or so.

Also think about the delta. If you apply the same DAMP every frame, the coins would stop faster if the game is running at high FPS.
You could do something like velocity.x *= DAMP * (1 - delta).
You could also use the Vector2.lerp funcion of Libgdx, if you are using Libgdx.

you could do something like … bit of pseudocode

  • start with a velocity vector, randomly generated at the “collision” event :
vec2 v = new vec2( rnd.nextFloat(), rnd.nextFloat() ) * 0.5 + 0.5;
v = normalize(v);

float min_speed = 10.0; // or whatever, this is probably too high.
float max_speed = 100.0; //

float speed = rnd.nextFloat() * (max_speed - min_speed) + min_speed;

v *= speed;
  • ever loop update apply “decay”
float timestep = 1000.0 / 60.0; // assuming fixed-timestep at 60 hz, millisec.

float decay = 0.008; // higher values = quicker decay.

void update( float step, float decay )
{
  // apply the velocity to something outside, coin position in your example
  coin_position += ( v * step );
  
  // decay v
  v *= 1.0 - clamp( decay * step, 0.0, 0.9999999 );
} 


i bet i got the step multiplication wrong :wink:

o/

If you do the computation in fixed time step, you can do it this way:

If you have variable time steps (with deltaT being the amount of thime since the last step):

@Varkas , thank you! This is just what I needed. :slight_smile: I used the first one.