Laya commercial-level tutorial 3d actual combat _012 collision detection

Laya commercial tutorial, laya tutorial
Goal of this section: collision with obstacles

laya commercial 3d game development

There are many solutions for intersection detection. Laya has a built-in physics engine, or other third-party libraries

Laya has a built-in physics engine. When the number of rigid bodies is large, the smoothness on the Mac will be lower

Insert picture description here

Performance and package body are very important indicators
, which determine the fluency and loading speed respectively. The smaller the game package body, the higher the user’s first loading retention rate.

Considering the performance and package body and the actual needs of the case

The frame integrates the simplest 3d collision module

aabb VS aabb symmetry bounding box intersection detection
Insert picture description here

The source code of the collision detection module is in the 3Dphysics directory

Insert picture description here

There are unity supporting tools to facilitate the sequence and deserialization of the collider
Insert picture description here

Add a collider to the character

Add the aabb shape component to catBase to adjust the boundary
Insert picture description here
Insert picture description here

Output information to the paste version
//center (0.0, 0.6, 0.0) size (0.6, 1.2, 0.3)

Add a collider to the character in onAwake
onAwake() { super.onAwake();

this.animator = this.gameObject.getChildByName('CatBase').getComponent(Laya.Animator);
this.animator.play('Cat_RunLong');

//碰撞器章节讲解内容
let aabbshape: AABBShape = new AABBShape();
aabbshape.mask = CollisionMask.Character;
//位运算,表示碰撞器和 障碍物和鱼产生碰撞
aabbshape.collisionMask = 1 << CollisionMask.Obstacle | 1 << CollisionMask.Fish;
//center (0.0, 0.6, 0.0)  size (0.6, 1.2, 0.3)
aabbshape.size = new Laya.Vector3(0.6, 1.2, 0.3)
aabbshape.center = new Laya.Vector3(0.0, 0.6, 0.0);

this.gameObject.addComponentIntance(aabbshape);
//考虑到性能原因,开启主动检测的对象才会进行遍历检测
//{a,b,c,......} a.ActiveDetec()
// detect(a ,b) 
// detect(a ,c) 
//........

//{a,b,c,......} a.ActiveDetec(); b.ActiveDetec()
// detect(a ,b) 
// detect(a ,c) 
//detect(b ,a) 
//detect(b ,c) 

aabbshape.ActiveDetec();
//碰撞进入
aabbshape.RegisetCollsionEnter(this, this.OnCollisionEnter)
//碰撞离开

// aabbshape.RegisetCollsionExit(this, this.OnCollisionExit)

}

OnCollisionEnter(source: AABBShape, target: AABBShape) {

console.log('OnCollisionEnter', target.mask);

if (target.mask == CollisionMask.Fish) {
  target.gameObject.active = false;
  GameSample.soundMgr.FishCollection.Play();
}
else if (target.mask == CollisionMask.Obstacle)//障碍物
{
  this.Fail();
  let obstalcle = Game.instance.obstacleSpawn.itemMap.getNumberKey(target.gameObject.id)
  obstalcle.ani.play('death');
  obstalcle.collider.enabled = false;
}

}

Fail() {

Game.instance.speed = 0
this.animator.play('Cat_Death')

}

Reborn(){}

Fish add collider and logic
FishSpwan.ts

Add
//The collider tutorial explains
protected CreateSpwanItem(spwanItemData: SpwanItemData): SpawnItem { let item = super.CreateSpwanItem(spwanItemData); let newgo = item.gob; let boxCollider = new AABBShape() boxCollider.mask = CollisionMask.Fish; boxCollider .collisionMask = 0; boxCollider.size = new Laya.Vector3(1, 1, 0.34); boxCollider.center = new Laya.Vector3(0, 0, 0); newgo.addComponentIntance(boxCollider);







    return item;
}

Insert picture description here

Add a collider for obstacles
Insert picture description here

ObstacleSpawn.ts

//重载CreateSpwanItem
protected CreateSpwanItem(spwanItemData: SpwanItemData): SpawnItem {

    let spwanitem = super.CreateSpwanItem(spwanItemData)
    let newgo = spwanitem.gob;

    //碰撞器的添加
    let boxCollider = new AABBShape()
    //center (0.0, 0.7, 0.0)  size (1.5, 1.4, 0.6)
    boxCollider.mask = CollisionMask.Obstacle;
    boxCollider.collisionMask = 0;

    boxCollider.center = new Laya.Vector3(0.0, 0.7, 0.0)
    boxCollider.size = new Laya.Vector3(1.5, 1.4, 0.6)

    let collidergob = newgo as Laya.Sprite3D;
    collidergob.addComponentIntance(boxCollider);

    let obstacle = new Obstacle();
    obstacle.ani = newgo.getChildAt(0).getComponent(Laya.Animator) as Laya.Animator;
    obstacle.collider = boxCollider;

    this.itemMap.add(newgo.id, obstacle)

    return spwanitem;
}

//Obstacle model
export class Obstacle { ani: Laya.Animator; //The collision system chapter will use collider: AABBShape; }



Add
protected onSpawn(newGo: Laya.Sprite3D, spwanItemData: SpwanItemData, z)

//Reactivate the collider
obstacle.collider.enabled=true;

Game.ts
Onawake

… //Collision detection module
this.scene.addComponent(CollsionManagerThreeD);
// this.addfishCollider();

Verify whether the collider is successfully added.
Open the global settings and turn on the display collider

Insert picture description here

F8 f5
Insert picture description here

The character sleeps after colliding with an obstacle

Insert picture description here

What do you need to know about the custom collision module?
Because the bit operation is used to indicate the type of collision detection between the colliders
, the CollisionMask will often need to be added simultaneously when new objects are added

//Because of performance reasons, only objects with automatic detection turned on will be detected.
// The following is that there are 3 objects in the table, 1 object is turned on, and 2 operations are required.
// Objects are turned on, needed Perform 4 operations
//{a,b,c,...} a.ActiveDetec()
// detect(a ,b)
// detect(a ,c)
//...

//{a,b,c,…} a.ActiveDetec(); b.ActiveDetec()
// detect(a ,b)
// detect(a ,c)
//detect(b ,a)
//detect(b ,c)

碰撞管理器CollsionManagerThreeD.ts
onLateUpdate() {
if (GameDesgin.enableCollsion) {
if (this.detectObjs.length >= 1) {
for (let i = 0; i < this.detectObjs.length; i++) {

                let detectAABB = this.detectObjs[i] as AABBShape;
                this.GetFilterZItems(this.detectObjs[i].transform.position.z, detectAABB);

                this.Detect(detectAABB, detectAABB.moveSpeed);
            }
        }
    }

}

// Perform rough detection
// Combined with the case, only objects within 30 meters in front of the protagonist will be detected
// Most physics engines use binary trees, quadtrees, and
octrees for rough detection this.GetFilterZItems(this. detectObjs[i].transform.position.z, detectAABB);

Insert picture description here

Game development,unity3d,laya,game development,unity3d,laya,game development,unity3d,laya,game development,unity3d,laya,game development,unity3d,laya,game development,unity3d,laya,game development,unity3d,laya,game Development,unity3d,laya,game development,unity3d,laya,game development,unity3d,laya,game development,unity3d,laya

Guess you like

Origin blog.csdn.net/koljy111/article/details/108020241