我正在用Java编写一个集群算法,但是我被困在了某个点(使用Ardor3D库,在2D平面上)。
基本上,我需要找到角度的差异,以增加当前的旋转。如果你只能得到它指向极坐标的方式,在北边有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();我不太擅长数学问题,所以代码比公式更有帮助:)
输入
中
输出
表示的度或弧度。
如果您能帮忙,请提前感谢:)
克里斯
发布于 2010-11-27 03:50:27
稍微偏离主题,但您可能会发现粒子群和漫游代码在我们的效果包(ardor3d-效果)也很有趣。
它们可以在热情的svn中找到,或者在这里找到:
http://ardorlabs.trac.cvsdude.com/Ardor3Dv1/browser/trunk/ardor3d-effects/src/main/java/com/ardor3d/extension/effect/particle/
发布于 2010-10-29 16:34:00
这是一个关于如何找到两个具有共同起源的向量之间的角度甲虫的实现。这是基于下面描述的算法:http://www.wikihow.com/Find-the-Angle-Between-Two-Vectors而被黑的。
public class DeltaDoodle {
private double delta(ColumnVector v1,ColumnVector v2) throws Exception{
double sp=scalarProduct(v1,v2);
double magV1=magnitude(v1);
double magV2=magnitude(v2);
return Math.acos(sp/(magV1*magV2)) * (180/Math.PI);
}
private double scalarProduct(ColumnVector a, ColumnVector b) {
return (a.x*b.x) + (a.y*b.y);
}
private double magnitude(ColumnVector a){
return Math.sqrt((a.x*a.x) + (a.y*a.y));
}
public static void main(String[] args) {
DeltaDoodle v=new DeltaDoodle();
try {
System.out.println("angle: " + v.delta(new ColumnVector(5, 5), new ColumnVector(1,1)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class ColumnVector {
public final double x, y;
public ColumnVector(double x1, double x2) {
this.x = x1;
this.y = x2;
}
}希望能帮上忙..。
https://stackoverflow.com/questions/4053396
复制相似问题