为什么我的寻的导弹算法的工作? [英] Why isn't my homing missile algorithm working?

查看:109
本文介绍了为什么我的寻的导弹算法的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经采取了code多数民众赞成深受这个答案的灵感,但我弹不归位我所期望的方式。初始弹方向通常垂直于目标。在这一点上,它似乎家中对他的方向,但如果通行证了他,似乎陷在一个地方可以和它冻结在一个点,但随后似乎遵循的目标,使没有在其预期的移动动作速度。我评论过一行code,我很担心。他用V3和V4在他的算法,我怀疑是他的一部分,一个错字,但我不知道。如果有人能帮助我,我在做什么错在这里,我将非常感激。

  normalizedDirectionToTarget = root.vector.normalize(target.pos.x  -  attack.x,target.pos.y  -  attack.y)#V4
V3 = root.vector.normalize(attack.velocity.x,attack.velocity.y)
normalizedVelocity = root.vector.normalize(attack.velocity.x,attack.velocity.y)

angleInRadians = Math.acos(normalizedDirectionToTarget.x *的V3.x + normalizedDirectionToTarget.y * V3.y)
maximumTurnRate = 50 #in度
maximumTurnRateRadians = maximumTurnRate *(Math.PI / 180)
signOfAngle =如果angleInRadians> = 0,1,否则(-1)
angleInRadians = signOfAngle * _.min([Math.abs(angleInRadians),maximumTurnRateRadians])
速度= 3
attack.velocity = root.vector.normalize(normalizedDirectionToTarget.x + Math.sin(angleInRadians),normalizedDirectionToTarget.y + Math.cos(angleInRadians))#我很担心,这是我的错误的根源
attack.velocity.x = attack.velocity.x *速度
attack.velocity.y = attack.velocity.y *速度
attack.x = attack.x + attack.velocity.x
attack.y = attack.y + attack.velocity.y
 


修改:code,它的工作原理

  normalizedDirectionToTarget = root.vector.normalize(target.pos.x  -  attack.x,target.pos.y  -  attack.y)#V4
normalizedVelocity = root.vector.normalize(attack.velocity.x,attack.velocity.y)
angleInRadians = Math.acos(normalizedDirectionToTarget.x * normalizedVelocity.x + normalizedDirectionToTarget.y * normalizedVelocity.y)
maximumTurnRate = 0.3 #in度
maximumTurnRateRadians = maximumTurnRate *(Math.PI / 180)
交叉积= normalizedDirectionToTarget.x * normalizedVelocity.y  -  normalizedDirectionToTarget.y * normalizedVelocity.x
signOfAngle =如果双重交叉> = 0,-1其他1
angleInRadians = signOfAngle * _.min([angleInRadians,maximumTurnRateRadians])
速度= 1.5
xPrime = attack.velocity.x * Math.cos(angleInRadians) -  attack.velocity.y * Math.sin(angleInRadians)
yPrime = attack.velocity.x * Math.sin(angleInRadians)+ attack.velocity.y * Math.cos(angleInRadians)
attack.velocity = root.vector.normalize(xPrime,yPrime)
attack.velocity.x * =速度
attack.velocity.y * =速度
attack.x = attack.x + attack.velocity.x
attack.y = attack.y + attack.velocity.y
 

解决方案

据我,如果你有一个向量(x,y)和你想要的角度THETA关于原点,新的向量旋转( X1,Y1)变为:

X1 = X * COS(THETA) - Y * SIN(THETA)

Y1 = Y * COS(THETA)+ X * SIN(THETA)

(上述可以使用极坐标来导出)

编辑:我不知道,如果我理解正确的,但如果你知道的速度和最终角度的绝对值(比如PHI),那么你为什么不能简单地做:

Vx的=速度* COS(PHI)

Vy速度=速度*罪(PHI)

编辑2:也,同时服用COS逆,可以有多个possiblities为angleinradians。您可能需要检查其中两个向量所在的象限。最高转动速度是在两个方向上50度。因此,余弦的角度将永远是正的。 (余弦为负只为90至270度。

编辑3:我想获取有关+已经转方向或-ve转方向,跨产品是一个更好的主意。

编辑4:VX / Vy速度应该工作,如果你进行了以下工作:

  initialAngleInRadians = Math.atan(normalizedVelocity.y / normalizedVelocity.x)
finalAngleInRadians = initialAngleInRadians + angleInRadians
VX =速度* COS(finalAngleInRadians)
VY =速度*罪(finalAngleInRadians)
 

I've taken code that's heavily inspired by this answer but my projectile is not homing in the way I expect. The initial projectile direction is often perpendicular to the target. At which point, it does seem to home in on his direction, but if it "passes" him, it seems to get stuck in place like it's frozen at a point but then seems to follow the movements the target makes without moving at its intended speed. I've commented a line of code that I'm concerned about. He's using V3 and V4 in his algorithm which I suspect is a typo on his part but I'm not sure. If anyone can help me with what I'm doing wrong here, I'd be very grateful.

normalizedDirectionToTarget = root.vector.normalize(target.pos.x - attack.x, target.pos.y - attack.y) #V4
V3 = root.vector.normalize(attack.velocity.x, attack.velocity.y)
normalizedVelocity = root.vector.normalize(attack.velocity.x, attack.velocity.y)

angleInRadians = Math.acos(normalizedDirectionToTarget.x * V3.x + normalizedDirectionToTarget.y * V3.y)
maximumTurnRate = 50 #in degrees
maximumTurnRateRadians = maximumTurnRate * (Math.PI / 180)
signOfAngle = if angleInRadians >= 0 then 1 else (-1)
angleInRadians = signOfAngle * _.min([Math.abs(angleInRadians), maximumTurnRateRadians])
speed = 3
attack.velocity = root.vector.normalize(normalizedDirectionToTarget.x + Math.sin(angleInRadians), normalizedDirectionToTarget.y + Math.cos(angleInRadians)) #I'm very concerned this is the source of my bug
attack.velocity.x = attack.velocity.x * speed
attack.velocity.y = attack.velocity.y * speed
attack.x = attack.x + attack.velocity.x
attack.y = attack.y + attack.velocity.y


Edit: Code that Works

normalizedDirectionToTarget = root.vector.normalize(target.pos.x - attack.x, target.pos.y - attack.y) #V4
normalizedVelocity = root.vector.normalize(attack.velocity.x, attack.velocity.y)
angleInRadians = Math.acos(normalizedDirectionToTarget.x * normalizedVelocity.x + normalizedDirectionToTarget.y * normalizedVelocity.y)
maximumTurnRate = .3 #in degrees
maximumTurnRateRadians = maximumTurnRate * (Math.PI / 180)
crossProduct = normalizedDirectionToTarget.x * normalizedVelocity.y - normalizedDirectionToTarget.y * normalizedVelocity.x
signOfAngle = if crossProduct >= 0 then -1 else 1
angleInRadians = signOfAngle * _.min([angleInRadians, maximumTurnRateRadians])
speed = 1.5
xPrime = attack.velocity.x * Math.cos(angleInRadians) - attack.velocity.y * Math.sin(angleInRadians)
yPrime = attack.velocity.x * Math.sin(angleInRadians) + attack.velocity.y * Math.cos(angleInRadians)
attack.velocity = root.vector.normalize(xPrime, yPrime)
attack.velocity.x *= speed
attack.velocity.y *= speed
attack.x = attack.x + attack.velocity.x
attack.y = attack.y + attack.velocity.y

解决方案

According to me, if you have a vector (x,y) and you want to rotate it by angle 'theta' about the origin, the new vector (x1,y1) becomes:

x1 = x*cos(theta) - y*sin(theta)

y1 = y*cos(theta) + x*sin(theta)

(the above can be derived using polar coordinates)

EDIT: I'm not sure if I understand correctly, but if you know the speed and the absolute value of the final angle (say phi), then why can't you simply do:

Vx = speed*cos( phi )

Vy = speed*sin( phi )

EDIT 2: also, while taking cos-inverse, there can be multiple possiblities for the angleinradians. You may have to check the quadrant in which both vectors lie. Your maximum turning rate is 50 degrees in either direction. Hence, the cosine for that angle shall always be positive. (cosine is negative only for 90 to 270 degrees.

EDIT 3: I think to get information about +ve turn direction or -ve turn direction, cross product is a better idea.

EDIT 4: Vx / Vy should work if you carry out the following:

initialAngleInRadians = Math.atan(normalizedVelocity.y / normalizedVelocity.x)
finalAngleInRadians = initialAngleInRadians + angleInRadians
Vx = speed*cos(finalAngleInRadians)
Vy = speed*sin(finalAngleInRadians)

这篇关于为什么我的寻的导弹算法的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆