CocosCreator 旋转角度问题1

需求: 已知两坐标点, 求角度并旋转.

 如下图所示, 简单介绍下, start1为起始点, start2为目标点, point为要旋转的对象.  下面代码中的temp字段为 start1与 start2两点组成的向量与水平向量的形成的夹角.  旋转方向默认按顺时针为正,逆时针为负.

CocosCreator 旋转角度问题1_第1张图片

 

const {ccclass, property} = cc._decorator;

@ccclass
export default class JiaJiao extends cc.Component {

    @property(cc.Node)
    ball1:cc.Node = null;
    @property(cc.Node)
    ball2:cc.Node = null;
    @property(cc.Node)
    ball3:cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {

        let angle = this.calculateAngle(this.ball1.position, this.ball2.position);
        this.ball3.rotation = angle;
    }

    // update (dt) {}

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

 

你可能感兴趣的:(Creator)