网站首页 mysql技术
游戏开发中--角度-方向计算
发布时间:2017-08-15 08:27查看次数:3160
/** * 计算角度类 */ class jisuan { public constructor() { } /** * 计算目标坐标在左边值的那个方向 * 当前位置X * 当前位置Y * 目标位置X * 目标位置Y */ public static getFangXiang(x: number, y: number, endX: number, endY: number): string { //求弧度 var returnNumber: number = Math.atan2(endY - y, endX - x); //求角度 var N = returnNumber * 180 / Math.PI; var Z: string = "无"; if (endY == x && endY == y) { return Z; } else { if (N <= 20 && N >= -20) { Z = "右"; } else if (N <= 110 && N >= 70) { Z = "下"; } else if (N <= -160 || N >= 160) { Z = "左"; } else if (N <= -70 && N >= -110) { Z = "上"; } else if (N < 70 && N > 20) { Z = "右下"; } else if (N < 160 && N > 110) { Z = "左下"; } else if (N < -110 && N > -160) { Z = "左上"; } else if (N < -20 && N > -70) { Z = "右上"; } return Z; } } /*** * 求角度 * 方向 换算角度 */ public static getJiaoDu(fangxian: string): number { var N = 0; switch (fangxian) { case "上": N = 0 break; case "右上": N += 45 break; case "右": N += 90 break; case "右下": N += 135 break; case "下": N += 180 break; case "左下": N += 225 break; case "左": N += 270 break; case "左上": N += 315 break; default: N = 0; break } return N; } /** * 取下一个移动坐标值 * 当前坐标 X * 当前坐标 Y * 移动方向 * 移动速度 */ public static getNestPosition(x: number, y: number, fangxian: string, speed: number):Array<Object> { switch (fangxian) { case "上": y -= speed; break case "下": y += speed; break case "左": x -= speed; break case "右": y += speed; break case "左上": y -= speed; x -= speed; break case "右上": y -= speed; x += speed; break case "左下": y += speed; x -= speed; break case "右下": y += speed; x += speed; break default: y -= speed; break } var t_array = [] t_array['x'] = x t_array['y'] = y; return t_array; }
关键字词:EGRET##