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
DrawBits
DrawBits
The draw bits for drawing physics debug information.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.enabledAccumulator
Boolean
If enabled accumulator, then will call step function with a 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.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 pointtestAABB
Test which colliders intersect the given world rectrayCast
Raycast the world for all colliders in the path of the ray. The raycast ignores colliders that contain the starting point.attachDebugDrawToCamera
Attach physics debug draw to cameradetachDebugDrawFromCamera
Detach physics debug draw to cameraon
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, if only type is passed as parameter, all listeners registered with that type will be removed.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.once
Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.dispatchEvent
Dispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.emit
Send an event to this object directly, this method will not propagate the event to any other objects. The event will be created from the supplied message, you can get the "detail" argument from event.detail.
Details
Properties
DrawBits
The draw bits for drawing physics debug information.
meta | description |
---|---|
Type | DrawBits |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/CCPhysicsManager.js:62 |
Examples
cc.director.getPhysicsManager().debugDrawFlags =
// cc.PhysicsManager.DrawBits.e_aabbBit |
// cc.PhysicsManager.DrawBits.e_pairBit |
// cc.PhysicsManager.DrawBits.e_centerOfMassBit |
cc.PhysicsManager.DrawBits.e_jointBit |
cc.PhysicsManager.DrawBits.e_shapeBit;
PTM_RATIO
The ratio transform between physics unit and pixel unit, generally is 32.
meta | description |
---|---|
Type | Number |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/CCPhysicsManager.js:80 |
VELOCITY_ITERATIONS
The velocity iterations for the velocity constraint solver.
meta | description |
---|---|
Type | Number |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/CCPhysicsManager.js:90 |
POSITION_ITERATIONS
The position Iterations for the position constraint solver.
meta | description |
---|---|
Type | Number |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/CCPhysicsManager.js:101 |
enabledAccumulator
If enabled accumulator, then will call step function with a 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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/CCPhysicsManager.js:130 |
enabled
Enabled the physics manager?
meta | description |
---|---|
Type | Boolean |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/CCPhysicsManager.js:505 |
debugDrawFlags
Debug draw flags.
meta | description |
---|---|
Type | Number |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/CCPhysicsManager.js:532 |
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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/CCPhysicsManager.js:580 |
Methods
testPoint
Test which collider contains the given world point
meta | description |
---|---|
Returns | PhysicsCollider |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/CCPhysicsManager.js:202 |
Parameters
- point Vec2 the world point
testAABB
Test which colliders intersect the given world rect
meta | description |
---|---|
Returns | [PhysicsCollider] |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/CCPhysicsManager.js:233 |
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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/CCPhysicsManager.js:260 |
Parameters
- p1 Vec2 start point of the raycast
- p2 Vec2 end point of the raycast
- type RayCastType optional, default is RayCastType.Closest
attachDebugDrawToCamera
Attach physics debug draw to camera
meta | description |
---|---|
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/CCPhysicsManager.js:345 |
Parameters
- camera Camera
detachDebugDrawFromCamera
Detach physics debug draw to camera
meta | description |
---|---|
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/physics/CCPhysicsManager.js:357 |
Parameters
- camera Camera
on
Register an callback of a specific event type on the EventTarget.
meta | description |
---|---|
Returns | Function |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:217 |
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).
- event Event event
- target Object The target (this object) to invoke the callback, can be null
- useCapture Boolean When set to true, the capture argument prevents callback
from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.
Examples
node.on(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, 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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:274 |
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
- useCapture Boolean Specifies whether the callback being removed was registered as a capturing callback or not.
If not specified, useCapture defaults to false. If a callback was registered twice, one with capture and one without, each must be removed separately. Removal of a capturing callback does not affect a non-capturing version of the same listener, and vice versa.
Examples
// register touchEnd eventListener
var touchEnd = node.on(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, node);
// remove touch end event listener
node.off(cc.Node.EventType.TOUCH_END, touchEnd, node);
// remove all touch end event listeners
node.off(cc.Node.EventType.TOUCH_END);
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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:329 |
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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:351 |
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).
- event Event event
- target Object The target (this object) to invoke the callback, can be null
- useCapture Boolean When set to true, the capture argument prevents callback
from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.
Examples
node.once(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, node);
dispatchEvent
Dispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.
meta | description |
---|---|
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:396 |
Parameters
- event Event The Event object that is dispatched into the event flow
emit
Send an event to this object directly, this method will not propagate the event to any other objects. The event will be created from the supplied message, you can get the "detail" argument from event.detail.
meta | description |
---|---|
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:410 |
Parameters
- message String the message to send
- detail Any whatever argument the message needs