我想做一个类似传奇那样控制人物移动的 小游戏
要用鼠标控制人物移动的方向
请问用什么算法能 让人物往鼠标点击的方向移动呢???
请大侠们指教一下
谢谢
------解决方案--------------------------------------------------------
给你一个函数,计算在两线所在方向上离第一个点指定距离的点的函数
- Java code
// 在两点所在直线上,以从startPoint到endPoint为方向,离startPoint的距离disToStartPoint的点 public static Point2D extentPoint(Point2D startPoint, Point2D endPoint, double disToStartPoint) { double disX = endPoint.getX() - startPoint.getX(); double disY = endPoint.getY() - startPoint.getY(); double dis = Math.sqrt(disX * disX + disY * disY); double sin = (endPoint.getY() - startPoint.getY()) / dis; double cos = (endPoint.getX() - startPoint.getX()) / dis; double deltaX = disToStartPoint * cos; double deltaY = disToStartPoint * sin; return new Point2D.Double(startPoint.getX() + deltaX, startPoint.getY() + deltaY); }
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
利用反正切可以很容易求出点在坐标系中与x轴正向的夹角
- Java code
// 求点与x轴正向的夹角 public static double degreeOfPoint(Point2D p) { return degreeOfPoint(p.getX(), p.getY()); } public static double degreeOfPoint(double x, double y) { // 先求在一或者四象限的角度值,再判断是否在二三象限 double radian = Math.atan(y / x); if ((x < 0 && y < 0) || (x < 0 && y > 0)) { radian += Math.PI; } return Math.toDegrees(radian); } public static void main(String[] args) { System.out.println(degreeOfPoint(20, 20)); System.out.println(degreeOfPoint(-20, 20)); System.out.println(degreeOfPoint(-20, -20)); System.out.println(degreeOfPoint(20, -20)); System.out.println(degreeOfPoint(40, 20)); System.out.println(degreeOfPoint(-20, 30)); System.out.println(degreeOfPoint(-50, -230)); System.out.println(degreeOfPoint(120, -60)); System.out.println(degreeOfPoint(120, -0)); }