Intro to collisions in Stencyl

Posted on Friday, July 20, 2012 by Cecily Carver

Hello Dames,

Since a lot of you seemed to be running into problems with collisions in Stencyl, I thought I’d write up this quick guide.

Collisions (a reaction that happens when two objects come into contact with each other – like a ball bouncing off a surface or a character being blocked by a wall or hit with a projectile) can take a bunch of different forms.

  • If you have a top-down view, you want your character to walk over floor tiles but be blocked by wall tiles. So, your character needs to collide with the wall tiles and not the floor tiles.
  • If the enemy shoots fireballs, you might want the fireballs to pass through other enemies without affecting them but collide with your player. So, the fireball needs to collide with enemy actors but not your player actor.
  • You might want your character to react to touching certain items while still being able to pass “through” them and not be blocked by them or bounce off them (like Mario grabbing a coin or pac-man eating dots). So you want collision detection (awareness of when two things are in contact) but not necessarily a collision effect where movement is blocked.

There are four areas that affect which objects can “collide” with which other objects.

1. Collision groups

In the left-hand menu in Stencyl, under Game Settings, there’s an area called Collision Groups. This is where you define categories for your objects and decide which of these groups of things should collide with each other.

There are a bunch of pre-set categories (Player, NPCs, Collectibles, Tiles, etc). You can decide here which things collide with which other things. Do you want to have the game enemies be able to move through each other while still colliding with the player? Do you want collectibles on the ground that the player can pick up but enemies can’t? Do you want ghosts that float through walls even when everyone else is blocked by them? This is where you set that up, by defining your categories and defining how they interact with each other. Later, you can group your specific actors into these categories.

2. Actor properties

When you open up an actor, the far right tab called “Properties” is where you define which collision group your actor is a part of. At the bottom of the screen there’s a label called “Choose Group” and a drop-down menu. These are collision groups. Pick the most appropriate one for your actor.

3. Actor collisions

Where you might trip up is that in this screen, you’re setting collisions not for your actor as a whole, but for each separate animation for that actor.

So, if you have a left walking animation, a right-walking animation, a jumping animation, etc. Stencyl will let you have different collision properties for each of those animated states. While this is useful in some situations (like if your actor is usually solid but has a temporary “ghost” state where she can walk through walls), it can be really confusing if you don’t realize that’s what’s happening. Click on each animation in the left hand panel to see the collision settings for that animation.

Unless you have a very compelling reason not to, make sure that the collision group for each animation is set to “Actor (Same as Default)”. All this means is that you’re sticking with the collision group that you defined in the actor properties in section 2.

You’ll also see a picture of your actor with a yellow box around it. This yellow box defines where the physical “border” of your actor is. If there seems to be an invisible force field around your actor, it’s because the box is too big. Enter the position and dimensions of the box using those four fields to the right of the drawing (X, Y, Width, Height). Make sure the yellow box hugs your drawing, and do this for each of your animations.

There’s also a checkbox called “Is a sensor.” If this is checked, it means that the object can detect collisions while appearing not to react to them. There are lots of times when this is useful, like if you want items on the ground that the actor can either walk over or pick up. Again, make sure you set it to the value you want for each animation.

4. Tile collisions

Each tile in your tileset can have individual collision bounds. Make sure it matches the shape and function of your tile (i.e. if you have a top-down view, floor tiles will be “No Collisions” so that actors can walk over them, while walls might be square).

*If you’re having collision problems, check the following: *

  1. in the overall game collision group settings (1), do the groups and their relationships match the way I want the collisions to work?
  2. in my actor properties (2), has my actor been assigned to the correct group?
  3. in the actor collision properties (3), is the group set to “Actor (Same as Default)” for each of my animations? If it’s not, do I have a really good reason why this is the case?
  4. for each of my actor animations (4), does the yellow bounding box hug my drawing? Is the sensor checkbox set correctly?
  5. for the tile collisions (4), does the collision shape match the shape of my tile?

I hope this helps at least one of you! Best of luck with starting your games.