Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon
28. April 2015


In this tutorial we are going to explore the tilemap functionality built into the Godot game engine.  A tile map is a 2D game map composed of layers of “tiles”, which are essentially just a fixed size sprite with some additional properties.  It allows you to quickly paint a level and reuse art assets, greatly decreasing storage and memory requirements for levels.  In addition to supporting tile maps, Godot also includes a handy editor, although there certainly are some warts.


There is an HD video version of this tutorial available here or embedded below.


We are going to require two scenes for this example, one to create our tileset and one to actually use it.


Creating a Tileset


Before we continue though, I need a tileset to use.  Instead of creating one, I am going to use this one from  Of course, you can use whatever image you want, just be sure the tiles are the same size and have no spacing between them (if you want to keep the math easy).


Here is the tileset I chose:



This version however is shrunk down, use the link above if you want to use the exact same tiles.  Each tile in this image is 32x32 pixels in size.  This will be important shortly.


Now let’s create a tileset in Godot.  Create a new scene.  This process is really labor intensive unfortunately, but very powerful too.  Create a root Node, then a Sprite for your first tile.  Each tile needs to have a unique name.  I am only going to use a subset of what is available on that spritesheet, but you simply repeat the process to add more.


Create a hierarchy like this:



Now for Sprite Tile1.  Now configure the sprite’s Texture to your spritesheet.  Importantly, set Region to on and set the Region Rect to 0,0,32,32, like so:



This sets the image source of this tile to a 32x32 region at the top left corner of the texture.  Now you should see:



Now let’s setup snapping, so we can arrange our tiles in a nice grid.  Select Edit->Configure Snap



Set Grid step to the same dimensions as your tiles, in my case 32x32:



Now select “Use Snap” as seen in the menu above.


The next part is optional, if you want to use physics or collisions or not.  If you do, we now need to set up StaticBody and hit detection boundaries.  This process is the same as we went through in the previous tutorial so I am not going to go into details on how.  You do want the end result like this however:



Then define the areas of your CollisionsPolygon2D that are collide-able or not, like so:



Turning snap on and off is critical here.  Snap makes it easy to create the initial bounding box, as it will automatically snap to each corner.  However, when you start adding fine detail, be sure to turn it off.


Now repeat this process for each tile you wish to include from your spritesheet.  You can make the process a great deal quicker using the Duplicate option ( or Ctrl +D ).  Simply select your first tile then duplicate.  Then you just need to reposition it on the map, redefine the CollisionPolygon2D bounds and of course, update the region to the next tile, like so:



This will select the next tile from the top left of the image.  Repeat until you have all of your tiles defined.  Yes, it would be nice if this was an automated process!


Because I’m lazy, im only going to define three tiles, like so:



Which look like this with collision polygons defined:



Please note, there was no need to put space between each tile like I have here.  Now that we’ve defined our tiles, first save your scene, I called mine tilemap.scn.  Now we are going to export a tilemap.  Simply choose Scene->Convert To…->TileSet…




I called mine tileset.xml.  Be aware of the option Merge With Existing.  When you make changes to your tileset in the future, you probably want to turn this off if you want your changes to be completely overwritten.



I personally found the Collision polygon exporting to be extremely buggy.  You may want to open up the generated XML file and make sure collision information was properly exported:



Using a Tilemap


Now that we’ve got our tileset created, our collision polygons configured and all things ready to go, it’s time to create a new scene then create a Tilemap.


The Node you want to add is a TileMap, like so:



Configure your tilemap like so:



If you are using Physics, set Use Kinematic on, otherwise don’t.  Set the Cell Size to the size of your tiles and tileset to your newly exported tileset.


You will notice now, with the TileMap node selected, your tiles will appear down the left hand side of the 2D editor window:



You can now use these to “paint” your level, like so:



You can create multiple TileMap objects if you need to have different layers of tiles (like foreground props for example).  Today though, we are going to stick to this simple example.


Finally I created a RigidBody sprite to interact with our tilemap, so our final scene looks like this:



Now when we run it, it should look like:



You can make some edits to your tilemap from within Godot without re-exporting the tileset.  With your Tilemap selected, select your Tileset property, then Edit:



You will notice a new Theme menu appears, allowing you to edit your Tileset, such as adding new items:



You can also see the tiles that make up the tileset in the Inspector:



The Video


Programming , ,

24. April 2015


As you may have guessed from the title, it today’s tutorial we are going to look at working with Sprites using Unreal Engine.  We already looked briefly at creating a sprite in the previous tutorial, but today we are going to get much more in-depth.


Before you can create a sprite, you need to have a texture to work with.  Unreal Engine supports textures in the following formats:

  • .bmp
  • .float
  • .pcx
  • .png
  • .psd
  • .tga
  • .jpg
  • .dds and .hdr ( cubemaps only, not applicable to 2D )


That said, not all textures are created equal.  Some formats such as bmp, jpg and pcx do not support an alpha channel.  This means if you texture requires any transparency at all, you cannot use these formats.  Other formats, such as PSD ( Photoshop’s native format ) are absolutely huge.  Others such as BMP have very poor compression rates and should generally be avoid.  At the end of the day, this generally means that your 2D textures should probably be in png or tga formats.  Unreal also wants your textures to be in Power of Two resolutions.  Meaning that width/height should be 2,4,8,16,32 … 512, 1024, 2048, etc…  pixels in size.  It will work with other sized textures, but MIP maps will not be generated (not a big deal in 2D) and performance could suffer(a big deal).  Keep in mind, your sprite doesn’t need to use all of the texture, as you will see shortly.  So it’s better to have empty wasted space then a non Power of Two size.


* Personally I’ve experienced all kinds of problems using PNG, such as distorted backgrounds, while TGA has always worked flawlessly. 


Adding a Texture to your game


Adding a Texture is simple as selecting a destination folder on the left, then dragging and dropping the appropriate file type (from the list above) from Finder/Exporter to the Content Browser window, shown below:



Alternately, you can click New –> Import



Then navigate to the file you wish to use and select it. 


You texture should now appear in the Content Browser.


Texture Editor


Now that you have a texture loaded, you can bring it up in the Texture Editor by either double clicking or right clicking and selecting Edit.  Here is the texture editor in action.  It is a modeless window that can be left open indepently of the primary Unreal Engine window.




The Texture Editor enables you to make changes to your image, such as altering it’s brightness, saturation, etc…  you can also change compression amounts here.  However, for our 2D game, we have one very critical task…  turning off MIP maps.

What's a MIP Map?

History lesson time! MIP stands for multum in parvo, Latin for "much in little". Doesn't exactly answer the question does it? Ok, lets try again. Essentially a MIP map is an optimiziation trick. As things in the 3D scene get further and further from the camera, they need less and less detail. So while right up close to an object you may see enough detail to justify a 2048x2048 resolution texture. However, as the rendered object gets farther away in the scene, the texture resolution doesn't need to be nearly as high. Therefore game engines often use MIPMaps, multiple resolution versions of the same texture. So, as the required detail gets lower and lower, it can use a smaller texture and thus less resources.
You know when you are playing a game and as you move rapidly, often textures in the background "pop" in or out? This is the mipmapping system screwing up! Instead of seamlessly transitioning between versions, you as the user are watching the transition occur.

Support for MIP maps is pretty much automatic in Unreal Engine.  However in the case of a 2D game, you don’t want mipmaps!  The depth never changes, there should never be different resolution versions of each texture.  Therefore, we want to turn them off, and the Texture Editor is the place to do it.  Simply select Mip Gen Setting and select NoMipmaps.



Before you close the Texture Editor, be sure to hit the Save button.



Creating A Sprite


Now that we have a Texture, we can create a sprite.  This is important, as you can’t otherwise position or display a Texture on it’s own.  So, then, what is a Sprite?  Well the nutshell version is, it’s a graphic that can be positioned and transformed.  The name goes back to the olden days of computer hardware, where there was dedicated hardware for drawing images that could move.  Think back to PacMan…  Sprites would be things like PacMan himself and the Ghosts in the scene.


In practical Unreal Engine terms, a Sprite has a texture ( or a portion of a texture, as we will see shortly ) and positional information.  You can have multiple sprites using the same texture, you can have multiple sprites within a texture, and the sprites source within a texture can also change.  Don’t worry, this will make sense shortly. In the meantime, you can think of it this way… if you draw it in 2D in Unreal Engine… it’s probably a Sprite!


Once you have a Texture in your project, you can easily create a sprite using the entire texture by right clicking the Texture and selecting Create Sprite, like so:



You can also create a new sprite using New->Miscellaneous->Sprite



This will then open up the Sprite Editor.  If you created the Sprite using an existing texture, the texture will already be assigned.  Otherwise you have to do it manually.  Simply click the Texture in the Content Browser.  Then click the arrow icon in the Details panel of the Sprite Editor on the field named Source Texture:



Your texture should now appear like so:



You can pan and zoom the texture in the view window using the right mouse button and the scroll wheel.


Now remember earlier when I said “all or part of the texture”?  Well a Sprite can easily use a portion of a texture, and that’s set using the Edit Source Region mode:



This changes the view panel so you can now select a sub rectangle of the image to use as your sprite source.  For example, if you only wanted to use Megatrons head, you could change it like:



Then when you flip back to View, your texture will be:



When dealing with sprite sheets, this becomes a great deal more useful, as you will see shortly. 


There are a couple other critical functions in the Sprite Editor that we will cover later.  Most importantly, you can define collision polygons and control the physics type used.  We will look at these functions later on when we discuss physics. 


Two very important settings available here are:



Pixels Per Unit and Pivot Mode.


Pixels per unit is exactly what it says… a mapping from pixels to Unreal units, which default as mm.  So right now, each pixel is 2.56mm in size.  Pivot Mode on the other hand determines where a sprite is transformed relative to.  So when you say rotate 90 degrees, you are rotating 90 degrees around the sprites center by default.  Sometimes top left or bottom left can be easier to work with, this is where you would change it.


The final important point here is the Default Material, seen here:



This part is about to look a lot scarier than it is!  Just know up front, if you prefer, you can ignore this part of Unreal Engine completely!




Every mesh in Unreal Engine has a material attached, and when you peel back all of the layers, a Sprite is still ultimately a mesh… granted, a very simple one.  There are two default options available to you included in the engine, although depending on how you created your project, you may have to change your view settings to access them:



Then you will find the two provided materials for sprites:



The name kind of gives away the difference… DefaultLitSpriteMaterial takes into account lighting used in the scene.  DefaultSpriteMaterial ignores lighting completely.  Unless you are using dynamic lighting, generally you will most likely want the DefaultSpriteMaterial.  You can edit the Material by double clicking:



This is the Material Editor and it is used to create node based materials.  Basically it’s a visual shader programming language, behind the scenes it ultimately is generating a GLSL or HLSL shader in the end.  Truth is the process is way beyond the scope of what I can cover here and in most cases you will be fine with the default shader.  If you do want to get in to advanced graphic effects, you will have to dive deeper into the Material Editor.


Creating a Sprite


Now that we have our texture and made a Sprite from it, it’s time to instance a Sprite.  That is, add one to our scene.  This is about as simple as it gets, simply drag a Sprite from the Content Browser to the Scene, like so:




Now that you’ve created a Sprite, you will notice that there area  number of details you can set in the Details panel:



All sprites by default share the same source sprite and material, but you can override it on an instance by instance basis.  For example, if you wanted a single sprite to be lit and all the others to be unlit, you can change the Material Override on that single sprite.  Obviously using Details you can also set the sprites positioning information and some other settings we probably wont need for now.



Next up, we will look at sprite animation using a flipbook.

Programming , ,

22. April 2015


The following is a companion post containing the hotkeys used in the following video.  It is an hour in duration and attempts to teach the basics of 3D modeling in Blender.  It is a companion to this video which is also an hour in duration and introduces the viewer to Blender.  Of course if you prefer text based tutorials, we’ve got you covered there too!


The Video


The HotKeys


Blender Hotkeys



Switch mode ( object, edit ) Tab
Switch Edit Mode (Vertex, Edge, Face) Ctrl + Tab
Switch to Edit Vertex Mode Ctrl + Tab + 1
Switch to Edit Edge Mode Ctrl + Tab + 2
Switch to Edit Face Mode Ctrl + Tab + 3
Rotate R
Scale S
Translate/Move/Grab G
Select Object RMB
Select Multiple Shift + RMB
Select All/Clear Selected A
Select Edge Loop Alt + RMB
Box select B
Circle Select C
Lasso Select Ctrl + RMB
X Ray Display Mode Z
Specials Menu (Common operations) W
Vertex Menu Ctrl + V
Edge Menu Ctrl + E
Face Menu Ctrl + F
Extrude E
Bevel Ctrl + B
Knife Tool K
Connect Vertex J
Fill/Create Face F
Insert Edge Loop Ctrl + R

Art , ,

19. April 2015


Today we are going to take a look at the PlayCanvas HTML5 3D Game engine.  While not strictly a tutorial, this guide should give you a pretty good idea of what the PlayCanvas Engine includes,if it is right for you and give you enough information to get you started.


There is also a video version of this tutorial available here.


First off, what exactly is the PlayCanvas Engine?  Well, let’s let them describe it in their own words:


PlayCanvas is the world’s easiest to use WebGL Game Engine. It’s free, it’s open source and it’s backed by amazing developer tools.

The amazing developer tools part isn’t hyperbole either, PlayCanvas ships with an impressive amount of polished and high quality tools.  Where it does get a bit confusing is where the open source part stops, and the pay to use it part starts.  


Getting Started


There are three ways to develop with PlayCanvas.  Download and run the tools locally, use the online tools and code using a Github or Bitbucket repository or, as I am going to do in this example, work entirely in their online hosted IDE.  It’s not a bad way to work but sadly is missing a few features, like code autocompletion. I would probably work in WebStorm using a Github repository if working on a production project instead of just playing around.


To get started with the online hosted tools, you need to create an account then log in to PlayCanvas.  You actually have to create two accounts, first is your over all account, the second is a developer account.  This is because PlayCanvas supports multiple concurrent developers working on and editing the same project.  It is however a premium feature.


Let’s talk about that for a second… what is premium and what is not?  PlayCanvas want to make money obviously, so let’s look at the tiers.  First off, the game engine is open source and available on Github.  However, not all of the tools like the content importer are.  I haven’t downloaded the engine locally, so I am unsure how much of an impact this has.  What is clear is the different tiers available.  As of today, the pricing looks like the following:



There is a second tier entirely for organizations as well.


In a nutshell, the number one biggest drawback is going to be 0 private projects in the free version.  If you are developing a free and open source game, this is a non issue, but obviously if you are creating a private commercial game, you will need to create a private project.  Basically this means anyone can see your project, source, resources, etc with the free version.  The remaining differences boil down to disk space availability, number of team member accounts you can create and being able to export an iOS ready project.  Prices range from 15 - 60$ a month.


Everything you see today is from the free edition.  Other than iOS functionality, all versions contain the same feature set.


The Editor

 So once you’ve logged in and created a developer account, you should see a screen like this one:



You can either create a new project from scratch, or start with one of the starter kits.  We are going to use a starter kit.  This is a pre-configured mini project to get you going. There are currently two starter projects available, a 3D model viewer and a platformer.  Let’s go with the later kit.  Since this is a free account, we have no options but to make the project Public.




You will now be brought to the project management page.  



Click the editor button and you will be brought to the guts of PlayCanvas, where you will be doing the majority of your work.  Click this button:

Pc5 5


The Editor


There are actually two editors currently available, the classic editor and a new beta editor.  You can toggle between them by selecting the Settings gear icon and choosing Use New Editor:



Here are the two different editors.


New Beta Editor



Classic Editor



For this post I am going to stick with the standard production editor.


The layout has your scene graph/tree on the left hand side, your palette of assets across the bottom, and your properties/components across the right.  In the middle of course is your 3D Scene view.  If you prefer, you can switch to a traditional quad view mode:


Obviously in the 3D view you can zoom, orbit and pan the camera, select and edit objects, etc.  You compose a scene by dragging it in from the Pack Explorer.   In addition to the Packs Explorer you see here, there is also an assets and a scripts one as well.


Press the Play icon and your game should start.  Below is an embed of the project ( after Publish from the Dashboard ):



Click the mouse to focus, used arrow keys to navigate and spacebar to jump.


So that’s creating an initial project… now let’s take a look at the developer experience.


Importing a 3D Model


This is always one of the most challenging aspects of working with a 3D engine… getting your content in it.  PlayCanvas makes it pretty simple, but I did run into some small hiccups.


First author your 3D object in your modelling app of choice.  Personally I am using Blender.  They supports FBX, COLLADA and 3DS formats, although FBX is highly recommended.  There are a few limits as well.  You need to embed textures in with your model ( or manually add them later, as we will see ), you have a 256 bone and 64K vertices limit per mesh.


I am going to create a very simple textured cube in Blender, like so:



And then export as an FBX using the following settings, the important ones highlighted:



Now the we have an FBX, we need to import it to PlayCanvas.  Next in the dashboard, go to the assets section and click New Asset:



Then simply drag the FBX file from finder/explorer to the appropriate spot.



Now if you look at the palette portion of the editor ( hit the refresh button top right corner of panel ) you should now see your object.  Simply drag one into the scene:



And tada:



In my case however, the texture is always missing.  I’ve tried different formatting options, going through FBX exporter and never had any luck.  It is however easily fixed.  Instead import your texture as an asset.  Then in 3D view, click your object so it is selected, click again so Material is selected in the Material Editor, select diffuse and assign your texture:



I have to assume this is a bug, either in Blender’s FBX import, or the PlayCanvas Import, but it’s extremely simply fixed by manually assigning the material so no big deal.


Now you will see your object renders properly in the scene:




Like most modern popular engines, PlayCanvas is component based.  Basically you add functionality to game entities by attaching components.  Let’s take a look at the main character and how it’s composed.  This can be seen by selecting the Player in the Pack Explorer then looking at the Attribute editor:



As you can see out Player is composed of a rigidbody, collision and script component.  Additionally you can see it’s got a child entity named model.  Model in turn has a animation and model component attached.  It is through this parenting and composition you make the objects that composed your game.


Adding a component to an entity is as simple as right clicking it and choosing Add Component:



You can similarly add a New Entity the same way, but instead choosing New Entity.




You can of course create your own components and entities as well, but that goes way beyond what I intend to cover here.  You may have noticed the script component attached to the Player…  that segues nicely to...



As you probably expected, the coding language behind PlayCanvas is JavaScript.  Scripts are attached to entities as a component.  Here for example is code for input in the Player from the platform example:

// Player Input
// Handle input from a keyboard
// Requires:
// - The entity must also have the platform_character_controller script attached

pc.script.create('player_input', function (context) {
    var PlayerInput = function (entity) {
        this.entity = entity;
    PlayerInput.LEFT = "left";
    PlayerInput.RIGHT = "right";
    PlayerInput.JUMP = "jump";
    PlayerInput.prototype = {
        initialize: function () {
            // Create a pc.input.Controller instance to handle input
            context.controller = new pc.input.Controller(document);
            // Register all keyboard input
            context.controller.registerKeys(PlayerInput.LEFT, [pc.input.KEY_A, pc.input.KEY_Q, pc.input.KEY_LEFT])
            context.controller.registerKeys(PlayerInput.RIGHT, [pc.input.KEY_D, pc.input.KEY_RIGHT])
            context.controller.registerKeys(PlayerInput.JUMP, [pc.input.KEY_W, pc.input.KEY_SPACE, pc.input.KEY_UP])
            // Retrieve and store the script instance for the character controller
            this.playerScript = this.entity.script.platform_character_controller;
        // Check for left, right or jump and send move commands to the controller script 
        update: function (dt) {
            if ( context.controller.isPressed(PlayerInput.LEFT) ) {
            } else if ( context.controller.isPressed(PlayerInput.RIGHT) ) {
            if (context.controller.wasPressed(PlayerInput.JUMP)) {

   return PlayerInput;


As you can see, the code is also very modular in nature as well.  It’s clean and fairly well documented, we will get to that shortly.


As I mentioned earlier, there are a couple of approaches to code editing.  You can host your code in a Bitbucket or Github respiratory, edit using your editor of choice then sync it back.  Otherwise you can use the built-in code editor.  Let’s take a look at it:



It has some of the features you would expect.  There is syntax highlighting, code folding and warning hints.  What however there is not is code completion… that’s a glaring missing feature IMHO.  It’s been requested and hopefully will be added soon.


The API is clean enough, the editor works, although for a large scale project you will most certainly want to use your own editor, I would certainly.  Although if they added intellisense/code competition, I would spend a lot more time in the web based editor instead of my own IDE.



This is an area that can make or break a project.  I will say that PlayCanvas has very solid documentation.  As you saw above, there are two starter projects to get you going, a Platformer and 3D model viewer.


The documentation is entirely online including a User Manual, Reference and Tutorials:



The documentation is clean, professional and for the most part as shown what I needed when I needed it.  The User Manual covers common concepts like importing models, etc…  but could certainly use a bit more depth.  There is a pretty solid collection of tutorials available:



All told, documentation is a definite plus of this project.  Some quick links if you want to check it out:

User Guide

API Reference



The Video Version





PlayCanvas is an impressive 3D engine for those looking to work in HTML5.  The tools are comprehensive, the documentation is quite good, the pricing structure is reasonable and the code seems clean.  Let me know if this is an engine you want to see additional coverage of here on


17. April 2015


If you are visiting from a mobile device, you may notice some massive changes.  Now when you visit the site, instead of the ugly GFS you know and love, you instead are getting an even uglier GFS!  Here is the site rendered on my HTC One for example:




I personally hate Hate HATE mobile sites, especially for sites like this one.  Code samples simply don’t look good on phones, and the full site looked just fine on my iPad.


However, Google disagrees and there is an algorithm change coming next week and if you like Google traffic, you can’t simply ignore it.  Your ranking is going to be tied to your mobile friendly-ness, and frankly, I wasn’t mobile friendly, not even close.  Today however I did mange to get the score up:



So the good news is, my Google index won’t get worse… in fact, it should improve.


The bad news is:

a) the mobile site is a quick smash-up.  Only about 5% of traffic is mobile as it is and if you are like me you just request the desktop site anyways.  I do need to spend some more time making this more functional and less ugly.

b) GFS is written with a fixed 900px working area assumed.  This was a requirement for formatting screen shots and code samples.  On mobile, neither of these are going to look ideal

c) bugs… there are certain to be bugs.


So, if you run into problems with the change, please let me know.  If you like me hate mobile sites, I’m sorry…  Google tied my hands on this one.


In time however I really need to sit down for a week or two and redo GFS from top to bottom.  Make things a bit less ugly and more importantly, better organize all of the content that is buried in this site.  When I do this I can hopefully merge the mobile and desktop experience into a more cohesive single theme.


Month List