Understanding Box2D’s one-way platforms, aka CLOUDS

One of the new features introduced with Box2D 2.1a is the improved contact listener class which comes in hand when we want to create one-way platforms, or “clouds”.

This can be made thanks to a function called before the contact is processed… something like “hey, two bodies are about to collide, what should I do?”… so you can decide to disable the contact for every collision you want.

The function used to do this task is PreSolve, working for all awake bodies that aren’t sensors.

If you don’t know what is a Box2D sensor, check Box2D Flash game creation tutorial – part 2.

So the concept is: listen for collisions, if a collision involves the cloud wall and the player, then check if the player is higher or lower than the cloud. If it’s lower, don’t process the collision and let the player fly through the cloud.

Let’s see the script, directly taken from Box2D Flash game creation tutorial – part 2: Read more

Box2D Flash game creation tutorial – part 2

After seeing the character creation in Box2D Flash game creation tutorial – part 1, it’s time to add some coins to collect.

This process will involve some interesting Box2D features, like sensors and custom collision management.

I would suggest to read the basics of sensors at Erase Box: the tutorial and custom collision management at Creation of a Flash Stabilize! clone using Box2D – part 4.

Although they are both referred to an older Box2D version, they’ll introduce you to sensor and collisions.

Now the concept is simple: we are placing some circular sensors around the stage (the coins), then we’ll create a custom contact listener class to check whether the player is over a coin or not. If it’s over, we’ll remove the coin.

So this is the main script: Read more

Box2D Flash game creation tutorial – part 1

My first Flash game tutorial ever was Flash game creation tutorial – part 1.

It was the first of a series to create a game like jmtb’s ball games. It was an old AS2 series, and some steps have been ported in AS3 with Create a Flash ball game using AS3.

The “ball” game is so simple yet addictive and customizable that I think it’s the perfect game to start a tutorial series based on a new language.

This time I am not covering a new language but the famous Box2D library, but I am going to add all necessary features to make it an interesting game to play.

In this first chapter, I am going to create the ball and the way you control it, by tapping arrow keys.

I am using the basics of Understanding Box2D applicable forces and Box2D tutorial for the absolute beginners – revamped, which I recommend you to read.

Now this is the code: Read more

Understanding Box2D applicable forces

In Box2d, bodies aren’t only affected by gravity and collisions, but you can also apply forces to them.

Knowing the right force to apply is very important when you want to control a body, as you may want to do in a Flash game

Let’s see the forces you can apply:

Applying a force

public function ApplyForce(force:b2Vec2, point:b2Vec2):void

Apply a force at a world point. If the force is not applied at the center of mass, it will generate a torque and affect the angular velocity. This wakes up the body, so you don’t have to wake it up by yourself.

The force is applied inNewtons (N).

Applying an impulse

public function ApplyImpulse(impulse:b2Vec2, point:b2Vec2):void

Apply an impulse at a point. This immediately modifies the velocity. It also modifies the angular velocity if the point of application is not at the center of mass. This wakes up the body, so you don’t have to wake it up by yourself.

The impulse is applied in Newton-seconds or kg-m/s.

Setting a linear velocity

public function SetLinearVelocity(v:b2Vec2):void

Set the linear velocity of the center of mass. This function will not work if the body is sleeping, so you must wake it up with SetAwake before applying the velocity.

What force should I apply?

You should choose the force according to the result you want to achieve. Let’s suppose you want to apply a vertical force, to make something jump in the air, and let’s suppose you can apply the force at any time, even when you just applied it.

You will have three cases:

Applying the force when the body is not moving: this happens when you apply the force when the body lies on the ground or when it’s in the air, in the moment it finished its “jumping” speed and it’s about to fall down. In this case there aren’t any noticeable difference among applied forces.

Applying the force when the body is moving up: this happens when the body is in the air and still has “jumping” speed. In this case, applying a force or an impulse will sum the “jumping” speed to the applied force to produce a “boost”, while setting the velocity again will just apply the velocity again, no matter the “jumping speed”

Applying the force when the body is falling down: this happens when the body finished its “jumping” speed and it’s falling down. As in the previous case, applying a force or an impulse will sum the falling speed to the applied force, and according to falling speed and applied force, the body will continue falling or jump a little (how little? the difference between the falling force and the applied one). Setting the velocity will just stop the body to fall and make it jumping again, no matter of the falling speed. Read more

Create incredible particle effects with Partigen 2

Do you remember Partigen?

Andrew Fitzgerald from Desuade released the new version, of his amazing particle effects engine: Partigen 2.

Featuring over 120 exclusive preset effects, Partigen 2 is the first and only extension for Flash that let’s you to create complex particle effects in just a click.

The list of features is huge, so I am listing the ones I found most interesting:

You can read the full list of features here

The documentation is awesome, the best I’ve seen so far in a product like this one. You can access the full API documentation, and from this link you can access a 42 minutes long video covering the entire Partigen 2 component, and you can create beautiful effects using the component in less than a minute.

Anyway, we’re not here to talk about the component, but to test the AS3 API. Read more

Box2D tutorial for the absolute beginners – revamped

About a year ago I published a Box2D tutorial for the absolute beginners.

With 2.1 release, a lot of things changed, so it’s time to publish another tutorial for the absolute beginners.

In this tutorial we’ll cover needed libraries to import, world creation, debug draw and the creation of static and dynamic boxes and circles. And obviously the simulation itself

This is the movie we are going to make:

And this is the script used to do it: Read more

Box2DFlash 2.1a released – what changed

Some days ago Boris the Brave released the new version of the popular physics library.

There are some critical changes that won’t make old projects run in the new environment.

The most important ones are:

But I think an example will be more explicative than a thousand words, so I created a simple vehicle you can control with left and right arrow keys. It uses revolute joints and motors.

This is the code you would need in the old Box2D version Read more

Way of an Idea Box2D prototype – Step 3

Welcome to the 3rd part. In part 2 we allowed the player to draw the chalk track in a “paused” Box2D environment and then run the simulation.

Now it’s time to delete our chalk track.

A bit of theory: although the simulation is paused, the chalk bodies are already placed in the Box2D world. So we can easily select them with our old friend GetBodyAtMouse function.

Then we need to know whether the selected body is a chalk or not… we don’t want to delete other level assets or even the ball!!

So we must check if the userData of the selected body (that is the attached sprite) is a chalk. If true, we can remove the sprite from the stage and destroy the body.

This is the code: Read more

Shrink it Box2D prototype – Step 2

In the previous step I showed you how to shrink/expand any kind of polygon.

Anyway in the original game, you can’t expand objects as much as you want, because you need mass to do it, and this adds strategy to the gameplay, because you have to shrink objects to gather the necessary mass to expand other ones.

I suppose the required amount of mass is the difference between the final and the initial mass.

At the same time, when we shrink an object, we’ll gather mass… probably determined by the difference between the initial and the final mass.

When you want to play with masses, the first thing you must setup carefully is the expand/shrink ratio. In the previous step I used a 10% for both shrinking and expanding.

This leads to a glitchy gameplay because if I have a sphere with a radius = 100 and I shrink it by 10% I get a sphere with a radius = 90. But if I expand 90 by 10% I get a sphere with a radius = 99. I don’t get the original sphere. So I cannot use these values, because I need to get the initial object if I expand it and then shrink it or if I shrink it and then expand it.

So in this example I am using 20% shrinking and 25% expanding. This way, our object with radius = 100 shrinked by 20% will have a radius of 80… and a radius = 80 expanded by 25% returns 100 again.

It’s up to you to find a couple of compatible numbers.

About masses, this is the concept: when the player shrinks an object, there isn’t any problem, I just have to compare the old mass with the new one and add the difference to the available mass. To get a body’s mass, use GetMass().

When the player tries to expand an objects, things become a little harder because we can’t know the future mass of the body, unless we want to heavily play with geometry.

So we have to make a little trick: first we remove the original shape, then we create and attach the new, expanded shape to the body, so we can get the mass of the final body. If we have enough mass, then we render the body, otherwise we remove the new shape and restore the old one. Since we do everything before rendering the frame, it will work nicely.

This is the script: Read more

Way of an Idea Box2D prototype – Step 2

It’s time to apply the concepts seen at Pausing a Box2D simulation with Way of an Idea Box2D prototype to create a paused Box2D simulation that will start only when you’ll press SPACE, and meanwhile you can draw paths, just like in the original game.

The only problem with this technique is you can’t use the debug draw to test your projects, because debug draw draws only after Step function has been executed, and since the game starts with the simulation paused, you can’t see the Box2D boxes you are drawing (and you can’t even see other assets too) until you start the simulation.

So the trick is: placing the Box2D boxes, the ones debug draw won’t draw, and attaching the movieclips to them. The movieclips will be rendered because I am adding them to stage, and they will remain in position until the simulation starts.

So the script becomes: Read more

Next Page →

flash games company