PhysicsManager Class

Module: cc

Physics manager uses box2d as the inner physics system, and hide most box2d implement details(creating rigidbody, synchronize rigidbody info to node). You can visit some common box2d function through physics manager(hit testing, raycast, debug info). Physics manager distributes the collision information to each collision callback when collision is produced. Note: You need first enable the collision listener in the rigidbody.

Index

Properties
  • PTM_RATIO Number The ratio transform between physics unit and pixel unit, generally is 32.
  • VELOCITY_ITERATIONS Number The velocity iterations for the velocity constraint solver.
  • POSITION_ITERATIONS Number The position Iterations for the position constraint solver.
  • FIXED_TIME_STEP Number Specify the fixed time step.
  • MAX_ACCUMULATOR Number Specify the max accumulator time.
  • enabledAccumulator Boolean If enabled accumulator, then will call step function with the fixed time step FIXED_TIME_STEP.
  • enabled Boolean Enabled the physics manager?
  • debugDrawFlags Number Debug draw flags.
  • gravity Vec2 The physics world gravity.
Methods
  • testPoint Test which collider contains the given world point
  • testAABB Test which colliders intersect the given world rect
  • rayCast Raycast the world for all colliders in the path of the ray.
  • hasEventListener Checks whether the EventTarget object has any callback registered for a specific type of event.
  • on Register an callback of a specific event type on the EventTarget.
  • off Removes the listeners previously registered with the same type, callback, target and or useCapture,...
  • targetOff Removes all callbacks previously registered with the same target (passed as parameter).
  • once Register an callback of a specific event type on the EventTarget,...
  • dispatchEvent Send an event with the event object.
  • clear Destroy all callbackInfos.

Details

Properties

PTM_RATIO

The ratio transform between physics unit and pixel unit, generally is 32.

meta description
Type Number
Defined in cocos2d/core/physics/CCPhysicsManager.js:70
VELOCITY_ITERATIONS

The velocity iterations for the velocity constraint solver.

meta description
Type Number
Defined in cocos2d/core/physics/CCPhysicsManager.js:80
POSITION_ITERATIONS

The position Iterations for the position constraint solver.

meta description
Type Number
Defined in cocos2d/core/physics/CCPhysicsManager.js:91
FIXED_TIME_STEP

Specify the fixed time step. Need enabledAccumulator to make it work.

meta description
Type Number
Defined in cocos2d/core/physics/CCPhysicsManager.js:102
MAX_ACCUMULATOR

Specify the max accumulator time. Need enabledAccumulator to make it work.

meta description
Type Number
Defined in cocos2d/core/physics/CCPhysicsManager.js:114
enabledAccumulator

If enabled accumulator, then will call step function with the fixed time step FIXED_TIME_STEP. And if the update dt is bigger than the time step, then will call step function several times. If disabled accumulator, then will call step function with a time step calculated with the frame rate.

meta description
Type Boolean
Defined in cocos2d/core/physics/CCPhysicsManager.js:145
enabled

Enabled the physics manager?

meta description
Type Boolean
Defined in cocos2d/core/physics/CCPhysicsManager.js:535
debugDrawFlags

Debug draw flags.

meta description
Type Number
Defined in cocos2d/core/physics/CCPhysicsManager.js:563
Examples
// enable all debug draw info
var Bits = cc.PhysicsManager.DrawBits;
cc.director.getPhysicsManager().debugDrawFlags = Bits.e_aabbBit |
Bits.e_pairBit |
Bits.e_centerOfMassBit |
Bits.e_jointBit |
Bits.e_shapeBit;

// disable debug draw info
cc.director.getPhysicsManager().debugDrawFlags = 0;
gravity

The physics world gravity.

meta description
Type Vec2
Defined in cocos2d/core/physics/CCPhysicsManager.js:610

Methods

testPoint

Test which collider contains the given world point

meta description
Returns PhysicsCollider
Defined in cocos2d/core/physics/CCPhysicsManager.js:222
Parameters
  • point Vec2 the world point
testAABB

Test which colliders intersect the given world rect

meta description
Returns [PhysicsCollider]
Defined in cocos2d/core/physics/CCPhysicsManager.js:253
Parameters
  • rect Rect the world rect
rayCast

Raycast the world for all colliders in the path of the ray. The raycast ignores colliders that contain the starting point.

meta description
Returns [PhysicsRayCastResult]
Defined in cocos2d/core/physics/CCPhysicsManager.js:280
Parameters
  • p1 Vec2 start point of the raycast
  • p2 Vec2 end point of the raycast
  • type RayCastType optional, default is RayCastType.Closest
hasEventListener

Checks whether the EventTarget object has any callback registered for a specific type of event.

meta description
Returns Boolean
Defined in cocos2d/core/event/event-target.js:69
Parameters
  • type String The type of event.
on

Register an callback of a specific event type on the EventTarget. This type of event should be triggered via emit.

meta description
Returns Function
Defined in cocos2d/core/event/event-target.js:77
Parameters
  • type String A string representing the event type to listen for.
  • callback Function The callback that will be invoked when the event is dispatched.
                           The callback is ignored if it is a duplicate (the callbacks are unique).
    
    • arg1 Any arg1
    • arg2 Any arg2
    • arg3 Any arg3
    • arg4 Any arg4
    • arg5 Any arg5
  • target Object The target (this object) to invoke the callback, can be null
Examples
eventTarget.on('fire', function () {
    cc.log("fire in the hole");
}, node);
off

Removes the listeners previously registered with the same type, callback, target and or useCapture, if only type is passed as parameter, all listeners registered with that type will be removed.

meta description
Defined in cocos2d/core/event/event-target.js:119
Parameters
  • type String A string representing the event type being removed.
  • callback Function The callback to remove.
  • target Object The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
Examples
// register fire eventListener
var callback = eventTarget.on('fire', function () {
    cc.log("fire in the hole");
}, target);
// remove fire event listener
eventTarget.off('fire', callback, target);
// remove all fire event listeners
eventTarget.off('fire');
targetOff

Removes all callbacks previously registered with the same target (passed as parameter). This is not for removing all listeners in the current event target, and this is not for removing all listeners the target parameter have registered. It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter.

meta description
Defined in cocos2d/core/event/event-target.js:163
Parameters
  • target Object The target to be searched for all related listeners
once

Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.

meta description
Defined in cocos2d/core/event/event-target.js:182
Parameters
  • type String A string representing the event type to listen for.
  • callback Function The callback that will be invoked when the event is dispatched.
                           The callback is ignored if it is a duplicate (the callbacks are unique).
    
    • arg1 Any arg1
    • arg2 Any arg2
    • arg3 Any arg3
    • arg4 Any arg4
    • arg5 Any arg5
  • target Object The target (this object) to invoke the callback, can be null
Examples
eventTarget.once('fire', function () {
    cc.log("this is the callback and will be invoked only once");
}, node);
dispatchEvent

Send an event with the event object.

meta description
Defined in cocos2d/core/event/event-target.js:208
Parameters
clear

Destroy all callbackInfos.

meta description
Defined in cocos2d/core/event/event-target.js:221

results matching ""

    No results matching ""