找到用于旋转给定实体的delta角度,其中位置,初始旋转和目标点为“面向”(Find delta angle to use for rotation given entity with positio

编程入门 行业动态 更新时间:2024-10-09 19:21:58
找到用于旋转给定实体的delta角度,其中位置,初始旋转和目标点为“面向”(Find delta angle to use for rotation given entity with position, initial rotation and target point to 'face toward')

我用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();

我不太擅长数学问题,所以代码会有用而不是公式:)

输入

实体的当前位置 实体的当前旋转(极化导向),以度为单位

产量

要加上或减去当前旋转的度数或弧度 ...或表示为极向角度的度数或弧度

如果你能提供帮助,请提前致谢:)

克里斯

I'm coding a flocking algorithm in Java but I'm stuck at a certain point (using the Ardor3D libraries, in a 2D plane).

Basically, I need to find the angle difference to add to the current rotation. If you can only get the way it should be pointing with polar coordinates with 0 degs at north and not the difference, not to worry -- I have a method which returns the angle difference taking into account the wraparound on the angle and negative angles.

At the moment, I have the following code, which clearly wouldn't work since the algorithm has no reference to the initial rotation:

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();

Updated code (not working, from first answer):

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();

Another modification based on the other answer:

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();

I'm not really too good at Maths problems, so code would be helpful rather than formulas :)

Inputs

Current position of entity Current rotation of entity (polar-oriented) in degrees

Output

Degrees or radians to add or subtract to current rotation ... or degrees or radians expressed as polar-oriented angle

Thanks in advance if you can help :)

Chris

最满意答案

您可以使用点积来确定当前方向与您想要面对的点之间的角度余弦(以弧度表示)。 假设进行观察的代理位于原点并且定向为面向由相对于Y轴的角度θ给出的某个方向(即0度是“向上”或“向北”)。 您想要找到该方向与面向点(x,y)之间的角度差θ'。 这是由:

θ' = cos-1[(x*sin(θ) + y*cos(θ)) / sqrt(x2 + y2)]

从θ减去θ'将使药剂朝向目标。

如果代理不在原点,只需从要查看的对象中减去代理的位置,即可将其置于上面的表单中。

You can use the dot product to determine the cosine of the angle (in radians) between the current orientation and the point you want to face. Assume the agent doing the viewing is locate at the origin and oriented to face some direction given by the angle θ relative to the Y-Axis (i.e. 0 degrees is "up" or "north"). You want to find the angular difference θ' between that direction and facing a point (x, y). That is given by:

θ' = cos-1[(x*sin(θ) + y*cos(θ)) / sqrt(x2 + y2)]

Subtracting θ' from θ will orient the agent towards the target.

If the agent is not at the origin, simply subtract the position of the agent from the object to view to bring it into the form above.

更多推荐

本文发布于:2023-04-29 09:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335609.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   点为   角度   位置   目标

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!