Laya 實操十七:物理引擎

物理引擎

export default class test extends Laya.Script {
    constructor() { 
        super(); 
        Laya.Scene3D.load("res/LayaScene_SampleScene/Conventional/SampleScene.ls",Laya.Handler.create(this,this.on_scene_loaded))
    }
    
    on_scene_loaded(scene3d){
        Laya.stage.addChild(scene3d);

        var ball = scene3d.getChildByName("Sphere");
        var ball_phy = ball.getComponent(Laya.Rigidbody3D);
        ball_phy.restitution = 1;

        //ball_phy.linearDamping = 10;
        //ball_phy.linearVelocity = new Laya.Vector3(0,0,1);
        //ball_phy.applyForce(ball.transform.position,new Laya.Vector3(0,0,-10));

        var plane = scene3d.getChildByName("Plane");
        var plane_phy = plane.getComponent(Laya.PhysicsCollider);
        plane_phy.restitution = 1;
    }
}

 碰撞

import objCol from "./objCol";
export default class test extends Laya.Script {
    constructor() { 
        super(); 
        Laya.Scene3D.load("res/LayaScene_SampleScene/Conventional/SampleScene.ls",Laya.Handler.create(this,this.on_scene_loaded))
    }
    
    on_scene_loaded(scene3d){
        Laya.stage.addChild(scene3d);

        var ball = scene3d.getChildByName("Sphere");
        var ball_phy = ball.getComponent(Laya.Rigidbody3D);
        ball_phy.restitution = 1;

        //ball_phy.linearDamping = 10;
        //ball_phy.linearVelocity = new Laya.Vector3(0,0,1);
        //ball_phy.applyForce(ball.transform.position,new Laya.Vector3(0,0,-10));

        var plane = scene3d.getChildByName("Plane");
        var plane_phy = plane.getComponent(Laya.PhysicsComponent);
        plane_phy.restitution = 1;

        ball.addComponent(objCol);

        //ball_phy.detectCollisions = false;//關閉碰撞

        //是否碰撞
        ball.layer = 1;
        plane.layer = 2;
        ball_phy.collisionGroup = (1<<ball.layer);
        ball_phy.canCollideWith = (1<<ball.layer)|(1<<plane.layer)|(1<<0);
        plane_phy.collisionGroup = (1<<plane.layer);
        plane_phy.canCollideWith = (1<<ball.layer);
    }
}
export default class objCol extends Laya.Script3D {
    constructor() { super(); }

    onStart(){
        console.log(0);
    }
    
    OnCollisionEnter(collision){
        console.log("開始碰撞",collision.other.owner.name);
    }

    onCollisionStay(collision){
        console.log("持續碰撞",collision.other.owner.name);
    }

    onCollisionExit(collision){
        console.log("結束碰撞",collision.other.owner.name);
    }

    onTriggerEnter(other){
        console.log("開始觸發",other.owner.name);
    }

    onTriggerStay(other){
        console.log("持續觸發",other.owner.name);
    }

    onTriggerExit(other){
        console.log("結束觸發",other.owner.name);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章