Physics - Rombusevil/flixel-gdx GitHub Wiki

By default flixel uses the quadtree algorithm for collision detection. The performance is great and it is simple to use. If you need realistic physics Box2D might be more suitable. Box2D is included in the engine.

Quadtree

The quadtree algorithm big advantage is the performance and efficiency since it can directly access game objects. The bounding box is aligned with the axes of the coordinate system, also known as an axis-aligned bounding box (AABB). However there is a drawback, the main disadvantage is only available for rotation of 90 degrees (or multiple thereof). No other rotation is available. In collision detection, when two bounding boxes do not intersect, then the contained objects cannot collide either. Check the image below.

Bounding box

As you can see some of the objects are rotated, but the bounding (green, red and blue dashed lines) are not. You can tweak the bounding box by tweaking the dimension (width and height) and offset of the object.

Physics properties

FlxObject got the physics attributes.

immovable Whether an object will move/alter position after a collision.
velocity The basic speed of the object.
mass The virtual mass of the object.
elasticity The bounciness of the object.
acceleration How fast the speed of the object is changing. Useful for smooth movement and gravity.
drag This isn't drag exactly, more like deceleration that is only applied when acceleration is not affecting the sprite.
maxVelocity If you are using acceleration, you can use maxVelocity with it to cap the speed automatically (very useful!).
angle Set the angle of a sprite to rotate it.
angularVelocity This is how fast you want this sprite to spin.
angularAcceleration How fast the spin speed should change.
angularDrag Like drag but for spinning.
maxAngular Use in conjunction with angularAcceleration for fluid spin speed control.

Collision usage

The collision methods are accessed via FlxG (the Global Helper class). There are several ways to let objects collide. Let’s take a look at the method FlxG.collide(). It got three optional arguments:

  1. The first object or group you want to check.
  2. The second object or group you want to check.
  3. A function with two FlxObject parameters that is called if those two objects overlap.

If you want to check collision on every object that is in game, you can call that method without any arguments given.

FlxG.collide(player, enemy);

If you want to check collision on certain objects you can do that with a single object or group. For maximum performance it is recommended to bundle a lot of object together using FlxGroup.

FlxG.collide(player, enemyGroup);

The last argument is the callback. When a collision occurs the callback will be fired.

FlxG.collide(player, enemyGroup, doDamage);
IFlxObject doDamage  = IFlxObject(FlxObject object1, FlxObject object2)
{
	object1.hurt() ; // _player
}

If you want more control over the collision like creating one-way platforms, you can set the collision directions using bitwise operators. There are four directions, UP, DOWN, LEFT and RIGHT which you store it in FlxObject.allowCollisions. The others are immovable (whether an object will move after collision) and mass (the bounciness of the object).

Overlap

The difference between collide() and overlap() is that overlap() doesn’t call physics routines and only determines if two objects are touching. The method can be called via FlxG.overlap() and it works the same as FlxG.collide().

Debug

When you’re in debug mode (pressing F2 and FlxG.debug must set to true) you’ll see dashes lines around objects, check the image at QuadTree as example. The color indicates the behavior of the object when a collision occurs.

RED Active, movable, solid object.
GREEN Solid, immovable object.
BLUE Non solid objects.
PINK Partially solid, like one-way platforms.

Examples:

⚠️ **GitHub.com Fallback** ⚠️