CocosCreator 旋转角度问题2 碰撞反弹

import Tools from "../Tools";

const {ccclass, property} = cc._decorator;

@ccclass
export default class BallBehaviour extends cc.Component {
    private screenWidth:number = 0;
    private screenHeight:number = 0;
    private limitX:number = 0;
    private limitY:number = 0;
    private defSpeed:number = 0;
    private speedX:number = 0;
    private speedY:number = 0;
    callBack:any = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
    }

    onKeyDown(event:cc.Event.EventKeyboard)
    {
        switch(event.keyCode)
        {
            case cc.macro.KEY.a:
                this.unschedule(this.callBack);
            break;
            case cc.macro.KEY.b:
                this.schedule(this.callBack, 0.02, cc.macro.REPEAT_FOREVER);
            break;
        }
    }

    start () {
        this.screenWidth = Math.floor(cc.winSize.width);
        this.screenHeight = Math.floor(cc.winSize.height);
        this.limitX = this.screenWidth / 2;
        this.limitY = this.screenHeight / 2;
        this.init(cc.Vec2.ZERO, 500, 45);
    }

    // update (dt) {}

    init(pos, defSpeed, angle)
    {
        this.defSpeed = defSpeed;
        this.node.position = pos;
        this.speedX = Tools.getSpeedX(this.defSpeed, Math.abs(angle));
        this.speedY = Tools.getSpeedY(this.defSpeed, Math.abs(angle));
        this.node.rotation = 90 - angle;
        this.callBack = function()
        {
            this.moveLogic(0.02);
        }
        this.schedule(this.callBack, 0.02, cc.macro.REPEAT_FOREVER);

    }
    
    moveLogic(dt)
    {
        if( (this.speedY > 0 && this.node.y > this.limitY) || (this.speedY < 0 && this.node.y < - this.limitY) )
        {
            this.speedY = -this.speedY;
            this.setRotation();
        }
        else if( ( this.speedX > 0 && this.node.x > this.limitX) || (this.speedX < 0 && this.node.x < - this.limitX) )
        {
            this.speedX = -this.speedX;
            this.setRotation();
        }
        this.node.x += this.speedX * dt;
        this.node.y += this.speedY * dt;
    }

    setRotation()  //方法一
    {
        let dirVec:cc.Vec2 =cc.Vec2.ZERO;
        dirVec.x = this.speedX;
        dirVec.y = this.speedY;
        let upVec:cc.Vec2 = cc.Vec2.UP;
        let degree = Tools.calculateAngle(dirVec, upVec);
        this.node.rotation = 180 + degree;
    }

    setRotation2()  //方法二
    {
        let dirVec:cc.Vec2 = cc.Vec2.ZERO;
        dirVec.x = this.speedX;
        dirVec.y = this.speedY;
        let upVec:cc.Vec2 = cc.Vec2.UP;
        let radian = dirVec.signAngle(upVec);
        let degree = cc.misc.radiansToDegrees(radian);
        this.node.rotation= degree;
    }
}
const {ccclass, property} = cc._decorator;

@ccclass
export default class Tools {

    static calculateAngle(first:cc.Vec2, second:cc.Vec2)
    {
        let len_y = second.y - first.y;
        let len_x = second.x - first.x;
        let tan_yx = Math.abs(len_y / len_x);
        let temp = Math.atan(tan_yx) * 180/Math.PI;
        let angle = 0;
        if(len_y > 0 && len_x < 0){
            angle = temp - 90;
        }
        else if(len_y > 0 && len_x > 0){
            angle = -temp + 90;
        }
        else if(len_y < 0 && len_x < 0){
            angle = -temp - 90;
        }
        else if(len_y < 0 && len_x > 0){
            angle = temp + 90;
        }
        else if(len_y == 0 && len_x != 0){
            angle = len_x < 0 ? -90 : 90;
        }
        else if(len_x == 0 && len_y != 0){
            angle = len_y < 0 ? 180 : 0;
        }
        console.log('Temp', temp);
        console.log('Angle ', angle)
        return angle;
    }

    static getSpeedX(speedOrigin: number, moveAngle: any)
    {
        speedOrigin = Math.abs(speedOrigin);
        let speedX = 0;
        switch(moveAngle)
        {
            case 0:
                speedX = speedOrigin;
            break;
            case 90:
            break;
            case 270:
            break;
            case 180:
                speedX = -speedOrigin;
            break;
            default:
                speedX = this.getCosValue(moveAngle) * speedOrigin;
            break;
        }
        return speedX;
    }

    static getSpeedY(speedOrigin: number, moveAngle: number)
    {
        speedOrigin = Math.abs(speedOrigin);
        let speedY = 0;
        switch(moveAngle)
        {
            case 0:
            break;
            case 90:
                speedY = speedOrigin;
            break;
            case 270:
                speedY = - speedOrigin;
            break;
            case 180:   
            break;
            default:
                speedY = this.getSinValue(moveAngle) * speedOrigin;
            break;
        }
        return speedY;
    }

    static getSinValue(angle: number)
    {
       return Math.sin(angle * Math.PI/180);
    }

    static getCosValue(angle: number)
    {
       return Math.cos(angle * Math.PI/180);
    }
}

 

你可能感兴趣的:(Creator)