CocosCreator collision using components (Chapter 15.)

Introduction:
Use collision system of the game everywhere, such as the collision with the enemy's bullets, heroes and monsters of the collision, the collision with the enemy ??? skills which need to use a system crash.
The following describes the collision system from these points:

  1. CocosCreator collision provides three components: Box Collider, Circle Collider, Polygon Collider;
  2. Group Management collision;
  3. Script code control collision event.

A, Box Collider, Circle Collider, Polygon Collider Introducing the collision

Adding collision components:
Here Insert Picture Description

1. Box (rectangle) collision assembly

Here Insert Picture DescriptionHere Insert Picture Description

Attributes Explanation
Tag Tag, when a plurality of components on a node collision, after the collision, this label may be used to determine which nodes are on a collision component crashed.
Editing Whether Edit this collision component, is valid only in the editor
Offset Component offset relative to the node.
Size Length and width of the component.

2. Circle (circular) collision assembly

Here Insert Picture DescriptionHere Insert Picture Description

Attributes Explanation
Offset Component offset relative to the node.
Radius Radius assembly.

3. Polygon (polygon) collision assembly

Here Insert Picture DescriptionHere Insert Picture Description

Attributes Explanation
Regenerate Points To automatically generate the vertex corresponding contour map according Sprite pixels assembly on the node where the assembly.
Threshold Generating a contour map indicating the minimum distance between the vertex, the less the greater the value of the generated points, can be adjusted according to demand.
Offset Component offset relative to the node.
Points Vertex array assembly.

Second, the impact of group management

1. Group Management

Group management, need to open the Project Settings panel to set the location for the menu bar -> Project -> Project Settings.
Open the Project Settings panel, you can see a column group management group configuration item list, as shown below
Here Insert Picture Description
click Add to add a new packet after packet button, there will be a default Default group.
Note that: the packet is added can not be deleted, but you can change the name of any grouping

2. collision packet pair

It can be grouped in the following list of paired grouping collision management table, as shown below
Here Insert Picture Description
table inside this row and column list the items inside the group list, grouping the list of changes will be mapped in real-time to this table. You can configure a packet which in this table, which can be other collision detection packet, is assumed to be a check on the column line b, it indicates the packet on the line will be a collision detection packet in the column b.
After modifying the group of nodes, you need to call apply Collider runtime modifications to take effect.


Third, script code control collision event

Acquiring collision detection system:
var manager = cc.director.getCollisionManager();
Default is disabled by collision detection system, collision detection system is turned on:
manager.enabled = true;
the default rendering collision detection system debug is disabled, open debug drawing:
manager.enabledDebugDraw = true;
display assembly collision bounding box:
manager.enabledDrawBoundingBox = true;

Collision system callback

Collision system callback function, there are three:

  1. Called when the collisiononCollisionEnter: function (other, self)
  2. Called when a collision remainonCollisionStay: function (other, self)
  3. At the end of call collisiononCollisionExit: function (other, self)
/**
 * @param  {Collider} other 产生碰撞的另一个碰撞组件
 * @param  {Collider} self  产生碰撞的自身的碰撞组件
 */
onCollisionEnter: function (other, self) {
    console.log('on collision enter');
    // 碰撞系统会计算出碰撞组件在世界坐标系下的相关的值,并放到 world 这个属性里面
    var world = self.world;
    // 碰撞组件的 aabb 碰撞框
    var aabb = world.aabb;
    // 节点碰撞前上一帧 aabb 碰撞框的位置
    var preAabb = world.preAabb;
    // 碰撞框的世界矩阵
    var t = world.transform;
    // 以下属性为圆形碰撞组件特有属性
    var r = world.radius;
    var p = world.position;
    // 以下属性为 矩形 和 多边形 碰撞组件特有属性
    var ps = world.points;
},

// 当碰撞产生后,碰撞结束前的情况下,每次计算碰撞结果后调用
onCollisionStay: function (other, self) {
    console.log('on collision stay');
},

// 当碰撞结束后调用
onCollisionExit: function (other, self) {
    console.log('on collision exit');
}

This world, no one who is likely to live longer than just someone screaming in pain, was in the silent effort.

Published 44 original articles · won praise 48 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_45021180/article/details/104730415