获得角度的代码
地图坐标系 转换为 世界坐标系
<span style="white-space:pre"> </span>public static getAnagle(point: egret.Point): number{
if(point.x == 0 && point.y > 0){
return Math.PI * 0.5;
}else if(point.x == 0 && point.y < 0){
return Math.PI * 1.5;
}else if(point.x > 0 && point.y >= 0){
return Math.atan(Math.abs(point.y/point.x));
}else if(point.x < 0 && point.y >= 0){
return (Math.atan(Math.abs(point.x / point.y)) + Math.PI * 0.5);
}else if(point.x > 0 && point.y < 0){
return (Math.atan(Math.abs(point.y / point.x)) + 1.5 * Math.PI);
} else if(point.x < 0 && point.y < 0){
return (Math.atan(Math.abs(point.x / point.y)) + Math.PI);
}
return 0;
}
世界坐标系 转换为 地图坐标系
<pre name="code" class="javascript">public static worldPosToBattleMapPos(offencePos: egret.Point,defencePos: egret.Point,battleMapPos: egret.Point): egret.Point{
var distance = egret.Point.distance(defencePos,offencePos);
var angle1 = NewBattleUtility.getAnagle(new egret.Point(defencePos.x - offencePos.x,defencePos.y - offencePos.y));
var newDistance = egret.Point.distance(battleMapPos,offencePos);
var angle2 = NewBattleUtility.getAnagle(new egret.Point(battleMapPos.x - offencePos.x,battleMapPos.y - offencePos.y));
var angle = angle2 - angle1;
var dis = newDistance / distance * 100;
var x = dis * Math.cos(angle);
var y = dis * Math.sin(angle);
return new egret.Point(x,y);
}
地图坐标系 转换为 世界坐标系
<pre name="code" class="javascript">public static battleMapPosToWorldPos(offencePos: egret.Point,defencePos: egret.Point,mapPos: egret.Point): egret.Point{
/*
let x = offencePos.x + mapPos.x / 100 * (defencePos.x - offencePos.x);
let y = offencePos.y + mapPos.x / 100 * (defencePos.y - offencePos.y);
return new egret.Point(x,y);*/
var distance = egret.Point.distance(defencePos,offencePos);//战场的长度
var angle1;
if((defencePos.x - offencePos.x) == 0){
angle1 = Math.PI / 2;
}else{
angle1 = Math.atan((defencePos.y - offencePos.y) / (defencePos.x - offencePos.x));
}
var angle2
if(mapPos.x == 0){
angle2 = Math.PI / 2;
}else{
angle2 = Math.atan(mapPos.y * 5 / mapPos.x);
}
if (defencePos.x > offencePos.x) {
if (defencePos.y > offencePos.y) {
//第一象限
//egret.log("battl 1pt: " + angle1);
} else {
//第四象限
//egret.log("battl 4pt: " + angle1);
angle1 = angle1 + Math.PI*2;
}
} else {
if (defencePos.y > offencePos.y) {
//第二象限
//egret.log("battl 2pt: " + angle1);
angle1 = angle1 + Math.PI;
} else {
//第三象限
//egret.log("battl 3pt: " + angle1);
angle1 = angle1 + Math.PI;
}
}
var newDistance = distance / 100 * Math.sqrt(Math.pow(mapPos.x,2) + Math.pow(mapPos.y,2));
var x = newDistance * Math.cos(angle1 + angle2);
var y = newDistance * Math.sin(angle1 + angle2);
//egret.log("battl x,y: " + x.toString() + "," + y.toString());
let pt = egret.Point.polar(newDistance,(angle1 + angle2));
//egret.log("battl pt: " + pt.toString());
pt.x = offencePos.x + pt.x;
pt.y = offencePos.y + pt.y;
//egret.log("battl pt: " + pt.toString());
let pt1 = new egret.Point(offencePos.x + x,offencePos.y + y);
//egret.log("battl pt1: " + pt1.toString());
return pt1;
}