As of late 2021, we are in the process of updating Stencylpedia and fixing up outdated & broken content. Thanks for your patience!

159,943

Views

Beginner

Level

18

Comments

Collisions & Groups

by Jon (Updated on 2015-10-20)


Contents

  • Collisions are Automatic
  • Groups
  • Shapes
  • Sensors
  • Handling Collisions
  • More Collision Handling (Sides, Groups, Points)

Collisions are Automatic

Because Stencyl is powered by Box2D, collisions happen automatically, as if every object existed in “real-life.” For the default cases, no action is required on your part to make collisions happen.

Ragdoll Demo

Groups

However, what if you only want certain collisions to happen? For example, if any enemy shoots a bullet, the bullet should only collide with the player, not other enemies.

stencyl-collision-groups-diagram

To do this, you have to set up Groups.

Groups are arbitrary collections of Actors. Groups tend to be named after certain classes of Actors, such as “Players” and “Enemies.”

stencyl-players-enemies-collision-groups-pic

Collisions happen between groups that are set up to collide with each other. Conversely, collisions are ignored between groups that are not set up to collide with each other.

How To: Setting up Groups

You can set up Groups in the Settings dialog. Click the Settings button to open the dialog.

Settings Button

Next, click the Groups button (shown below), and you'll see a dialog that shows the Collision Groups in your game, the green Create New button that lets you make a new Group, and other settings.

Collision Groups Dialog

How To: Assigning Actors to Groups

You can set an Actor's collision properties, and specify the group it belongs to, in the Properties page of the Actor Editor.

stencyl-assign-actor-to-collision-group

Default Groups

Every Stencyl game starts out with a default set of Groups to help you get started. These groups have a lock next to them. Default groups cannot be edited or removed.

  • Players
  • Tiles
  • Doodads (Scenery)
  • Actors
  • Regions

By default, Doodads and Regions never get involved in collisions. They may be useful if you want a quick way to designate an actor as "non-colliding"

Shapes

As we talked about in the Animations section, Actors can take on different Animation states. For each Animation state, the Actor can have different collision bounds.

For example, if a character is standing, he may be taller. If he’s ducking, he may be shorter.

squid-game-sprite

Stencyl supports 3 kinds of collision shapes: Boxes, Circles and Polygons. (For Polygons, only convex shapes are allowed!)

stencyl-collision-shapes-buttons

You can place an arbitrary number of these shapes, in order to form more complex ones.

stencyl-place-collision-bounds

In this example, we’ve made an “L” shape using 2 boxes.

Sensors

What if you want to detect collisions, without the shapes actually colliding? For example, in Tower Defense games, the towers shoot at targets that come into range.

You’d use Sensors to make this happen. Sensors are special kinds of shapes (a yes/no flag on any shape to be precise), in which an Actor does not physically collide with another Actor, but still detects the collision.

stencyl-collisions-sensor-option-pic

Just check this box in the right-hand pane of an Actor’s Collision Page to make a certain shape a Sensor.

Handling Collisions

Use Events to handle collisions. By "handling", we mean responding to collisions with logic. For example, if a Fireball crashes into Mario, Mario will die.

We support the following collision events.

stencyl-collision-events

Regardless of which Event you pick, you’ll typically see a block similar to this.

stencyl-design-mode-collision-event-block

1st actor always pertains to the first actor in the “sentence.” The same goes for the 2nd actor.

For example....

stencyl-design-mode-collision-event-block-example

Then, when this kind of collision happens, the 1st actor will refer to the Hero and the 2nd actor will refer to the Button.

stencyl-design-mode-collision-event-block-example-actor-references

Just in case, you didn’t know, you can drag the “1st actor” and “2nd actor” blocks out and use them anywhere within this event.

stencyl-design-mode-collision-event-dragging-actor-reference-blocks

More Collision Handling

For any kind of collision, you can check on 3 additional bits of info.

  • What side the collisions happened on
  • What Groups were involved
  • Collision Points (where the collisions happened)

Collision Sides

One point to watch out for is that collision sides are based on the rectangular, bounding box of the shape that collided, so if you used a circle or a polygon, pay attention to the results.

stencyl-design-mode-collision-side-blocks

Exercise: Stomping is one common use case for collision sides. Something is "stomped" if it was hit from the top. Can you think of other use cases for collision sides?

Groups for Colliding Shapes

stencyl-design-mode-collision-shape-group-block

What is this “colliding shape” business about? It stems from the ability to override an Actor’s default group on a per-shape basis, so that a particular shape can take on a different group.

stencyl-design-mode-changing-collision-group-for-shape

When working with the “Collision Group for colliding shape” block, be sure to compare groups directly, rather than comparing the textual name of the Group, which can lead to bugs and crashes.

stencyl-design-mode-collision-example-right-way
(This is the right way)

stencyl-design-mode-collision-example-wrong-way
(This is the wrong way, don’t do this!)

You can find all Group and Type “getter” blocks under Actor > Properties.

stencyl-design-mode-collision-group-getter-blocks

Collision Points

Last but not least, you can grab exactly where collisions happened. This isn’t done very often, but it’s here in case you want to do any additional logic based on the collision location.

stencyl-design-mode-collision-points-blocks

Summary

  • Thanks to physics, collisions are automatic.
  • Groups help you filter out collisions by specifying who collides with who else.
  • Use sensors to detect collisions without the actor being “solid.”
Print Article Edit Article How to Edit an Article
Disclaimer: Use comments to provide feedback and point out issues with the article (typo, wrong info, etc.). If you're seeking help for your game, please ask a question on the forums. Thanks!

18 Comments

Mineat
Important Note: For two actors colliding, make sure that one has no collision code conflicting with the other and the other with the most prevalent collision code mean for both it and the other while keeping all other trigger code for both actors' collisions in separate behaviors for them!
0 4 years, 1 month ago
Herondil
"Sensors

What if you want to detect collisions, without the shapes actually colliding? For example, in Tower Defense games, the towers shoot at targets that come into range."

Not sure that's the best way to do this, in the region chapter there's an exemple about an actor's range which's fine to.

If you want a sensor bigger than the actor, you'll need a blank space around your picture or a dynamic shape creation bound to the current animation. A region won't got those flaws.


0 5 years, 4 months ago
asonero
Also in the blocks, there exist a block that says "<actor> is colliding with <actor>"? i don't see it.
And if 2 actors are overlapping?

0 8 years, 6 months ago
mappel6
@magewarrior2222

There is. I recently needed something much the same when I was getting some code ready for my game. If you go into a Behavior or an Actor's Events section, and choose to Add Event, under Collision, there is an option that says "Any actor collides with... Member of Group". This will allow you to specify "Self" as the "any actor," and choose any one group to cause the collision effect with.

Of course, it has been a while and you may already know this, but hey, it's out there for people to use now.

0 9 years, 3 months ago
mattgam3r68
so, i was doing the stencyltv guide and i did the right thing for collisions and when the enemy hit my player...it did nothing! pls help...
0 9 years, 8 months ago
magewarrior2222
Okay, I have a problem here. I have an actor that can fly, which normally would require less coding, but I have a system made to set gravity on and off which requires a boolean. This boolean is one which detects whether the actor is on/off the ground, and I have it set up to make it only become true when up is pressed. Is there any sort of block which detects if an actor is currently in collision with a group? And if not, will you add one later on in Stencyl?

(I'm using 3.2)

0 9 years, 8 months ago
JAPrice
So as a quick test of my understanding, say I wanted to make a character swing a sword. I would make the animation and perhaps use two collision shapes on the attacking sprite, one for the character and another for the slash effect, and give the slash effect a separate collision shape (say, a circle) and make it a sensor and part of a different group, so that if the slash collides with an enemy, the enemy is damaged, but the player sprite can still be damaged even if attacked during the attack animation...sound about right?
2 10 years, 2 months ago
sazdx
Hello, I wanted to ask about the collisions, since I seem to have some trouble with them. YOu see,for some reason when I move my character to the right, a collision isn't made, and thus the character doesn't lose health. However, it sometimes does detect the collision... but about 40 px off to the right.
Is there a way to solve this problem?
Thanks!

2 10 years, 8 months ago
Jon
@speeddemon1234 - Thanks for your feedback. We agree that the game attributes interface is in need of improvement and will do so in the future.
0 10 years, 9 months ago
Fuhzbahl
How would I make it so that a character will bounce off game borders if they are moving left and right
1 10 years, 10 months ago

Sign In to Comment