【发布时间】:2013-03-13 19:02:52
【问题描述】:
我的 mob AI 遇到了麻烦
它工作到一定程度,除了它与我想要的相反,这意味着它在错误的方向上转动 180 度
Target = playerST.Posistion;
Vector2 trivial;
trivial.X = Posistion.X - Target.X;
trivial.Y = Posistion.Y - Target.Y;
instant = ((float)Math.Atan2(trivial.Y, trivial.X)) + 3.141592f;
这告诉我我的目标在哪里,并计算出我需要旋转到的数字
它的弧度加上 3.1etc 就像 180 度,因为以这种方式计算它会给我一个最小值 -3.141 或最大值 3.141 但是敌人的旋转是在 0 到 6.28 之间完成的,加上 3.141 使得瞬间 = 在敌人旋转的范围内,而不是在 3.141 的范围内
无论如何,这是我卡在的部分......实际的旋转
// irrelevant
if (attack == true)
{
Vector2 modelVelocityAdd = Vector2.Zero;
modelVelocityAdd.Y = -(float)Math.Sin(rotation);
modelVelocityAdd.X = -(float)Math.Cos(rotation);
modelVelocityAdd *= (0.00002f * speed);
if ((((rotation) + 0.2f)) < instant && (rotation - 0.2f) > instant)
{
modelVelocity += modelVelocityAdd;
}
// not so irrelvant and needs fixing!
if (instant < rotation )
{
rotation -= rotationspeed / 2000;
}
else if (rotation < instant)
{
rotation += rotationspeed / 2000;
}
所以我的问题是我如何阻止它向错误的方向旋转 180 度并真正让它面对玩家而不是完全相反
我不能简单地做下面的事情,因为船被困在 5 度和负 5 度之间来回移动
if (instant < rotation )
{
rotation += rotationspeed / 2000;
}
else if (rotation < instant)
{
rotation -= rotationspeed / 2000;
}
感谢您的帮助
【问题讨论】:
-
不聪明,但你不能把极性反转吗?我的意思是交换你的旋转速度 -= 和 +=
-
看看代码本质的Chapter 6,具体来说,示例6.1。
-
我当然想到了……但如果我这样做……敌舰卡住了,它有点像这样去 +10 度达到某个点回到负 10像那样来回……所以我不能简单地交换它们
-
@QuantumArchi - 我相信来回走动表明它正在工作。你需要有一种方法让它完全不改变——你没有。
-
@Hogan - 是的,但它的来回不是针对目标,而是来回 0
标签: c# xna rotation artificial-intelligence