Log In  

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 :)

Cart #demenifji-0 | 2022-08-17 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

P#115949 2022-08-17 19:02

1

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.

P#115950 2022-08-17 19:39 ( Edited 2022-08-17 19:39)
1

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

Cart #zapiwojoba-0 | 2022-08-17 | Code ▽ | Embed ▽ | No License
1

P#115957 2022-08-17 23:06 ( Edited 2022-08-17 23:09)

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.

P#115958 2022-08-18 00:16
1

you need an easing function!
https://www.lexaloffle.com/bbs/?tid=40577

P#115959 2022-08-18 01:00
1

@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. :)

P#116178 2022-08-23 19:12 ( Edited 2022-08-23 19:13)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2022-12-03 05:55:01 | 0.068s | Q:30