1: Getting Started
2: Building Logic
5: Game Mechanics
6: Advanced Topics
7: Testing & Tuning
8: The Last 10%
M1: Mobile - Intro
M2: Mobile - Basics
M3: Mobile - Services
M4: Mobile - Publishing
B: How-To Guides
3.0 Drafts (In Progress)
Working with Physics
What is Box2D?
Stencyl incorporates an industry-standard physics engine (Box2D) into all its games. What's the benefit? You get realistic and accurate collisions for free, and Actors generally behave like real-world objects.
How realistic? Try out the following demo.
Note: The rest of this article walks through the Physics Page interface. If you're skimming or are a first timer, read through the following sections and skip the others for now. They are the most important!
- General Tab
- Lightweight Actors
Friction determines the "roughness" of the Actor's surface. A high friction will cause an Actor to slow down quicker when sliding. A low, or zero, friction will cause the Actor to glide far or never slow down at all.
In real-life high friction surfaces would include dirt and sandpaper. Low-friction surfaces could include ice and glass.
Setting bounciness to a value of 1.0 means an actor will bounce back to the same height it fell from, whereas a value of 0.0 means it won’t bounce at all.
This works out well for actors that have a friction of bounciness of 0 - as you'd expect, that negates everything and causes an effective friction/bounciness value of 0 too.
Terrain/Tiles have friction and bounciness of values of 1, so they have no net effect whichever way.
Damping is a difficult topic to visualize. It's a form of resistance against motion or rotation that varies depending on how quickly the Actor is moving or rotating.
Linear Damping is like wind or air resistance. It grows stronger as your Actor moves more quickly to the point where the Damping is so strong that the Actor is unable to move any faster. This is known as Terminal Velocity.
Angular Damping is the same idea, except applied to rotation. It puts an effective cap on how fast an Actor can rotate and smooths out things if you find your Actor to rotate too quickly when subjected to twisting (angular forces).
Let you opt this Actor out of physics. We talk about this further in the next section.
Auto-Scale Collision Bounds
Choosing “Yes” for this option will automatically re-size your Actor’s collision bounds when you resize the Actor using the "scale to" Tweening block.
Choosing “No” means the collision bounds will stay the same regardless of what happens to the Actor.
Enable Continuous Collisions
Enable this setting to prevent this Actor, if it moves quickly enough, from penetrating through thin surfaces, like shown below. This setting is useful for small and quick actors, such as bullets, arrows and other weapons.
Can be paused?
This setting lets you opt an Actor in or out of the Pausing system.
(iOS) Can be touched?
This option is only for games that use a touch interface (for example, iPhone or iPad games).
Lightweight Actors are actors that opt out of the physics system.
The Lightweight Actors feature works well for games that involve many actors or require more precise motion and collision detection than Box2D allows. For example...
- Shoot 'Em ups
- Puzzle Games
- Pixel Perfect Platformers (like N)
- Point and Click Adventures
Although we're exploring the idea of an engine in which physics can be turned entirely off, the Lightweight physics feature lets you selectively opt an actor out of physics. Even in regular games, this can prove to be useful for...
- User Interface Elements
Bear in mind that when you turn off physics, the actor no longer collides with anything, can no longer be pushed and is generally barred from any physics-related features. Setting position and velocity will still work.
Going with lightweight actors can be a game-saving choice if done right, but you need to understand its (current) limitations.
Sometimes, an actor will get caught in the corner of a box and be unable to move further. In a similar scenario, a jumping actor who jumps right next to a wall versus a little bit off the wall will find his jump to be shorter.
The reasons behind this are quite technical, but the workarounds are less so.
The easiest workaround is to redo the actor's collision shape as...
A box with its corners chopped off slightly or some other means to avoid the sharp 90 degree corners. We did this for a Mario-like platformer and effectivley made an octagon.
My actors don't collide!
Several legitimate reasons for this.
The two actors are set to "Cannot Move", "Cannot Be Pushed" or a combination of the two. This is just the way Box2D works and can catch you off guard.
Did you make sure the actors aren't set as Sensors?
Did you make sure that the actors are from a pair of Groups that are to collide with other?
The game crashes when my actor gets squished between two rotating objects.
This is rare, but it's is a flaw in the current version of Box2D. It will go away in Stencyl 2.5 when we rewrite our engine and upgrade or replace the current version of Box2D with something even better. To work around it, try to detect this scenario ahead of time and move the actor out of the way.
The Physics page has many options and can be a somewhat daunting area to work with at first. We're working on a new interface (with the same options) that will make the Physics page simpler for newcomers, yet more efficient for veterans alike. This will arrive by Stencyl 3.0.
- Through Box2D, Stencyl brings realistic physics to all games.
- Opt an Actor out of physics to improve performance for games with many actors or no need for physics.
- Box2D is powerful, but it can sometimes present challenges when you want your game to NOT operate like the real world.
Create a simple game with moving platforms inside of it, like you've find in any Mario game. It's trickier than it sounds at first!
What are some of the considerations?
- How do you prevent the platform from shifting when something steps on it?
- How will the character stay on the platform after stepping on it?
- If the platform is moving up and down, what happens if it moves too quickly downwards?
- How will you move the platforms? Setting position? Velocities? Forces?
- How do you ensure that the platform starts and ends at the same positions?
Start with a platform that moves up and down. Then, try one that moves left and right.
21825 have read this article