Reduce object speed as distance to target reduces - Use distance as a exponential value

This is not cocos2D-x specific. Its just general coding problem

An object has a position and target

I want its speed to reduce like exponentially as it gets closer to target. I want to use the distance between them to make my sped value

One example that works

float speed = distance / 10;

if(speed > maxSpeed){speed = maxSpeed;}

As distance gets towards zero, speed reduces to zero…

I’m just trying see if there is a more awesome way, I imagine its a cool little wee bit of logic.

Any takers

The idea is that the speed slows down smooth as ice

Basically you want tweening functions.

You can read the code for EaseOutQuadratic, EaseOutExponential
(they may have somewhat different names)

interactive (flash) demo: http://www.gizma.com/easing/#l
visual examples: https://www.redgiant.com/user-guide/universe/animation-tween-examples/

There’s a file of tween functions (TweenFunctions.h I think??).

2 Likes