Log In  


Maybe it's just Friday burn out but I'm struggling to get a "slithering" action right. I know it needs to make use of cos() and sin() but I don't use them regularly enough to quite know where to plug it in properly or math to give it.

What I'm chasing is to get a snake made of N number of segments move across from the screen from left to right. So the whole thing moves on the X with the wave is on the Y. But I think my hang up is how to get each segment wave on a snake-like path; not all together at the same time - basically alternating waves? I dunno..you know how a snake looks :)

In each segment update loop is:

self.x+=-1
self.y+=cos(t())

And that's certainly making the wave happen but all the segments are moving in unison and not in sequence like I was hoping. I want something like this:

But I'm getting something like this:

Like I said, some of this may be weekly burnout because I swear I've tackled this problem before but don't have anything in my snippet library that suggests so.

Any tips, math help, or snippets is appreciated...



I was half right...I have dealt with this before! Found this thread from when I asked years ago. I guess I never learn!! https://www.lexaloffle.com/bbs/?tid=4019

But that taught me what cos/sin is good for...that I did learn way back when. Took that and adjusted a bit and got a little closer...

For each segment I'm starting angle+.01 but I sense that's not offsetting like I though it would

x+=1
y+=cos(angle)*3
angle+=0.033

I found this helpful demo...still picking it apart but at least it's starting where I want to end up; so just some good old fashioned dissecting...

https://github.com/rik-cross/pico8/blob/main/circular_movement.p8


This might help: https://www.lexaloffle.com/bbs/?pid=136752#p

The specific question was about the rotation but I added the additional parameters just because. The phase is the bit you're probably interested in which let's you shift the whole wave left/right.


Cart #rijaretefu-0 | 2025-02-14 | Code ▽ | Embed ▽ | No License


basically what you want the y value to be is sin(t()*frequency+segment index/number of segments*period)*amplitude
you can also replace t() with x/128 if you want it to move at a non constant speed or forward/backward and whatnot, cuz its ultimately just based on any value from 0-1



[Please log in to post a comment]