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<
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);
    }
}

 

你可能感兴趣的:(laya)