• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By fazook
      Hi, guys!
      I have a rather abstract question, because I don't know which side to approach to its solution. So, I would appreciate any information.
      I have a task to create a simple game that generates floor plans and I following by this perfect algorithm (https://www.hindawi.com/journals/ijcgt/2010/624817/). At the moment I use squarified treemaps (http://www.win.tue.nl/~vanwijk/stm.pdf) and here no problems. I create nested array in which elements are rooms with size. Problems starts when I trying to represent generated "rooms" as edges and vertexes (a, b, c, d steps in attached picture) That representation can give me access to this elements as special "entities" in future game versions.
      I don't have skills in graphs (and do I need graphs?) and at the moment totally stucked at this step. How can I represent room walls as trees (or graphs?) at this step? Calculate size of squares (rooms) and convert sides to a vectors? Then in loop find shared vectors (same position by "x" or "y") and determine them as shared walls? The instinct tells me that there exist more elegant and efficient ways.
      Anyway, thanks for any information about this.

    • By Swatchoos

      Hi Guys,
      We would like to present our new panel: Swatchoos, the most advanced swatch panel for Adobe Photoshop CC with lots of features and improvements like:
      - Group Creation and Management
      - Swatch and Group Reordering
      - Change brightness and darkness of any color
      - Sort Swatches by Luminosity or HUE
      - Fill any Shape/Text/Bitmap layer
      - Generate Swatches from bitmaps and .psd layers
      - Customisable the look of your color
      - Import .aco or bitmaps
      - Share your swatch palettes with others
      - Auto color naming
      and much much more.
      You can try it for free so go to www.swatchoos.com and test it out!
    • By drago28
      I'm using construct 2 physics I have a working bike with everything setup and I'm able to do frontflip and backflip by adding angular velocity to the bike
      I want to increase score/ add points and display the "backflip / frontflip" text when the bike does it.
      so please help me I want to know the logic and how it's done.
      p.s. example about construct 2 is not necessary, just explain me how other games do it or anything else you have to share
    • By otgs17
      You have 12 hours to search for supplies, transportation or you can continue your journey.
      But as soon as night falls, you must find shelter, turn it into a fortress to stand against the hordes of monsters.
      Equip soldiers, use bonuses and do not forget to follow the energy level of your fighters. 
      In battle be useful to your transport, it would be nice to find a tank.
      Every day the number of monsters to get more, they mutate into stronger and more dangerous forms.

    • By Masson
      Name: Dodging Dots
      Category: Arcade/Casual
      Come up with your own strategy. Collect myriads of dots and maneuver from the coming blocks. Beware! There gonna be lots of them!
      The minimalistic world will captivate you for a long time. Try out!
      Use upgrades to improve your game experience 🎮 Increase the velocity 🏄 of the dots to avoid the avalanche of blocks. Or you can increase the number of falling boosters to break 💥 through it, the choice is yours! Nice color shades that change throughout the game Ongoing game. Original soundtrack 🎧 Integration with Google Play Games Compete with other players 🏆 18 achievements to warm up your interest 🔥 Simple control, play with one finger Google Play: https://play.google.com/store/apps/details?id=com.groovin.blocks.delicate.survive.gameplay
      Feel free to leave comments. We really interested in improving this product and appreciate your support.
  • Advertisement
  • Advertisement

Fixed distance smooth snake movement

Recommended Posts

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?

Edited by caymanbruce

Share this post

Link to post
Share on other sites
On 07/07/2017 at 6:26 PM, caymanbruce said:

when I try to move fast and rotate at the same time. The snake will squeeze shorter

That's some relativity effect right there.

But seriously, if you are doing everything right, then my guess is that you aren't restricting change of angle which just makes the snake contract because the actualDistance doesn't exceed your desired fixed distance and fall into heavy lerp-ing.

While playing slither.io, then you might have noticed that the rate of change in angle is (or at least looks like it is) proportional to time difference, so when traveling faster the change of angle is even smaller per unit distance traveled.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement