I need some help with the math for making an object move between 2 fixed positions, then slow down smoothly to the final position.
The cart contains my attempt to make this work but I cannot quite reach the end point, and therefore the mode variable does not change.
Can anyone see what is missing here, and what could I do to make the last few steps a bit faster? (It slows to a crawl at the end...!)
Many thanks in advance :)
The problem seems to be that once the remaining distance is small enough, its value/100 is, as far as Pico-8 is concerned, equal to zero. Probably the simplest fix would be just to add a small constant value to the velocity.
Try this!
function _init() mode="move" start=10 finish=110 pl_x=start pl_y=64 pl_sp=1 pl_dx=4 end function _update() if mode=="move" then -- using mode to stop drifting distance=finish-pl_x pl_x+=(pl_dx*(distance/100)+0.2) --above line changed end if pl_x >= finish then pl_dx=0 mode="wait" end end function _draw() cls() circ(pl_x,pl_y,3,8) pset(start,64,10) pset(finish,64,10) print(distance) print("mode: "..mode) end |
Simply change:
if pl_x >= finish then pl_dx=0 mode="wait" end |
for
if abs(distance) < 0.1 then pl_dx=0 pl_x=finish mode="wait" end |
0.1 there is an arbitrary value, it could be 1, 0.5, whatever you want to be the threshold to "snap" into the finish position. Doing that should work.
you need an easing function!
https://www.lexaloffle.com/bbs/?tid=40577
@JadeLombax @MrAwesome @profpatonildo @merwok
Thanks all for your responses! All your suggestions had a nice effect and to follow on I am now trying out some more easing functions. :)
[Please log in to post a comment]