我正在使用Java编写一种群集算法,但是我在某个点上卡住了(在2D平面中使用Ardor3D库)。
基本上,我需要找到要添加到当前旋转的角度差异。如果您只能获取极坐标下应该指向的方式,其中0度为北,而不是差异,请不要担心 - 我有一种方法,该方法考虑了角度和负角度的环绕。
目前,我有以下代码,显然不能工作,因为算法没有参考初始旋转:
基于其他答案的另一种修改方式:
基本上,我需要找到要添加到当前旋转的角度差异。如果您只能获取极坐标下应该指向的方式,其中0度为北,而不是差异,请不要担心 - 我有一种方法,该方法考虑了角度和负角度的环绕。
目前,我有以下代码,显然不能工作,因为算法没有参考初始旋转:
long tpf = currUpdateTimeMS - lastUpdateTimeMS;
Vector2 pos = new Vector2();
rt.getPosition(pos);
double rot = pos.angleBetween(app.getAvgBoidPos(new Vector2()).normalizeLocal());
rt.setRotation(rot);
pos.addLocal(
Math.cos((rot - MathUtils.HALF_PI)) * (tpf / 10f),
Math.sin((rot - MathUtils.HALF_PI)) * (tpf / 10f)
);
rt.setPosition(pos);
super.updateLogic();
更新后的代码(不起作用,来自第一个答案):
long tpf = currUpdateTimeMS - lastUpdateTimeMS;
//rt.setRotation(rt.getRotation() + ((tpf / (ROT_SPEED / 2f)) % 360));
Vector2 avgpos = app.getAvgBoidPos(new Vector2());
Vector2 pos = rt.getPosition(new Vector2());
avgpos.subtractLocal(pos);
double angleRads = rt.getRotation() * FastMath.DEG_TO_RAD;
double rot = MathUtils.acos((
(avgpos.getX() * MathUtils.sin(angleRads)
) +
(avgpos.getY() * MathUtils.cos(angleRads)
)) / ((Math.pow(avgpos.getX(), 2) + Math.pow(avgpos.getY(), 2)) * 0.5));
double adegdiff = rot * FastMath.RAD_TO_DEG;
rt.setRotation(rt.getRotation() - adegdiff);
double newrot = rt.getRotation();
pos.addLocal(
Math.cos((newrot - MathUtils.HALF_PI)) * (tpf / 10f),
Math.sin((newrot - MathUtils.HALF_PI)) * (tpf / 10f)
);
rt.setPosition(pos);
super.updateLogic();
基于其他答案的另一种修改方式:
long tpf = currUpdateTimeMS - lastUpdateTimeMS;
//rt.setRotation(rt.getRotation() + ((tpf / (ROT_SPEED / 2f)) % 360));
Vector2 avgpos = app.getAvgBoidPos(new Vector2());
Vector2 pos = rt.getPosition(new Vector2());
avgpos.subtractLocal(pos);
double rot = pos.angleBetween(
app.getAvgBoidPos(new Vector2()).normalizeLocal()
) - (rt.getRotation() * MathUtils.DEG_TO_RAD);
rt.setRotation(rt.getRotation() - (rot * MathUtils.RAD_TO_DEG));
double newrot = rt.getRotation();
pos.addLocal(
Math.cos((newrot - MathUtils.HALF_PI)) * (tpf / 10f),
Math.sin((newrot - MathUtils.HALF_PI)) * (tpf / 10f)
);
rt.setPosition(pos);
super.updateLogic();
我不太擅长数学问题,所以编程代码比公式更有帮助 :)
输入
- 实体的当前位置
- 实体的当前旋转角度(极坐标定向)以度为单位
输出
- 要添加或减去到当前旋转角度的度数或弧度
- ... 或表示为极坐标定向角度的度数或弧度
如果您能提供帮助,谢谢!
克里斯
rt.getPosition
给出实体的位置,而app.getAvgBOIDPos
给出目标位置,是这样吗?那么什么可以给出实体的方向呢? - Beta