Richard's Arcade Tips #1 - SVG Paths for moving Sprites

Hey folks!

I’ve decided I’m going to start a series of (mostly) weekly little tutorials about some tips and tricks you can use when making MakeCode Arcade games.

This week, we’re going to be looking at movement animations! That’s right, the other animation block:

image

At first glance, this block might not appear super useful; it has a few neat built-in animations but they aren’t really customizable and they’re very specific (how many games really need a parachute animation?).

However, this block actually possesses a hidden super power: you can pass in a string to make your own custom animations!

hello-path

Let’s learn how!

SVG Paths

All of our movement animations are defined using SVG path syntax. I’m not going to go into the details of all the different kinds of things you can do with paths (here is a very helpful article), but let’s take a look at the basics.

First off, path strings are made up of a series of commands. These commands tell the path how to move to different points. Here are a few useful ones:

  • M x y - “move” command teleports you to the given (x, y) point
  • L x y - “line” command draws a line to the given (x, y) point
  • H x - “horizontal line” command draws a line to the given x
  • V x - “vertical line” command draws a line to the given y

To build a path, we can chain together these command. For example:

M 80 60 H 100 V 80 H 80 V 60

Let’s break this path into steps:

  1. M 80 60 - Teleport the Sprite to (80, 60)
  2. H 100 - Make a horizontal line to x=100
  3. V 80 - Make a vertical line to y=80
  4. H 80 - Make a horizontal line to x=80
  5. V 60 - Make a vertical line to y=60

Chain all those together, and here’s what that path looks like:

svg-square-path

That’s neat! But it’s also annoying to have to constantly give everything in absolute coordinates. What if I want my sprite to move in a rectangle from wherever it’s current position is?

Well, every command in SVG path has a relative version. If you use a lowercase letter instead of an upper case letter, then all the points will be taken as relative from the current position. Let’s rewrite our path with relative commands:

h 20 v 20 h -20 v -20

And breaking that down again:

  1. h 20 - Move right 20 pixels from the current position
  2. v 20 - Move down 20 pixels from the current position
  3. h -20 - Move left 20 pixels from the current position
  4. v -20 - Move up 20 pixels from the current position

That’s much easier to understand!

Example #1: NPC movement

It’s a pretty common thing in RPG-style games to have NPCs that walk around a little bit. SVG path animations are an easy way to automate this behavior! Let’s make a simple path to have our NPC walk around between a few tiles by chaining together some horizontal/vertical relative commands:

h 16 v 16 h 16 v -16

Let’s run this on our NPC with the “loop” parameter set to true:

Here’s the result:

npc-path-bad

Oh no! Our NPC is glitching back to the start position every time the path loops. That’s because paths always start back at the same spot when loop is set to true. We can fix this by altering our path so that we return back to the same spot:

h 16 v 16 h 16 v -16 h -16 h -16

That gives us a smooth loop:

npc-path-good

This is nice, but the movement might look a little more natural if the NPC were to pause between moves. We can do that by adding some segments to our path in between the moves. All commands in the path are given the same amount of time in the animation. That means we can force our NPC to pause if we add some commands that don’t move at all (e.g. h 0). Let’s try this path:

h 16 h 0 v 16 h 0 h 16 h 0 v -16 h 0 h -16 h 0 h -16 h 0

And of course, we’ll want to double the amount of time for our animation:

Now we have much nicer movement:

npc-path-pauses

Example #2: Random NPC movement

We can also use path animations to move NPCs randomly. In this example, we move the NPC in a random direction once per second and keep them within the dirt tiles:

Other ways to write SVG paths

Writing SVG paths by hand can be rather tedious, but luckily there are some tools you can use to make it a little easier.

One option is to use an SVG path editor. Here’s the one I used for making that “hello” program:

I also threw together a little extension for making paths with blocks. Check it out here:

31 Likes

Wow, this is incredibly cool! (And I thought that block was useless for everything except animating floating orbs.)

3 Likes

How long did that “hello” take you?!

5 Likes

@ChimbroDaPro eh, like 10 minutes.

9 Likes

Woah, this is awesome! I look forward to the weekly coding tips! :smiley:

5 Likes

This is really cool! I wonder what people will do with this

3 Likes

This will probably make boss fights way easier to code

5 Likes

Woah this is great!

@richard to import the extension we can import that share link just like a GitHub repo? (I’m adding this to Awesome Arcade extensions definitely)

5 Likes

Hey, this is awesome! It opens a lot of new doors for art…
I could also use this with my AI’s! I’ve been trying to make a generative AI that could draw, but having it decide each pixel’s color would be way too big. With a path like this, it could generate a path and draw that way, which is smaller and much more ‘human’ like… Thank you!

5 Likes

@UnsignedArduino yes! I will probably move it into a repo soon though!

2 Likes

The ducks so happy to see you! :smile:

12 Likes

POV: Me when I manage to get into Makecode Forums: A Coder King Mystery.

2 Likes

Bro is sending easter eggs (and spoilers, watch yourself) everywhere :skull:

2 Likes

Awweee! That’s so adorable! >w<

2 Likes

@richard Is it possible to do ease in/out path animations with this? If not, what other method is there?

2 Likes

@Sarge no, not really. the built in path animations always give the same amount of time to each segment of the path regardless of length

i started working on an extension that would change animations to work based on distance travelled instead of segments (i.e. so that sprites could follow path animations at a constant speed) but never polished it up. would be pretty easy to adapt that code to follow an ease in/out function instead of a linear one, though. here’s the code if you want to play with it:

3 Likes

I like to code easing in and out with the distance from player block as that auto increases the speed at which an object moves towards the player (but also with +60-70 so it doesn’t completely slow down when close)

2 Likes

Can someone make an SVG path extension?

1 Like

Richard made one read the post