# CocosCreator 旋转角度问题1

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

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:

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


10-25 1083
12-18
11-16 2815
03-23 1420
08-28 3万+
05-06 3682
11-02 7453
04-26 2975
02-25 1万+
11-21 2052
10-24 312