导弹系统数学题(代码很少) [英] Missile System Math Problem (very few code)

查看:32
本文介绍了导弹系统数学题(代码很少)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望导弹跟随以下规则的玩家:
- 当玩家相对于导弹位于右侧时,以固定速率向右改变旋转
- 否则以相同的速度向左旋转

I want a Missile to follow a player with following rules:
- when the player is at the right side relative to the missile, change the rotation to the right at a fixed rate
- else change the rotation to the left at the same rate

            if (missile.Rotation > 0 && missile.Rotation < Math.PI)
            {
                if (angle > missile.Rotation && angle < missile.Rotation + Math.PI)
                {
                    missile.Rotation += 0.05f;
                }
                else
                {
                    missile.Rotation -= 0.05f;
                }
            }
            else
            {
                if (angle < missile.Rotation && angle > missile.Rotation - Math.PI)
                {
                    missile.Rotation -= 0.05f;
                }
                else
                {
                    missile.Rotation += 0.05f;
                }
            }  
            missile.Position += new Vector2((float)(5 * gameTime.ElapsedGameTime.TotalSeconds * Math.Cos(missile.Rotation)), (float)(5 * gameTime.ElapsedGameTime.TotalSeconds * Math.Sin(missile.Rotation)));

用这个代码,一些导弹似乎有问题,当我和玩家一起跳跃时,导弹进入一个无法停止的无限循环(它们在一个圆圈中飞行)

With this code, some missiles seem to bug, when I jump with the player, the missiles go into a endless loop which cannot be stopped (they fly in a circle)

坐标系和XNA一样(360/0°在右边,270°在上面……)

有人可以帮我吗?

推荐答案

使用欧拉角是一种非常低效的方法.使用向量要容易得多.

Using Euler angles is a very inefficient way to do this. It is much easier to use vectors.

让 P 是玩家的位置,M 是导弹的位置.您需要从中计算出两个向量:MP 和 Mr,其中 MP = 从导弹到玩家的向量,Mr 是指向导弹右侧的向量.现在计算向量 MP 和 Mr 之间的点积.结果的符号告诉你朝哪个方向转动导弹.如果为正则顺时针转动导弹,如果为负则逆时针转动导弹.

Let P be the position of the player and M the position of the missile. You need to calculate two vectors from these: MP and Mr where MP = vector from missile to player and Mr is the vector pointing to the missile's right hand side. Now calculate the dot product between the vectors MP and Mr. The sign of the result tells you which way to turn the missile. If it's positive then turn the missile clockwise, if it's negative then turn the missile anticlockwise.

为什么会这样?

两个向量 A 和 B 的点积可以写成:

The dot product of two vectors A and B can be written as:

Ax.Bx + Ay.By

还有:

|A|.|B|.cos (theta)

其中 theta 是两个向量之间的夹角,|x|是向量 x 的大小.这给了我们:

where theta is the angle between the two vectors and |x| is the magnitude of vector x. This gives us:

cos (theta) = (Ax.Bx + Ay.By) / (|A|.|B|)

由于我们只想知道值 cos (theta) 的符号,正值是 +- 90 度之间的角度,负值是 90 到 270 度之间的角度,我们只需要计算分子的符号,分母总是正数!因此点积的符号会告诉我们目标在哪一边.

Since we only want to know the sign of the value cos (theta), positive values being angles between +- 90 degrees and negative values being angles between 90 and 270 degrees, we only need to work out the sign of the numerator, the denominator is always positive! Thus the sign of the dot product will tell us which side the target is on.

但是,您在想,向量不是比角度更难使用吗?我不需要做一些触发函数来计算导弹的右向量吗?

But, you're thinking, aren't vectors harder to use than angles? Won't I need to do some trig functions to calculate the missile's right vector?

理想情况下,您根本不应该使用欧拉角做任何事情.您应该使用矩阵来存储世界中对象的位置和旋转.导弹的右向量只是导弹位置/方向矩阵的顶行(或第一列,取决于您如何设置系统).并且矩阵是渲染硬件用来绘制对象的东西,因此您不会因为使用它们而增加任何开销(实际上,使用角度会增加开销,因为需要将角度表示法转换为用于渲染的矩阵).

Ideally, you shouldn't be using Euler angles at all for anything. You should be using matrices to store the position and rotation of objects in your world. The missile's right vector is just the top row of the missile's position/orientation matrix (or first column, depends how you set up your system). And matrices are what the rendering hardware uses to draw objects so you're not adding any overhead by using them (in fact, using angles adds overhead as the angle notation needs to be converted to matrices for the rendering).

您的代码最终应如下所示:

You code should end up looking like this:

if (VectorDotProduct (missile.right, player.pos - missil.pos) > 0)
{
   missile.rotate clockwise
}
else
{
   missile.rotate anti-clockwise
}

这篇关于导弹系统数学题(代码很少)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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