坐标转换的总结

获得角度的代码
 <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;
            
        }



                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值