2D Physics Manager

    Use the physics system to access some of the common functions of the physics module, such as click testing, ray testing, and setting up test messages.

    The Physics Manager is enabled by default:

    Draw physics debugging information

    To enable draw debugging information, use the debugDrawFlags.

    The physics system provides a variety of debugging information, you can combine the information to draw the relevant content.

    1. EPhysics2DDrawFlags.Pair |
    2. EPhysics2DDrawFlags.CenterOfMass |
    3. EPhysics2DDrawFlags.Shape;

    Set the drawing flag to EPhysics2DDrawFlags.None to disable drawing.

    1. PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.None;

    General physics modules (Box2D) uses Metre-Kilogramme-Second (MKS) unit system, it has the best performance operating under such a unit system. But we use the world coordinate system units (short for world units) as the unit of length in 2D games, so we need a ratio to convert the physics units to the world units.

    In general, set this ratio to 32, which can be obtained by PhysicsManager.PTM_RATIO, and this value is read-only. Usually the user does not need to care about this value, the physics system will automatically convert the physics units and world units to each other. User can use the familiar world units for all the calculations.

    Set physics gravity

    Gravity is a very important thing in physics operations, and most physics games use the gravity as a important feature.

    The default gravity is (0, -320) world units per second^2, according to the conversion rules described above, that’s (0, -10) m/s^2 in physics unit.

    Gravity can be set to 0. Example:

    It is possible to change the acceleration of gravity to something else, such as a 20 m/s. Example:

    1. PhysicsSystem2D.instance.gravity = v2(0, -20 * PHYSICS_2D_PTM_RATIO);

    The Physics System updates the physics world according to a fixed timestep, the default timestep is . But some games may not want to follow such a high frequency to update the physics world, after all, this operation is more time consuming, then you can reduce the timestep to achieve this effect.

    1. const system = PhysicsSystem2D.instance;
    2. // Physics timestep, default fixedTimeStep is 1/60
    3. system.fixedTimeStep = 1/30;
    4. // The number of iterations per update of the Physics System processing speed is 10 by default
    5. system.velocityIterations = 8;
    6. // The number of iterations per update of the Physics processing location is 10 by default
    7. system.positionIterations = 8;

    Querying physics object

    Often, knowing which physics objects are in a given scene is beneficial. For example, if a bomb explodes, objects in its range will be damaged; or in a strategy game, you may want to let the user drag to move a unit from a certain range.

    The physics system provides several ways to efficiently and quickly look for objects in a region, each of which uses different ways to query objects that fit the needs of the game.

    Point test

    The point test will test if there’s a collider contains a specific point under the world coordinate system. If the test is successful, it will return the collider. If there’re multiple collider that contains the point, a random one will be returned.

    The rectangle test will test a specified rectangle in the world coordinate system, and if the bounding box of a collision body overlaps with this rectangle, then the collision body will be added to the return list.

    1. const colliderList = PhysicsSystem2D.instance.testAABB(rect);

    Ray test

    The Box2D physics module (not available in the Builtin module) also provides ray detection to detect which collision bodies a given line segment passes through. We can also obtain the normal vector at the point where the given line passes through and other useful information.

    1. const results = PhysicsSystem2D.instance.raycast(p1, p2, type, mask);
    2. for (const i = 0; i < results.length; i++) {
    3. const result = results[i];
    4. const collider = result.collider;
    5. const point = result.point;
    6. const normal = result.normal;
    7. const fraction = result.fraction;
    8. }

    The third parameter of the raycast function specifies the type of detection, and the ray detection supports four types. This is because the ray detection of Box2D is not detected from the nearest object of the ray starting point, so the result of the test can not guarantee that the result is sorted by the distance from the object near the start of the ray. Cocos Creator‘s physics system will determine whether the Box2d test results are sorted based on the type of detection. This type will affect the result return to user.

    • ERaycast2DType.Any

      Detect any collider on the ray path. Once it detects any collider, it will immediately end the detection process and will no longer detect other objects.

    • ERaycast2DType.Closest

      Detect the nearest collider on the ray path, which is the default for the raycast detection, slightly slower than above method.

    • ERaycast2DType.All

      Detect all colliders on the ray path, the order of the detected results is not fixed. In this type of detection, a collider may return multiple results because Box2D run the detection by testing the fixture, and a collider may consist of multiple fixtures. This is a more costly method and will be slower than above methods.

    • All colliders on the ray path are detected, but the return result is filtered and only the relevant information about the nearest point of each collider is returned, the slowest method of all.

    The result of ray detection

    The results of ray detection contain a lot of useful information, you can utilize these info according to the actual need.

    • collider

      Specifies which collider the ray passes through.

    • point

      Specifies the point at which the ray intersects the collider.

    • normal

      Specifies the normal vector of the surface of the collider at the intersection.

    • fraction

      Specifies the score of the intersection point at the ray.

    The following figure helps to better understand the result of ray detection.

    Group and mask for ray detection