Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

Fixed distance smooth snake movement

$
0
0
Snake movement probably has been asked and answered many times before. I managed to do some basic snake movement. But I find it hard to fix the distance. Let me explain my problem. The code I am using is like this: const dx = this.tracer.x - this.x; const dy = this.tracer.y - this.y; const actualDistance = Math.hypot(dx, dy); this.vx = this.tracer.speed * ( dx / actualDistance ) || 0; this.vy = this.tracer.speed * ( dy / actualDistance ) || 0; if (actualDistance >= this.distance) { this.x += this.vx; this.y += this.vy; } else { this.x += lerp(0, this.vx, actualDistance / this.distance); this.y += lerp(0, this.vy, actualDistance / this.distance); } The above code is in the "update" function of each snake segment. Each segment will follow the previous segment to its target position. The tracer means the body which is following by current body. The distance is a pre-calculated fixed value. I want to move the snake at normal speed, or at a higher speed, or slow down to normal speed. Like that in slither.io. This is not hard to do using my script. I can move my snake at normal speed or speed up or slow down with all the bodies following the previous one. I can also turn or rotate the snake smoothly. But there is one problem when I try to move fast and rotate at the same time. The snake will squeeze shorter which means all the distance between two adjacent bodies become shorter. So the snake looks like a smaller one when it is doing a fast turn. This will last until it slows down its speed to normal and it's back to its normal shape. All I want is make it same size as normal no matter how I turn or rotate the snake at any speed. Can this be achieved?

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>